Home

Awesome

Reactor, a LiveView library for Django

Reactor enables you to do something similar to Phoenix framework LiveView using Django Channels.

TODO MVC demo app

What's in the box?

This is no replacement for VueJS or ReactJS, or any JavaScript but it will allow you use all the potential of Django to create interactive front-ends. This method has its drawbacks because if connection is lost to the server the components in the front-end go busted until connection is re-established. But also has some advantages, as everything is server side rendered the interface comes already with meaningful information in the first request response, you can use all the power of Django template and ORM directly in your component and update the interface in real-time by subscribing to events on the server. If connection is lost or a component crashes, the front-end will have enough information to rebuild their state in the last good known state and continue to operate with the connection is restored.

Installation and setup

Reactor requires Python >=3.9.

Install reactor:

pip install django-reactor

Reactor makes use of django-channels, by default this one uses an InMemory channel layer which is not capable of a real broadcasting, so you might wanna use the Redis one, take a look here: Channel Layers

Add reactor and channels to your INSTALLED_APPS before the Django applications so channels can override the runserver command.

INSTALLED_APPS = [
    'reactor',
    'channels',
    ...
]

...

ASGI_APPLICATION = 'project_name.asgi.application'

and modify your project_name/asgi.py file like:

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')

import django
django.setup()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from reactor.urls import websocket_urlpatterns

application = ProtocolTypeRouter({
    'http': get_asgi_application(),
    'websocket': AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
})

Note: Reactor since version 2, autoloads any live.py file in your applications with the hope to find there Reactor Components so they get registered and can be instantiated.

In the templates where you want to use reactive components you have to load the reactor static files. So do something like this so the right JavaScript gets loaded:

{% load reactor %}
<!doctype html>
<html>
    <head>
        ... {% reactor_header %}
    </head>
    ...
</html>

Don't worry if you put this as early as possible, the scripts are loaded using <script defer> so they will be downloaded in parallel with the html, and when all is loaded they are executed.

Simple example of a counter

In your app create a template x-counter.html:

{% load reactor %}
<div {% tag_header %}>
  {{ amount }}
  <button {% on 'click' 'inc' %}>+</button>
  <button {% on 'click' 'dec' %}>-</button>
  <button {% on 'click' 'set_to' amount=0 %}>reset</button>
</div>

Anatomy of a template: each component should be a single root tag and you need to add {% tag_header %} to that tag, so a set of attributes that describe the component state are added to that tag.

Each component should have an id so the backend knows which instance is this one and a state attribute with the necessary information to recreate the full state of the component on first render and in case of re-connection to the back-end.

Render things as usually, so you can use full Django template language, trans, if, for and so on. Just keep in mind that the instance of the component is referred as this.

Forwarding events to the back-end: Notice that for event binding in-line JavaScript is used on the event handler of the HTML elements. How does this work? When the increment button receives a click event send(this, 'inc') is called, send is a reactor function that will look for the parent custom component and will dispatch to it the inc message, or the set_to message and its parameters {amount: 0}. The custom element then will send this message to the back-end, where the state of the component will change and then will be re-rendered back to the front-end. In the front-end morphdom (just like in Phoenix LiveView) is used to apply the new HTML.

Now let's write the behavior part of the component in live.py:

from reactor.component import Component


class XCounter(Component):
    _template_name = 'x-counter.html'

    amount: int = 0

    async def inc(self):
        self.amount += 1

    async def dec(self):
        self.amount -= 1

    async def set_to(self, amount: int):
        self.amount = amount

Let's now render this counter, expose a normal view that renders HTML, like:

def index(request):
    return render(request, 'index.html')

And the index template being:

{% load reactor %}
<!doctype html>
<html>
    <head>
        .... {% reactor_header %}
    </head>
    <body>
        {% component 'XCounter' %}

        <!-- or passing an initial state -->
        {% component 'XCounter' amount=100 %}
    </body>
</html>

Don't forget to update your urls.py to call the index view.

Persisting the state of the Counter in the URL as a GET parameter

Add:

...

class SearchList(Component):
  query: str = ""

  @classmethod
  def new(cls, reactor: ReactorMeta, **kwargs):
    # read the query parameter and initialize the object with that parameter
    kwargs.setdefault("query", reactor.params.get("query", ""))
    return cls(reactor=reactor, **kwargs)

  async def filter_results(self, query: str):
    self.query = query
    # update the query string in the browser
    self.reactor.params["query"] = query
...

This will make that everytime that method gets called the query string on the browser will get updated to include "query=blahblah". Never replace the self.reactor.params, mutate it instead.

Here is another example, suppose you have a list of items that can be expanded, like nodes in a tree view:

