Skip to content

Commit 2ddc13e

Browse files
Merge pull request #4 from delsim/master
Improved example
2 parents 45bc03b + a8bd6f3 commit 2ddc13e

File tree

4 files changed

+73
-64
lines changed

4 files changed

+73
-64
lines changed

README.md

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cd django-plotly-dash
4343
To use existing dash applications, first register them using the `DelayedDash` class. This
4444
replaces the `dash.Dash` class of `plotly.py.`
4545

46-
Taking as an example a slightly modified variant of one of the [getting started](https://dash.plot.ly/getting-started-part-2) examples:
46+
Taking a very simple example inspired by the excellent [getting started](https://dash.plot.ly/) documentation:
4747

4848
```python
4949
import dash
@@ -52,39 +52,52 @@ import dash_html_components as html
5252

5353
from django_plotly_dash import DelayedDash
5454

55-
app = DelayedDash('SimpleExample') # replaces dash.Dash
55+
app = DelayedDash('SimpleExample')
5656

5757
app.layout = html.Div([
5858
dcc.RadioItems(
59-
id='dropdown-a',
60-
options=[{'label': i, 'value': i} for i in ['Canada', 'USA', 'Mexico']],
61-
value='Canada'
59+
id='dropdown-color',
60+
options=[{'label': c, 'value': c.lower()} for c in ['Red', 'Green', 'Blue']],
61+
value='red'
6262
),
63-
html.Div(id='output-a'),
64-
63+
html.Div(id='output-color'),
6564
dcc.RadioItems(
66-
id='dropdown-b',
67-
options=[{'label': i, 'value': i} for i in ['MTL', 'NYC', 'SF']],
68-
value='MTL'
65+
id='dropdown-size',
66+
options=[{'label': i, 'value': j} for i, j in [('L','large'), ('M','medium'), ('S','small')]],
67+
value='medium'
6968
),
70-
html.Div(id='output-b')
69+
html.Div(id='output-size')
7170

7271
])
7372

7473
@app.callback(
75-
dash.dependencies.Output('output-a', 'children'),
76-
[dash.dependencies.Input('dropdown-a', 'value')])
77-
def callback_a(dropdown_value):
78-
return 'You\'ve selected "{}"'.format(dropdown_value)
79-
74+
dash.dependencies.Output('output-color', 'children'),
75+
[dash.dependencies.Input('dropdown-color', 'value')])
76+
def callback_color(dropdown_value):
77+
return "The selected color is %s." % dropdown_value
8078

8179
@app.callback(
82-
dash.dependencies.Output('output-b', 'children'),
83-
[dash.dependencies.Input('dropdown-a', 'value'),
84-
dash.dependencies.Input('dropdown-b', 'value')])
85-
def callback_b(dropdown_value,other_dd):
86-
return 'You\'ve selected "{}" and "{}"'.format(dropdown_value,
87-
other_dd)
80+
dash.dependencies.Output('output-size', 'children'),
81+
[dash.dependencies.Input('dropdown-color', 'value'),
82+
dash.dependencies.Input('dropdown-size', 'value')])
83+
def callback_size(dropdown_color, dropdown_size):
84+
return "The chosen T-shirt is a %s %s one." %(dropdown_size,
85+
dropdown_color)
86+
87+
a2 = DelayedDash("Ex2")
88+
a2.layout = html.Div([
89+
dcc.RadioItems(id="dropdown-one",options=[{'label':i,'value':j} for i,j in [
90+
("O2","Oxygen"),("N2","Nitrogen"),]
91+
],value="Oxygen"),
92+
html.Div(id="output-one")
93+
])
94+
95+
@a2.callback(
96+
dash.dependencies.Output('output-one','children'),
97+
[dash.dependencies.Input('dropdown-one','value')]
98+
)
99+
def callback_c(*args,**kwargs):
100+
return "Args are %s and kwargs are %s" %("".join(*args),str(kwargs))
88101
```
89102

90103
Note that the `DelayedDash` constructor requires a name to be specified. This name is then used to identify the dash app in
@@ -96,7 +109,7 @@ templates:
96109
{% plotly_item "SimpleExample" %}
97110
```
98111

99-
Note that the registration code needs to be in a location
112+
The registration code needs to be in a location
100113
that will be imported into the Django process before any template tag attempts to use it. The example Django application
101114
in the demo subdirectory achieves this through an import in the main urls.py file; any views.py would also be sufficient.
102115

demo/demo/plotly_apps.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,33 @@
88

99
app.layout = html.Div([
1010
dcc.RadioItems(
11-
id='dropdown-a',
12-
options=[{'label': i, 'value': i} for i in ['Canada', 'USA', 'Mexico']],
13-
value='Canada'
11+
id='dropdown-color',
12+
options=[{'label': c, 'value': c.lower()} for c in ['Red', 'Green', 'Blue']],
13+
value='red'
1414
),
15-
html.Div(id='output-a'),
16-
15+
html.Div(id='output-color'),
1716
dcc.RadioItems(
18-
id='dropdown-b',
19-
options=[{'label': i, 'value': i} for i in ['MTL', 'NYC', 'SF']],
20-
value='MTL'
17+
id='dropdown-size',
18+
options=[{'label': i, 'value': j} for i, j in [('L','large'), ('M','medium'), ('S','small')]],
19+
value='medium'
2120
),
22-
html.Div(id='output-b')
21+
html.Div(id='output-size')
2322

2423
])
2524

2625
@app.callback(
27-
dash.dependencies.Output('output-a', 'children'),
28-
[dash.dependencies.Input('dropdown-a', 'value')])
29-
def callback_a(dropdown_value):
30-
return 'You\'ve selected "{}"'.format(dropdown_value)
31-
26+
dash.dependencies.Output('output-color', 'children'),
27+
[dash.dependencies.Input('dropdown-color', 'value')])
28+
def callback_color(dropdown_value):
29+
return "The selected color is %s." % dropdown_value
3230

3331
@app.callback(
34-
dash.dependencies.Output('output-b', 'children'),
35-
[dash.dependencies.Input('dropdown-a', 'value'),
36-
dash.dependencies.Input('dropdown-b', 'value')])
37-
def callback_b(dropdown_value,other_dd):
38-
return 'You\'ve selected "{}" and "{}"'.format(dropdown_value,
39-
other_dd)
32+
dash.dependencies.Output('output-size', 'children'),
33+
[dash.dependencies.Input('dropdown-color', 'value'),
34+
dash.dependencies.Input('dropdown-size', 'value')])
35+
def callback_size(dropdown_color, dropdown_size):
36+
return "The chosen T-shirt is a %s %s one." %(dropdown_size,
37+
dropdown_color)
4038

4139
a2 = DelayedDash("Ex2")
4240
a2.layout = html.Div([

docs/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Contents
1111

1212
.. toctree::
1313
:maxdepth: 2
14-
:caption: Contents:
1514

1615
installation
1716
simple_use

docs/simple_use.rst

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,34 @@ Taking as an example a slightly modified variant of one of the `getting started
1818

1919
app.layout = html.Div([
2020
dcc.RadioItems(
21-
id='dropdown-a',
22-
options=[{'label': i, 'value': i} for i in ['Canada', 'USA', 'Mexico']],
23-
value='Canada'
21+
id='dropdown-color',
22+
options=[{'label': c, 'value': c.lower()} for c in ['Red', 'Green', 'Blue']],
23+
value='red'
2424
),
25-
html.Div(id='output-a'),
26-
25+
html.Div(id='output-color'),
2726
dcc.RadioItems(
28-
id='dropdown-b',
29-
options=[{'label': i, 'value': i} for i in ['MTL', 'NYC', 'SF']],
30-
value='MTL'
27+
id='dropdown-size',
28+
options=[{'label': i,
29+
'value': j} for i, j in [('L','large'), ('M','medium'), ('S','small')]],
30+
value='medium'
3131
),
32-
html.Div(id='output-b')
32+
html.Div(id='output-size')
3333

3434
])
3535

3636
@app.callback(
37-
dash.dependencies.Output('output-a', 'children'),
38-
[dash.dependencies.Input('dropdown-a', 'value')])
39-
def callback_a(dropdown_value):
40-
return 'You\'ve selected "{}"'.format(dropdown_value)
41-
37+
dash.dependencies.Output('output-color', 'children'),
38+
[dash.dependencies.Input('dropdown-color', 'value')])
39+
def callback_color(dropdown_value):
40+
return "The selected color is %s." % dropdown_value
4241

4342
@app.callback(
44-
dash.dependencies.Output('output-b', 'children'),
45-
[dash.dependencies.Input('dropdown-a', 'value'),
46-
dash.dependencies.Input('dropdown-b', 'value')])
47-
def callback_b(dropdown_value,other_dd):
48-
return 'You\'ve selected "{}" and "{}"'.format(dropdown_value,
49-
other_dd)
43+
dash.dependencies.Output('output-size', 'children'),
44+
[dash.dependencies.Input('dropdown-color', 'value'),
45+
dash.dependencies.Input('dropdown-size', 'value')])
46+
def callback_size(dropdown_color, dropdown_size):
47+
return "The chosen T-shirt is a %s %s one." %(dropdown_size,
48+
dropdown_color)
5049

5150
Note that the ``DelayedDash`` constructor requires a name to be specified. This name is then used to identify the dash app in
5251
templates:::

0 commit comments

Comments
 (0)