Plotly-Dash:如何使用 dash bootstrap 组件设计布局?

Tim*_*unn 6 plotly plotly-dash

我对 Dash Plotly 很陌生,我正在尝试弄清楚如何设计这样的布局。

布局

在此处输入图片说明

据我了解,使用 dash bootstrap 组件可以更轻松地完成此操作。 https://dash-bootstrap-components.opensource.faculty.ai 作为第一步,我应该重现布局(灰色瓷砖),作为第二步,我应该添加一些文本和一些图形。只是基本的。

谢谢你。

Kri*_*aga 8

您应该查看此链接以了解有关 Dash Bootstrap 组件以及如何构建布局的更多信息。

我已经使用JupyterDash与您所需的布局相匹配的示例。

在此处输入图片说明

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')
Run Code Online (Sandbox Code Playgroud)

  • 这是一个漂亮且非常简单的转发示例。谢谢你。 (2认同)