|
| 1 | +.. _demo_notes: |
| 2 | + |
| 3 | +Demonstration application |
| 4 | +========================= |
| 5 | + |
| 6 | +There are a number of pages in the demo application in the |
| 7 | +source repository. |
| 8 | + |
| 9 | +#. Direct insertion of one or more dash applications |
| 10 | +#. Initial state storage within Django |
| 11 | +#. Enhanced callbacks |
| 12 | +#. Live updating |
| 13 | +#. Injection without using an iframe |
| 14 | +#. Simple html injection |
| 15 | +#. Bootstrap components |
| 16 | +#. Session state storage |
| 17 | + |
| 18 | +The templates that drive each of these can be found in |
| 19 | +the `github repository <https://github.com/GibbsConsulting/django-plotly-dash/tree/master/demo/demo/templates>`_. |
| 20 | + |
| 21 | +There is a more details walkthrough of the :ref:`session state storage <session_example>` example. This example also |
| 22 | +shows the use of `dash bootstrap components <https://pypi.org/project/dash-bootstrap-components/>`_. |
| 23 | + |
| 24 | +.. _session_example: |
| 25 | +Session state example walkthrough |
| 26 | +--------------------------------- |
| 27 | + |
| 28 | +The session state example has three separate components in the demo application |
| 29 | + |
| 30 | +* A template to render the application |
| 31 | +* The ``django-plotly-dash`` application itself |
| 32 | +* A view to render the template having initialised the session state if needed |
| 33 | + |
| 34 | +The first of these is a standard Django template, containing instructions to |
| 35 | +render the Dash application:: |
| 36 | + |
| 37 | + {%load plotly-dash%} |
| 38 | + |
| 39 | + ... |
| 40 | + |
| 41 | + <div class="{%plotly_class name="DjangoSessionState"%}"> |
| 42 | + {%plotly_app name="DjangoSessionState" ratio=0.3 %} |
| 43 | + </div> |
| 44 | + |
| 45 | +The view sets up the initial state of the application prior to rendering. For this example |
| 46 | +we have a simple variant of rendering a template view:: |
| 47 | + |
| 48 | + def session_state_view(request, template_name, **kwargs): |
| 49 | + |
| 50 | + # Set up a context dict here |
| 51 | + context = { ... values for template go here, see below ... } |
| 52 | + |
| 53 | + return render(request, template_name=template_name, context=context) |
| 54 | + |
| 55 | +and it suffices to register this view at a convenient URL as it does not |
| 56 | +use any parameters:: |
| 57 | + |
| 58 | + ... |
| 59 | + url('^demo-eight', |
| 60 | + session_state_view, |
| 61 | + {'template_name':'demo_eight.html'}, |
| 62 | + name="demo-eight"), |
| 63 | + ... |
| 64 | + |
| 65 | +In passing, we note that accepting parameters as part of the URL and passing them as initial |
| 66 | +parameters to the app through the template is a straightforward extension of this example. |
| 67 | + |
| 68 | +The session state can be accessed in the app as well as the view. The app is essentially formed |
| 69 | +from a layout function and a number of callbacks. In this particular example, |
| 70 | +`dash-bootstrap-components <https://dash-bootstrap-components.opensource.asidatascience.com/>`_ |
| 71 | +are used to form the layout:: |
| 72 | + |
| 73 | + dis = DjangoDash("DjangoSessionState", |
| 74 | + add_bootstrap_links=True) |
| 75 | + |
| 76 | + dis.layout = html.Div( |
| 77 | + [ |
| 78 | + dbc.Alert("This is an alert", id="base-alert", color="primary"), |
| 79 | + dbc.Alert(children="Danger", id="danger-alert", color="danger"), |
| 80 | + dbc.Button("Update session state", id="update-button", color="warning"), |
| 81 | + ] |
| 82 | + ) |
| 83 | + |
| 84 | +Within the :ref:`expanded callback <extended_callbacks>`, the session state is passed as an extra |
| 85 | +argument compared to the standard ``Dash`` callback:: |
| 86 | + |
| 87 | + @dis.expanded_callback( |
| 88 | + dash.dependencies.Output("danger-alert", 'children'), |
| 89 | + [dash.dependencies.Input('update-button', 'n_clicks'),] |
| 90 | + ) |
| 91 | + def session_demo_danger_callback(n_clicks, session_state=None, **kwargs): |
| 92 | + if session_state is None: |
| 93 | + raise NotImplementedError("Cannot handle a missing session state") |
| 94 | + csf = session_state.get('bootstrap_demo_state', None) |
| 95 | + if not csf: |
| 96 | + csf = dict(clicks=0) |
| 97 | + session_state['bootstrap_demo_state'] = csf |
| 98 | + else: |
| 99 | + csf['clicks'] = n_clicks |
| 100 | + return "Button has been clicked %s times since the page was rendered" %n_clicks |
| 101 | + |
| 102 | +The session state is also set during the view:: |
| 103 | + |
| 104 | + def session_state_view(request, template_name, **kwargs): |
| 105 | + |
| 106 | + session = request.session |
| 107 | + |
| 108 | + demo_count = session.get('django_plotly_dash', {}) |
| 109 | + |
| 110 | + ind_use = demo_count.get('ind_use', 0) |
| 111 | + ind_use += 1 |
| 112 | + demo_count['ind_use'] = ind_use |
| 113 | + session['django_plotly_dash'] = demo_count |
| 114 | + |
| 115 | + # Use some of the information during template rendering |
| 116 | + context = {'ind_use' : ind_use} |
| 117 | + |
| 118 | + return render(request, template_name=template_name, context=context) |
| 119 | + |
| 120 | +Reloading the demonstration page will cause the page render count to be incremented, and the |
| 121 | +button click count to be reset. Loading the page in a different session, for example by using |
| 122 | +a different browser or machine, will have an independent render count. |
| 123 | + |
| 124 | + |
0 commit comments