|
| 1 | +'''Dash demonstration application |
| 2 | +
|
| 3 | +TODO attribution here |
| 4 | +''' |
| 5 | + |
| 6 | +# The linter doesn't like the members of the html and dcc imports (as they are dynamic?) |
| 7 | +#pylint: disable=no-member |
| 8 | + |
| 9 | +import dash |
| 10 | +import dash_core_components as dcc |
| 11 | +import dash_html_components as html |
| 12 | +import plotly.graph_objs as go |
| 13 | +#import dpd_components as dpd |
| 14 | +import numpy as np |
| 15 | +from django_plotly_dash import DjangoDash |
| 16 | + |
| 17 | +#from .urls import app_name |
| 18 | +app_name = "DPD demo application" |
| 19 | + |
| 20 | +dashboard_name1 = 'dash_example_1' |
| 21 | +dash_example1 = DjangoDash(name=dashboard_name1, |
| 22 | + serve_locally=True, |
| 23 | + app_name=app_name |
| 24 | + ) |
| 25 | + |
| 26 | +# Below is a random Dash app. |
| 27 | +# I encountered no major problems in using Dash this way. I did encounter problems but it was because |
| 28 | +# I was using e.g. Bootstrap inconsistenyly across the dash layout. Staying consistent worked fine for me. |
| 29 | +dash_example1.layout = html.Div(id='main', |
| 30 | + children=[ |
| 31 | + html.Div([dcc.Dropdown(id='my-dropdown1', |
| 32 | + options=[{'label': 'New York City', 'value': 'NYC'}, |
| 33 | + {'label': 'Montreal', 'value': 'MTL'}, |
| 34 | + {'label': 'San Francisco', 'value': 'SF'} |
| 35 | + ], |
| 36 | + value='NYC', |
| 37 | + className='col-md-12', |
| 38 | + ), |
| 39 | + html.Div(id='test-output-div') |
| 40 | + ]), |
| 41 | + |
| 42 | + dcc.Dropdown( |
| 43 | + id='my-dropdown2', |
| 44 | + options=[ |
| 45 | + {'label': 'Oranges', 'value': 'Oranges'}, |
| 46 | + {'label': 'Plums', 'value': 'Plums'}, |
| 47 | + {'label': 'Peaches', 'value': 'Peaches'} |
| 48 | + ], |
| 49 | + value='Oranges', |
| 50 | + className='col-md-12', |
| 51 | + ), |
| 52 | + |
| 53 | + html.Div(id='test-output-div2') |
| 54 | + |
| 55 | + ]) # end of 'main' |
| 56 | + |
| 57 | +@dash_example1.expanded_callback( |
| 58 | + dash.dependencies.Output('test-output-div', 'children'), |
| 59 | + [dash.dependencies.Input('my-dropdown1', 'value')]) |
| 60 | +def callback_test(*args, **kwargs): #pylint: disable=unused-argument |
| 61 | + 'Callback to generate test data on each change of the dropdown' |
| 62 | + |
| 63 | + # Creating a random Graph from a Plotly example: |
| 64 | + N = 500 |
| 65 | + random_x = np.linspace(0, 1, N) |
| 66 | + random_y = np.random.randn(N) |
| 67 | + |
| 68 | + # Create a trace |
| 69 | + trace = go.Scatter(x=random_x, |
| 70 | + y=random_y) |
| 71 | + |
| 72 | + data = [trace] |
| 73 | + |
| 74 | + layout = dict(title='', |
| 75 | + yaxis=dict(zeroline=False, title='Total Expense (£)',), |
| 76 | + xaxis=dict(zeroline=False, title='Date', tickangle=0), |
| 77 | + margin=dict(t=20, b=50, l=50, r=40), |
| 78 | + height=350, |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | + fig = dict(data=data, layout=layout) |
| 83 | + line_graph = dcc.Graph(id='line-area-graph2', figure=fig, style={'display':'inline-block', 'width':'100%', |
| 84 | + 'height':'100%;'}) |
| 85 | + children = [line_graph] |
| 86 | + |
| 87 | + return children |
| 88 | + |
| 89 | + |
| 90 | +@dash_example1.expanded_callback( |
| 91 | + dash.dependencies.Output('test-output-div2', 'children'), |
| 92 | + [dash.dependencies.Input('my-dropdown2', 'value')]) |
| 93 | +def callback_test2(*args, **kwargs): |
| 94 | + 'Callback to exercise session functionality' |
| 95 | + |
| 96 | + print(args) |
| 97 | + print(kwargs) |
| 98 | + |
| 99 | + children = [html.Div(["You have selected %s." %(args[0])]), |
| 100 | + html.Div(["The session context message is '%s'" %(kwargs['session_state']['django_to_dash_context'])])] |
| 101 | + |
| 102 | + return children |
0 commit comments