|
2 | 2 |
|
3 | 3 | Django models and application state |
4 | 4 | ================ |
| 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``. |
0 commit comments