|
1 | 1 | """ |
2 | | -A simple app demonstrating how to dynamically render tab content containing |
3 | | -dcc.Graph components to ensure graphs get sized correctly. We also show how |
4 | | -dcc.Store can be used to cache the results of an expensive graph generation |
5 | | -process so that switching tabs is fast. |
| 2 | +When rendering Plotly graphs as the children of tabs, sometimes the graph will |
| 3 | +not be sized correctly if it wasn't initially visible. The solution to this |
| 4 | +problem is to render the tab content dynamically using a callback. |
| 5 | +
|
| 6 | +This example shows how to do that, and also shows how to use a dcc.Store |
| 7 | +component to cache the graph data so that if the generating process is slow, |
| 8 | +the graph still renders quickly when the user switches tabs. |
6 | 9 | """ |
7 | 10 |
|
8 | 11 | import time |
|
13 | 16 | import plotly.graph_objs as go |
14 | 17 | from dash import Input, Output, dcc, html |
15 | 18 |
|
| 19 | +EXPLAINER = """This example shows how to use callbacks to render graphs inside |
| 20 | +tab content to ensure that they are sized correctly when switching tabs. It |
| 21 | +also demonstrates use of a `dcc.Store` component to cache graph data so that |
| 22 | +if the data generating process is expensive, switching tabs is still quick.""" |
| 23 | + |
16 | 24 | app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP]) |
17 | 25 |
|
18 | 26 | app.layout = dbc.Container( |
19 | 27 | [ |
20 | | - dcc.Store(id="store"), |
21 | 28 | html.H1("Dynamically rendered tab content"), |
22 | | - html.Hr(), |
| 29 | + dcc.Markdown(EXPLAINER), |
23 | 30 | dbc.Button( |
24 | 31 | "Regenerate graphs", |
25 | 32 | color="primary", |
|
34 | 41 | id="tabs", |
35 | 42 | active_tab="scatter", |
36 | 43 | ), |
37 | | - html.Div(id="tab-content", className="p-4"), |
| 44 | + # we wrap the store and tab content with a spinner so that when the |
| 45 | + # data is being regenerated the spinner shows. delay_show means we |
| 46 | + # don't see the spinner flicker when switching tabs |
| 47 | + dbc.Spinner( |
| 48 | + [ |
| 49 | + dcc.Store(id="store"), |
| 50 | + html.Div(id="tab-content", className="p-4"), |
| 51 | + ], |
| 52 | + delay_show=100, |
| 53 | + ), |
38 | 54 | ] |
39 | 55 | ) |
40 | 56 |
|
|
0 commit comments