Skip to main content

Display a fallback in templates until its children have finished loading.

Project description

Django Suspense

PyPI version PyPI Supported Python Versions PyPI Supported Django Versions Coverage)

Django Suspense is small package to easily display a fallback in templates until children have finished loading.

Quick start

1. Install package:

To get started, install the package from pypi:

pip install django-suspense

Now you can add suspense to your django project. Change your INSTALLED_APPS setting like this:

INSTALLED_APPS = [
    ...,
    "suspense",
]

Optionally, you can specify suspense as builtins and this will be available in any of your templates without additionally specifying {% load suspense %}:

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates"],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                # ... list of default processors
            ],
            "builtins": ["suspense.templatetags.suspense"], # <--
        },
    },
]

If you choose not use it as a built-in, you will need to add {% load suspense %} to the top of your template whenever you want to use suspense.

2. Create view with slow lazy load object:

Because django executes database queries lazily, they may sometimes not work as expected. Let's try to create a very slow but lazy object and write a view function:

from suspense.shortcuts import render

# app/views.py
def view(request):
    def obj():
        import time

        time.sleep(1)
        return range(10)

    return render(request, 'template.html', {'obj': obj})

3. Use suspense in your template:

Let's now add the output of the received data to the template. At this point, we still haven't made a database query, so we can easily and quickly show the template right away.

{% load suspense %}

<ul>
    {% suspense %}
        {% fallback %}
            <li class="skeleton">Loading ... </li>
        {% endfallback %}

        {% for data in obj %}
            <li>{{ data }}</li>
        {% endfor %}
    {% endsuspense %}
</ul>

Once obj is ready for use, we will show it. But until it is ready, fallback works. While we are waiting for the data to be displayed, a request is made on the client side.

Suspense does not add additional DOM elements after rendering the final result. That's why syntactically the code above will be valid.

4. Hooray! Everything is ready to use it.

Troubleshooting

Safari delay in rendering

On Safari if your webpage is very light/simple, you may experience a delay in rendering.

Ex: the page renders only after some django-suspense or all content is downloaded.

WebKit has an issue with streaming responses requiring a certain amount of visible content before to actually start rendering.

See webkit issue #252413

If you are experiencing this issue, you can use the additional {% webkit_extra_invisible_bytes %} template tag to add a few extra invisible bytes in Safari.

{% load suspense %}

{% webkit_extra_invisible_bytes %}

By default the webkit_extra_invisible_bytes adds 200 bytes but you can specify a different amount:

{% webkit_extra_invisible_bytes 300 %}

Content Security Policy (CSP) nonce error because of strict-dynamic

If you are using a Content Security Policy (CSP) with nonce and strict-dynamic, you may need to add the nonce attribute to the script tag.

You can override the suspense/replacer.html template and add the nonce attribute to the script tag.

With django-csp:

{% extends "suspense/replacer.html" %}

{% block script_attributes %}nonce="{{request.csp_nonce}}"{% endblock %}

ASGI support

Async views with an ASGI server is also supported.

import asyncio

from suspense.shortcuts import async_render

# app/views.py
async def view(request):
    async def obj():

        await asyncio.sleep(1)
        return range(10)

    return async_render(request, 'template.html', {'obj': obj()})

Suspense will wait for any awaitable object to finish before rendering the suspense tags.

Specify which awaitable to wait for

If you have multiple suspense blocks with different awaitable, you can specify which awaitable to wait for or each suspense block will await everything.

Ex: {% suspense obj obj2 %}

{% load suspense %}

<ul>
    {% suspense obj %}
        {% fallback %}
            <li class="skeleton">Loading ... </li>
        {% endfallback %}

        {% for data in obj %}
            <li>{{ data }}</li>
        {% endfor %}
    {% endsuspense %}

    {% suspense obj2 %}
        {% fallback %}
            <li class="skeleton">Loading 2... </li>
        {% endfallback %}

        {% for data in obj2 %}
            <li>{{ data }}</li>
        {% endfor %}
    {% endsuspense %}
</ul>

Important: If your async context variable is used by more than one suspense block, or you did not specify any variables on the tags, make sure to wrap your coroutines in tasks so they can be awaited multiple times.

Ex: asyncio.create_task(obj())

import asyncio

from suspense.shortcuts import async_render


# app/views.py
async def view(request):
    async def obj():
        await asyncio.sleep(1)
        return range(10)

    task_obj = asyncio.create_task(obj())
    return async_render(request, 'template.html', {'obj': task_obj})

ASGI notes

  • synchronous streaming response with AGSI will wait for the full render before sending the response to the client.

Contributing

If you would like to suggest a new feature, you can create an issue on the GitHub repository for this project. Also you can fork the repository and submit a pull request with your changes.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_suspense-1.2.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_suspense-1.2.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file django_suspense-1.2.0.tar.gz.

File metadata

  • Download URL: django_suspense-1.2.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for django_suspense-1.2.0.tar.gz
Algorithm Hash digest
SHA256 3fcafe3d56732a8ace4a1ad273c70a64c45cbe65f6138f059be820cf205b7d20
MD5 b67b514ba144680a79835661e9c887f2
BLAKE2b-256 d5a7efc7b9f9c09dc2aeac8344f11a913b6e6c159851b0b8c0411f30148988f4

See more details on using hashes here.

File details

Details for the file django_suspense-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_suspense-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aece82b88344823e785a8f69a0dd714fd2b2462ac32b8c1462947903d137ba5f
MD5 4ce7ca4f5069dc3029a8b504a70959fb
BLAKE2b-256 0297dc6fb8022378bfc0423f763b56afea2691cd7a9d4d04e816f4f00e106c84

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page