Skip to content

Commit ceca5ae

Browse files
author
Mark Gibbs
committed
More documentation for live updating
1 parent 234a027 commit ceca5ae

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

docs/installation.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
Installation
44
============
55

6-
The package requires version 2.0 or greater of Django, essentially due to the use of the ``path`` function for
7-
registering routes. The minimum Python version needed is 3.5.
6+
The package requires version 2.0 or greater of Django, and a minimum Python version needed of 3.5.
87

98
Use ``pip`` to install the package, preferably to a local ``virtualenv``::
109

docs/introduction.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ above for stateless applications.
3131
Also, an enhanced version of the ``Dash`` callback is provided, giving the callback access to the current User, the current session, and also
3232
the model instance associated with the application's internal state.
3333

34-
This package is compatible with version 2.0 onwards of Django, as it uses the new path registration functionality.
34+
This package is compatible with version 2.0 onwards of Django. Use of the :ref:`live updating <updating>` feature requires
35+
the Django Channels extension; in turn this requires a suitable messaging backend such as Redis.
36+

docs/updating.rst

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,71 @@
33
Live updating
44
=============
55

6-
Live updating blah.
6+
Live updating is supported using additional ``Dash`` :ref:`components <dash_components>` and
7+
leveraging `Django Channels <https://channels.readthedocs.io/en/latest/>`_ to provide websocket endpoints.
8+
9+
Server-initiated messages are sent to all interested clients. The content of the message is then injected into
10+
the application from the client, and from that point it is handled like any other value passed to a callback function.
11+
The messages are constrained to be JSON serialisable, as that is how they are transmitted to and from the clients, and should
12+
also be as small as possible given that they travel from the server, to each interested client, and then back to the
13+
server again as an argument to one or more callback functions.
14+
15+
Live updating requires a server setup that is considerably more
16+
complex than the alternative, namely use of the built-in `Interval <https://dash.plot.ly/live-updates>`_ component. However, live
17+
updating can be used to reduce server load (as callbacks are only made when needed) and application latency (as callbacks are
18+
invoked as needed, not on the tempo of the Interval component).
719

820
Message channels
921
----------------
1022

1123
Messages are passed through named channels, and each message consists
12-
of a ``label`` and ``value`` pair.
24+
of a ``label`` and ``value`` pair. A :ref:`Pipe <pipe_component>` component is provided that listens for messages and makes
25+
them available to ``Dash`` callbacks. Each message is sent through a message channel to all ``Pipe`` components that have
26+
registered their interest in that channel, and in turn the components will select messages by ``label``.
27+
28+
A message channel exists as soon as a component signals that it is listening for messages on it. The
29+
message delivery requirement is 'hopefully at least once'. In other words, applications should be robust against both the failure
30+
of a message to be delivered, and also for a message to be delivered multiple times. A design approach that has messages
31+
of the form 'you should look at X and see if something should be done' is strongly encouraged. The accompanying demo has
32+
messages of the form 'button X at time T', for example.
33+
34+
Sending messages from within Django
35+
-----------------------------------
36+
37+
Messages can be easily sent from within Django, provided that they are within the ASGI server.
1338

14-
Pipes
15-
-----
39+
.. code-block:: python
1640
17-
A :ref:`Pipe <pipe_component>` component is provided.
41+
from django_plotly_dash.consumers import send_to_pipe_channel
42+
43+
# Send a message
44+
#
45+
# This function may return *before* the message has been sent
46+
# to the pipe channel.
47+
#
48+
send_to_pipe_channel(channel_name="live_button_counter",
49+
label="named_counts",
50+
value=value)
51+
52+
# Send a message asynchronously
53+
#
54+
await async_send_to_pipe_channel(channel_name="live_button_counter",
55+
label="named_counts",
56+
value=value)
57+
58+
In general, making assumptions about the ordering of code between message sending and receiving is
59+
unsafe. The ``send_to_pipe`` function uses the Django Channels ``async_to_sync`` wrapper around
60+
a call to ``async_send_to_pipe`` and therefore may return before the asynchronous call is made (perhaps
61+
on a different thread). Furthermore, the transit of the message through the channels backend
62+
introduces another indeterminacy.
1863

1964
HTTP Endpoint
2065
-------------
2166

2267
There is an HTTP endpoint that allows direct insertion of messages into a message channel.
2368

69+
Deployment
70+
----------
71+
72+
Need redis and daphne.
73+

0 commit comments

Comments
 (0)