@@ -43,7 +43,7 @@ cd django-plotly-dash
4343To use existing dash applications, first register them using the ` DelayedDash ` class. This
4444replaces 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
4949import dash
@@ -52,39 +52,52 @@ import dash_html_components as html
5252
5353from django_plotly_dash import DelayedDash
5454
55- app = DelayedDash(' SimpleExample' ) # replaces dash.Dash
55+ app = DelayedDash(' SimpleExample' )
5656
5757app.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
90103Note 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
100113that will be imported into the Django process before any template tag attempts to use it. The example Django application
101114in the demo subdirectory achieves this through an import in the main urls.py file; any views.py would also be sufficient.
102115
0 commit comments