forked from radovankavicky/dash-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_callbacks.py
More file actions
94 lines (73 loc) · 2.68 KB
/
Copy pathbasic_callbacks.py
File metadata and controls
94 lines (73 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import dash_core_components as dcc
import dash_html_components as html
import styles
import tools
examples = [
tools.load_example(s) for s in [
'tutorial/examples/basic_callbacks_example_1.py',
'tutorial/examples/basic_callbacks_example_2.py',
'tutorial/examples/basic_callbacks_events.py'
]
]
layout = html.Div(children=[
dcc.Markdown('''
## Interactivity with Callbacks
The heart and soul of Dash is providing an easy way to bind Python
callbacks to web interfaces.
With Dash's callback decorators, you can update certain components
when other components change values. Here's a practical example.
View the interactive app below the code.
'''.replace(' ', '')),
dcc.SyntaxHighlighter(
examples[0][0], language='python', customStyle=styles.code_container
),
html.Div('This code will generate an app like this:'),
html.Div(examples[0][1], className='example-container')
])
layout.children.extend([
dcc.Markdown('''
## Multiple Inputs
Let's extend this example by including
a secondary input element that controls
which variable we should plot.
Dash apps are "reactive" which means that
whenever values change in the front-end,
the callback functions will get called
automatically.
'''),
dcc.SyntaxHighlighter(
examples[1][0], language='python', customStyle=styles.code_container),
html.Div(examples[1][1], className='example-container')
])
layout.children.extend([
dcc.Markdown('''
## Events and States
Reactive interfaces are great when the callbacks are fast.
As a user, the delay between selected an option in the dropdown
and seeing the graph update is small and the interface feels
snappy.
An alternative to these types of reactive interfaces is subscribing
explicitly to events. Subscribing to events is great when your
callbacks takes at least a couple of seconds to run or when
you would like your users to update a set of controls before showing
them the output.
You can subscribe to event changes with the
`dash.dependencies.Event` object. Most Dash components will have
events that you can subscribe to. To see the available events of any
dash component, either call `help()` or look up a list with the
`available_events` property:
```
>>> print(html.Button.available_events)
['click']
>>> print(dcc.Graph.available_events)
['restyle', 'relayout']
```
In this example, we'll update our graph when we click on the button.
We'll pass in the currently selected values of the dropdowns through
the `state` arguments.
'''),
dcc.SyntaxHighlighter(
examples[2][0], language='python', customStyle=styles.code_container
),
html.Div(examples[2][1], className='example-container')
])