Skip to content

Commit f86addb

Browse files
author
Mark Gibbs
committed
Added model documentation
1 parent 2e81c52 commit f86addb

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

django_plotly_dash/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ def update_current_state(self, wid, key, value):
6969
setattr(self,'_current_state_hydrated_changed',True)
7070

7171
def current_state(self):
72+
'''
73+
Return the current internal state of the model instance.
74+
75+
This is not necessarily the same as the persisted state stored in the self.base_state variable.
76+
'''
7277
cs = getattr(self,'_current_state_hydrated',None)
7378
if not cs:
7479
cs = json.loads(self.base_state)

docs/extended_callbacks.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ To do this, simply replace ``callback`` with ``expanded_callback`` when register
99
registered with this application
1010
to receive extra ``kwargs`` in addition to the callback parameters.
1111

12-
For example, the ``plotly_apps.py`` example contains this dash application::
12+
For example, the ``plotly_apps.py`` example contains this dash application:
13+
14+
.. code-block:: python
15+
16+
import dash
17+
import dash_core_components as dcc
18+
import dash_html_components as html
19+
20+
from django_plotly_dash import DjangoDash
1321
1422
a2 = DjangoDash("Ex2")
1523

docs/models_and_state.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,52 @@
22

33
Django models and application state
44
================
5+
6+
The ``django_plotly_dash`` application defines a ``DashApp`` model. This represents an instance of application state.
7+
8+
.. code-block:: python
9+
10+
class DashApp(models.Model):
11+
'''
12+
An instance of this model represents a Dash application and its internal state
13+
'''
14+
app_name = models.CharField(max_length=100, blank=False, null=False, unique=False)
15+
instance_name = models.CharField(max_length=100, unique=True, blank=True, null=False)
16+
slug = models.SlugField(max_length=110, unique=True, blank=True)
17+
base_state = models.TextField(null=False, default="{}")
18+
creation = models.DateTimeField(auto_now_add=True)
19+
update = models.DateTimeField(auto_now=True)
20+
save_on_change = models.BooleanField(null=False,default=False)
21+
22+
... methods, mainly for managing the Dash application state ...
23+
24+
def current_state(self):
25+
'''
26+
Return the current internal state of the model instance
27+
'''
28+
29+
def update_current_state(self, wid, key, value):
30+
'''
31+
Update the current internal state, ignorning non-tracked objects
32+
'''
33+
34+
def populate_values(self):
35+
'''
36+
Add values from the underlying dash layout configuration
37+
'''
38+
39+
The ``app_name`` corresponds to an application registered through the instantiation of a ``DjangoDash`` object. The ``slug`` field provides a unique identifier
40+
that is used in URLs to identify the instance of an application, and also its associated server-side state.
41+
42+
The persisted state of the instance is contained, serialised as JSON, in the ``base_state`` variable. This is an arbitrary subset of the internal state of the
43+
object. Whenever a ``Dash`` application requests its state (through the ``<app slug>_dash-layout`` url), any values from the underlying application that are present in
44+
``base_state`` are overwritten with the persisted values.
45+
46+
The ``populate_values`` member function can be used to insert all possible initial values into ``base_state``. This functionality is also exposed in the Django
47+
admin for these model instances, as a ``Populate app`` action.
48+
49+
From callback code, the ``update_current_state`` method can be called to change the initial value of any variable tracked within the ``base_state``. Variables not tracked
50+
will be ignored. This function is automatically called for any callback argument and return value.
51+
52+
Finally, after any callback has finished, and after any result stored through ``update_current_state``, then the application model instance will be persisted by means
53+
of a call to its ``save`` method, if any changes have been detected and the ``save_on_change`` flag is ``True``.

docs/simple_use.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Simple usage
66
To use existing dash applications, first register them using the ``DjangoDash`` class. This
77
replaces the ``Dash`` class from the ``dash`` package.
88

9-
Taking a simple example inspired by the excellent `getting started <https://dash.plot.ly/getting-started-part-2>`_ guide::
9+
Taking a simple example inspired by the excellent `getting started <https://dash.plot.ly/getting-started-part-2>`_ guide:
10+
11+
.. code-block:: python
1012
1113
import dash
1214
import dash_core_components as dcc

0 commit comments

Comments
 (0)