class Node(Component):
    name: str
    expanded: bool = False

    @classmethod
    def new(cls, reactor: ReactorMeta, id: str, **kwargs):
        kwargs["expanded"] = id in reactor.params.get("expanded.json", [])
        return cls(reactor=reactor, id=id, **kwargs)

    async def toggle_expanded(self):
        self.expanded = not self.expanded
        expanded = self.reactor.params.setdefault("expanded.json", [])
        if self.expanded:
            expanded.append(self.id)
        elif self.id in expanded:
            expanded.remove(self.id)

Here expanded.json is a list of the expanded nodes. Notice the .json this indicates that the value of this key should be encoded/decoded to/from JSON, so just put there JSON serializable stuff.

Settings:

Default settings of reactor are:


from reactor.schemas import AutoBroadcast

REACTOR = {
    "TRANSPILER_CACHE_SIZE": 1024,
    "USE_HTML_DIFF": True,
    "USE_HMIN": False,
    "BOOST_PAGES": False,
    "TRANSPILER_CACHE_NAME": "reactor:transpiler",
    "AUTO_BROADCAST": AutoBroadcast(
        # model-a
        model: bool = False
        # model-a.1234
        model_pk: bool = False
        # model-b.9876.model-a-set
        related: bool = False
        # model-b.9876.model-a-set
        # model-a.1234.model-b-set
        m2m: bool = False
        # this is a set of tuples of ('app_label', 'ModelName')
        # to subscribe for the auto broadcast
        senders: set[tuple[str, str]] = Field(default_factory=set)
    ),
}

Back-end APIs

Template tags and filters of reactor library

Component live cycle

Initialization & Rendering

This happens when in a "normal" template you include a component.

{% component 'Component' param1=1 param2=2 %}

This passes those parameter there to Component.new that should return the component instance and then the component get's rendered in the template and is sent to the client.

Joins

When the component arrives to the front-end it "joins" the backend. Sends it's serialized state to the backend which rebuilds the component and calls Component.joined.

After that the component is rendered and the render is sent to the front-end. Why? Because could be that the client was online while some change in the backend happened and the component needs to be updated.

User events

When a component or its parent has joined it can send user events to the client. Using the on template tag, this events are sent to the backend and then the componet is rendered again.

Subscriptions

Every time a component joins or responds to an event the Componet._subscriptions set is reviewed to check if the component subscribes or not to some channel.

Disconnection

If the component is destroyed using the Component.destroy or just desapears from the front-end it is removed from the backend. If the the websocket closes all components in that connection are removed from the backend and the state of those componets stay just in the front-end in the seralized form awaiting for the front-end to join again.

Component API

Each component is a Pydantic model so it can serialize itself. I would advice not to mess with the __init__ method. Instead use the class method new to create the instance.

Rendering

Subscriptions

Actions

Front-end APIs

Event binding in the front-end

Look at this:

  <button {% on "click.prevent" "submit" %}>Submit</button>

Syntax: {% on <event-and-modifiers> <event-handler> [<event-handler-arguments-as-kwargs>] %} The format for event and modifiers is @<event>[.modifier1][.modifier2][.modifier2-argument1][.modifier2-argument2]

Examples:

Misc:

Event arguments

Reactor sends the implicit arguments you pass on the on template tag, but also sends implicit arguments. The implicit arguments are taken from the form the element handling the event is in or from the whole component otherwise.

Examples:

Here any event inside that component will have the implicit argument x being send to the backend.

<div {% tag-header %}>
  <input name="x"/>
  <button {% on "click" "submit" %}>Send</button>
</div>

Here any submit_x will send x, and submit_y will send just y.

<div {% tag-header %}>
  <input name="x"/>
  <button {% on "click" "submit_x" %}>Send</button>
  <form>
    <input name="y"/>
    <button {% on "click.prevent" "submit_y" %}>Send</button>
  </form>
</div>

Event handlers in the back-end

Given:

<button {% on 'click 'inc' amount=2 %}>Increment</button>

You will need an event handler in that component in the back-end:

async def inc(self, amount: int):
    ...

It is good if you annotate the signature so the types are validated and converted if they have to be.

More complex components

I made a TODO list app using models that signals from the model to the respective channels to update the interface when something gets created, modified or deleted.

This example contains nested components and some more complex interactions than a simple counter, the app is in the /tests/ directory.

Development & Contributing

Clone the repo and create a virtualenv or any other contained environment, get inside the repo directory, build the development environment and the run tests.

git clone git@github.com:edelvalle/reactor.git
cd reactor
make install
make test

If you want to run the included Django project used for testing do:

make
cd tests
python manage.py runserver

Enjoy!