Skip to content

Commit 11ed631

Browse files
authored
[examples] Graph in tabs explainer (#1030)
* Explain graph_in_tabs example better * format
1 parent ba5ddfd commit 11ed631

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

examples/advanced-component-usage/graphs_in_tabs.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
"""
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.
69
"""
710

811
import time
@@ -13,13 +16,17 @@
1316
import plotly.graph_objs as go
1417
from dash import Input, Output, dcc, html
1518

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+
1624
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
1725

1826
app.layout = dbc.Container(
1927
[
20-
dcc.Store(id="store"),
2128
html.H1("Dynamically rendered tab content"),
22-
html.Hr(),
29+
dcc.Markdown(EXPLAINER),
2330
dbc.Button(
2431
"Regenerate graphs",
2532
color="primary",
@@ -34,7 +41,16 @@
3441
id="tabs",
3542
active_tab="scatter",
3643
),
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+
),
3854
]
3955
)
4056

0 commit comments

Comments
 (0)