Skip to content

Commit 2c9ee6d

Browse files
author
Mark Gibbs
committed
Documentation on extended callbacks and an overview of how things work
1 parent 69d9b89 commit 2c9ee6d

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# django-plotly-dash
22

3-
Expose [plotly dash](https://plot.ly/products/dash/) apps as django tags.
3+
Expose [plotly dash](https://plot.ly/products/dash/) apps as [Django](https:://www.djangoproject.com/) tags. Multiple Dash apps can
4+
then be embedded into a single web page, persist and share internal state, and also have access to the
5+
current user and session variables.
46

57
See the source for this project here:
68
<https://github.com/GibbsConsulting/django-plotly-dash>
79

810
This README file provides a short guide to installing and using the package, and also
911
outlines how to run the demonstration application.
1012

13+
14+
1115
More detailed information
1216
can be found in the online documentation at
1317
<https://readthedocs.org/projects/django-plotly-dash>

demo/demo/plotly_apps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def callback_size(dropdown_color, dropdown_size):
3939
a2 = DjangoDash("Ex2")
4040
a2.layout = html.Div([
4141
dcc.RadioItems(id="dropdown-one",options=[{'label':i,'value':j} for i,j in [
42-
("O2","Oxygen"),("N2","Nitrogen"),]
42+
("O2","Oxygen"),("N2","Nitrogen"),("CO2","Carbon Dioxide")]
4343
],value="Oxygen"),
4444
html.Div(id="output-one")
4545
])
@@ -50,5 +50,5 @@ def callback_size(dropdown_color, dropdown_size):
5050
)
5151
def callback_c(*args,**kwargs):
5252
da = kwargs['dash_app']
53-
return "Args are %s and kwargs are %s" %("".join(*args),str(kwargs))
53+
return "Args are [%s] and kwargs are %s" %(",".join(args),str(kwargs))
5454

docs/extended_callbacks.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,47 @@ Extended callback syntax
44
========================
55

66
The ``DjangoDash`` class allows callbacks to request extra arguments when registered.
7+
8+
To do this, simply replace ``callback`` with ``expanded_callback`` when registering any callback. This will cause all of the callbacks
9+
registered with this application
10+
to receive extra ``kwargs`` in addition to the callback parameters.
11+
12+
For example, the ``plotly_apps.py`` example contains this dash application::
13+
14+
a2 = DjangoDash("Ex2")
15+
16+
a2.layout = html.Div([
17+
dcc.RadioItems(id="dropdown-one",options=[{'label':i,'value':j} for i,j in [
18+
("O2","Oxygen"),("N2","Nitrogen"),("CO2","Carbon Dioxide")]
19+
],value="Oxygen"),
20+
html.Div(id="output-one")
21+
])
22+
23+
@a2.expanded_callback(
24+
dash.dependencies.Output('output-one','children'),
25+
[dash.dependencies.Input('dropdown-one','value')]
26+
)
27+
28+
def callback_c(*args,**kwargs):
29+
da = kwargs['dash_app']
30+
return "Args are [%s] and kwargs are %s" %(",".join(args),str(kwargs))
31+
32+
The additional arguments, which are reported as the ``kwargs`` content in this example, include
33+
34+
:dash_app: For stateful applications, the ``DashApp`` model instance
35+
:dash_app_id: The application identifier. For stateless applications, this is the (slugified) name given to the ``DjangoDash`` constructor.
36+
For stateful applications, it is the (slugified) unique identifier for the associated model instance.
37+
:session_state: A dictionary of information, unique to this user session. Any changes made to its content during the
38+
callback are persisted as part of the Django session framework.
39+
:user: The Django User instance.
40+
41+
The ``DashApp`` model instance can also be configured to persist itself on any change. This is discussed
42+
in the :ref:`models_and_state` section.
43+
44+
45+
.. _using_session_state:
46+
Using session state
47+
------------------
48+
49+
Changes to the session state and other server-side objects are not automatically propagated to an application. Something in the front-end UI has to invoke a callaback; at this point the latest version of these objects will be provided to the callback. The same considerations as in other Dash `live updates <https://dash.plot.ly/live-updates>`_ apply.
50+

docs/introduction.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
Introduction
44
============
55

6-
The purpose of django-plotly-dash is to enable Plotly Dash applications to be served up as part of a Django application, in order to provide
6+
The purpose of ``django-plotly-dash`` is to enable `Plotly Dash <https://dash.plot.ly>`_ applications
7+
to be served up as part of a `Django <https://www.djangoproject.com/>`_ application, in order to provide
78
these features:
89

910
* Multiple dash applications can be used on a single page
@@ -13,3 +14,18 @@ these features:
1314

1415
There is nothing here that cannot be achieved through expanding the Flask app around Plotly Dash, or indeed by using an alternative web
1516
framework. The purpose of this project is to enable the above features, given that the choice to use Django has already been made.
17+
18+
.. _overview:
19+
Overview
20+
--------
21+
22+
``django_plotly_dash`` works by wrapping around the ``dash.Dash`` object. The http endpoints exposed by the
23+
``Dash`` application are mapped to Django ones, and an application is embedded into a webpage through the
24+
use of a template tag. Multiple ``Dash`` applications can be used in a single page.
25+
26+
A subset of the internal state of a ``Dash`` application can be persisted as a standard Django model instance, and the application with this
27+
internal state is then available at its own URL. This can then be embedded into one or more pages in the same manner as described
28+
above for stateless applications.
29+
30+
Also, an enhanced version of the ``Dash`` callback is provided, giving the callback access to the current User, the current session, and also
31+
the model instance associated with the application's internal state.

0 commit comments

Comments
 (0)