|
3 | 3 | Expose [plotly dash](https://plot.ly/products/dash/) apps as django tags. |
4 | 4 |
|
5 | 5 | See the source for this project here: |
6 | | -<https://github.com/GibbsConsulting/django-plotly-dash>. |
| 6 | +<https://github.com/GibbsConsulting/django-plotly-dash> |
| 7 | + |
| 8 | +Online documentation can be found here: |
| 9 | +<https://readthedocs.org/projects/django-plotly-dash> |
7 | 10 |
|
8 | 11 | ## Installation |
9 | 12 |
|
10 | | -To use existing dash applications, just register thenm |
| 13 | +First, install the package. This will also install plotly and some dash packages if they are not already present. |
| 14 | + |
| 15 | + pip install django_plotly_dash |
| 16 | + |
| 17 | +Then, just add `django_plotly_dash` to `INSTALLED_APPS` in your Django `settings.py` file |
| 18 | + |
| 19 | + INSTALLED_APPS = [ |
| 20 | + ... |
| 21 | + 'django_plotly_dash', |
| 22 | + ... |
| 23 | + ] |
| 24 | + |
| 25 | +## Demonstration |
| 26 | + |
| 27 | +The source repository contains a demo application. To clone the repo and lauch the demo: |
| 28 | + |
| 29 | +```bash |
| 30 | +git clone https://github.com/GibbsConsulting/django-plotly-dash.git |
| 31 | + |
| 32 | +cd django-plotly-dash |
| 33 | + |
| 34 | +./make_env # sets up a virtual environment for development |
| 35 | + # with direct use of the source code for the package |
| 36 | + |
| 37 | +./prepare_demo # prepares and launches the demo |
| 38 | + # using the Django debug server at http://localhost:8000 |
| 39 | +``` |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +To use existing dash applications, first register them using the `DelayedDash` class. This |
| 44 | +replaces the `dash.Dash` class of `plotly.py.` |
| 45 | + |
| 46 | +Taking as an example a slightly modified variant of one of the [getting started](https://dash.plot.ly/getting-started-part-2) examples: |
| 47 | + |
| 48 | +```python |
| 49 | +import dash |
| 50 | +import dash_core_components as dcc |
| 51 | +import dash_html_components as html |
| 52 | + |
| 53 | +from django_plotly_dash import DelayedDash |
| 54 | + |
| 55 | +app = DelayedDash('SimpleExample') # replaces dash.Dash |
| 56 | + |
| 57 | +app.layout = html.Div([ |
| 58 | + dcc.RadioItems( |
| 59 | + id='dropdown-a', |
| 60 | + options=[{'label': i, 'value': i} for i in ['Canada', 'USA', 'Mexico']], |
| 61 | + value='Canada' |
| 62 | + ), |
| 63 | + html.Div(id='output-a'), |
| 64 | + |
| 65 | + dcc.RadioItems( |
| 66 | + id='dropdown-b', |
| 67 | + options=[{'label': i, 'value': i} for i in ['MTL', 'NYC', 'SF']], |
| 68 | + value='MTL' |
| 69 | + ), |
| 70 | + html.Div(id='output-b') |
| 71 | + |
| 72 | +]) |
| 73 | + |
| 74 | +@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 | + |
| 80 | + |
| 81 | +@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) |
| 88 | +``` |
| 89 | + |
| 90 | +Note that the `DelayedDash` constructor requires a name to be specified. This name is then used to identify the dash app in |
| 91 | +templates: |
| 92 | + |
| 93 | +```jinja2 |
| 94 | +{% load plotly_dash %} |
| 95 | +
|
| 96 | +{% plotly_item "SimpleExample" %} |
| 97 | +``` |
| 98 | + |
| 99 | +Note that the registration code needs to be in a location |
| 100 | +that will be imported into the Django process before any template tag attempts to use it. The example Django application |
| 101 | +in the demo subdirectory achieves this through an import in the main urls.py file; any views.py would also be sufficient. |
| 102 | + |
0 commit comments