Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly-Dash: How to design the layout using dash bootstrap components?

I'm very new to Dash Plotly and I'm trying to figure out how can I design a layout like this.

Layout:

enter image description here

As I understood, this can be done more easy using dash bootstrap components. https://dash-bootstrap-components.opensource.faculty.ai As a first step I should reproduce the layout (grey tiles) and as a second step, I should add some text and some graphs. Just basic.

Thank you.

like image 252
Tim Dunn Avatar asked Aug 26 '20 07:08

Tim Dunn


People also ask

What are the layout components of bootstrap?

There are three main layout components in dash-bootstrap-components: Container , Row , and Col . The Container component can be used to center and horizontally pad your app's content.

Can you use bootstrap with dash?

Bootstrap components are available as native Dash components to let you easily incorporate them into your Dash apps. Each component exposes a number of props to let you control the behaviour with Dash callbacks.

What is Dash app layout?

The layout of a Dash app describes what the app looks like. The layout is a hierarchical tree of components. Dash HTML Components ( dash. html ) provides classes for all of the HTML tags and the keyword arguments describe the HTML attributes like style , class , and id .

What are dash components?

Dash is a web app framework that provides pure Python abstraction around HTML, CSS, and JavaScript. Instead of writing HTML or using an HTML templating engine, you compose your layout using Python with the Dash HTML Components module ( dash. html ).


2 Answers

You should check out this link to learn more about Dash Bootstrap Components, and how to structure your layout.

I have made an example using JupyterDash that matches your desired layout.

enter image description here

import plotly.express as px
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
import plotly.express as px

# Iris bar figure
def drawFigure():
    return  html.Div([
        dbc.Card(
            dbc.CardBody([
                dcc.Graph(
                    figure=px.bar(
                        df, x="sepal_width", y="sepal_length", color="species"
                    ).update_layout(
                        template='plotly_dark',
                        plot_bgcolor= 'rgba(0, 0, 0, 0)',
                        paper_bgcolor= 'rgba(0, 0, 0, 0)',
                    ),
                    config={
                        'displayModeBar': False
                    }
                ) 
            ])
        ),  
    ])

# Text field
def drawText():
    return html.Div([
        dbc.Card(
            dbc.CardBody([
                html.Div([
                    html.H2("Text"),
                ], style={'textAlign': 'center'}) 
            ])
        ),
    ])

# Data
df = px.data.iris()

# Build App
app = JupyterDash(external_stylesheets=[dbc.themes.SLATE])

app.layout = html.Div([
    dbc.Card(
        dbc.CardBody([
            dbc.Row([
                dbc.Col([
                    drawText()
                ], width=3),
                dbc.Col([
                    drawText()
                ], width=3),
                dbc.Col([
                    drawText()
                ], width=3),
                dbc.Col([
                    drawText()
                ], width=3),
            ], align='center'), 
            html.Br(),
            dbc.Row([
                dbc.Col([
                    drawFigure() 
                ], width=3),
                dbc.Col([
                    drawFigure()
                ], width=3),
                dbc.Col([
                    drawFigure() 
                ], width=6),
            ], align='center'), 
            html.Br(),
            dbc.Row([
                dbc.Col([
                    drawFigure()
                ], width=9),
                dbc.Col([
                    drawFigure()
                ], width=3),
            ], align='center'),      
        ]), color = 'dark'
    )
])

# Run app and display result inline in the notebook
app.run_server(mode='external')
like image 93
Kristian Haga Avatar answered Oct 11 '22 22:10

Kristian Haga


Yes - this can be done with dash-bootstrap. Since the screen layout is divided into 12 columns - you'll have to set the width by the number of columns you would like each section to take. So if for example, you need 4 columns, each should be width a width=3.

Your layout would look something like this - 3 rows, the first with 4 columns, the second with 3 columns, the third with 2 columns. After building the layout - you can play with the width of the columns in each row - so it would fit the layout you need

dbc.Row([dbc.Col([content]),dbc.Col([content]),dbc.Col([content]),dbc.Col([content])]),
dbc.Row([dbc.Col([content]),dbc.Col([content]),dbc.Col([content])]),
dbc.Row([dbc.Col([content]),dbc.Col([content])])

You can see a detailed explanation here

Here is a working dash example:

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
import dash_bootstrap_components as dbc


app = dash.Dash(external_stylesheets=[dbc.themes.CYBORG])

app.layout = \
dbc.Container\
([
    html.Br(),
    dbc.Row([
    dbc.Col([dbc.Button("row 1 col 1",style={"width":"100%"})],width=3),
    dbc.Col([dbc.Button("row 1 col 2", style={"width": "100%"})],width=3),
    dbc.Col([dbc.Button("row 1 col 3",style={"width":"100%"})],width=3),
    dbc.Col([dbc.Button("row 1 col 4",style={"width":"100%"})],width=3),
    ]),
    html.Br(),
    dbc.Row([
    dbc.Col([dbc.Button("row 2 col 1",style={"width":"100%"})],width=3),
    dbc.Col([dbc.Button("row 2 col 2", style={"width": "100%"})],width=3),
    dbc.Col([dbc.Button("row 2 col 3",style={"width":"100%"})],width=6),
    ]),
    html.Br(),
    dbc.Row([
    dbc.Col([dbc.Button("row 3 col 1",style={"width":"100%"})],width=9),
    dbc.Col([dbc.Button("row 3 col 2", style={"width": "100%"})],width=3),
    ])
])

if __name__ == "__main__":
    app.run_server(debug=False, port=8050, host='0.0.0.0')
like image 43
Roee Anuar Avatar answered Oct 12 '22 00:10

Roee Anuar