Search code examples
pythonhtmlplotly-dashplotly-python

HTML Plotly Dash


I found one HTML template that fits the dashboard very well. In the beginning, I figured out how to display and update the graph. Then, while posting the page, I ran into the problem of building HTML in Plotly Dash. HTML structure

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">CARD_ONE</h4>
                <a class="heading-elements-toggle"><i class="la la-ellipsis-v font-medium-3"></i></a>
                <div class="heading-elements">
                    <ul class="list-inline mb-0">
                        <li><a data-action="collapse"><i class="ft-minus"></i></a></li>
                        <li><a data-action="expand"><i class="ft-maximize"></i></a></li>
                    </ul>
                </div>
            </div>
            <div class="card-content collapse show">
                <div class="card-body chartjs">
                  !!! dash chart !!!  <canvas id="line-chart" height="500"></canvas>
                </div>
            </div>
        </div>
    </div>
</div>

Can't figure out the plot structure in Plotly Dash.

code dash.

def serve_layout():
    return html.Div(
        children=[
            html.H4(children='OWERVIEW'),
            html.Div(id='my-id', className='card', children='''ONE_CARD'''),
            html.Div(className='card-header'),
            html.H4(className="card-title", children='ONE_CARD??'),
            html.Div(className='heading-elements-toggle'),
            html.Div(className='heading-elements'),
            html.Div(className="list-inline mb-0"),
            dcc.Graph(id='example-graph', animate=True, responsive=True),
            dcc.Interval(
                id='interval-component',
                interval=3 * 1000,
                n_intervals=0,
            ),
        ],
    )

All the components in the code are executed line by line and the entire HTML structure does not look like this.

Question: What should the HTML structure look like in Dash Plotly code?


Solution

  • You'll need to add IDs to some components, and they can be whatever. Not quite sure about the data-action part, though. Overall, it should look like this:

    def serve_layout():
        return html.Div(className='row', children=[
            html.Div(className='col-12', children=[
                html.Div(className='card', children=[
                    html.Div(className='card-header', children=[
                        html.Div(id='my-id', className='card', children='''ONE_CARD'''),
                        html.A(className='header-elements-toggle', children=html.I(className='la la-ellipsis-v font-medium-3'))
                        html.Div(className='heading-elements', children=[
                            html.Ul(className='list-inline mb-0', children=[
                                html.Li(html.A(html.I(className='ft-minus'))),
                                html.Li(html.A(html.I(className='ft-maximize'))),
                            ]),
                        ]),
                    ]),
                    html.Div(className='card-content collapse show', children=[
                        html.Div(className='card-body chartjs', children=[
                            dcc.Graph(id='example-graph', animate=True, responsive=True),
                            dcc.Interval(
                                id='interval-component',
                                interval=3 * 1000,
                                n_intervals=0,
                            ),
                        ]),
                    ]),
                ]),
            ]),
        ]),