Skip to main content

Browser hot reload for Python ASGI web apps

Project description

arel

Build Status Coverage Python versions Package version

Browser hot reload for Python ASGI web apps.

Overview

What is this for?

arel can be used to implement development-only hot-reload for non-Python files that are not read from disk on each request. This may include HTML templates, GraphQL schemas, cached rendered Markdown content, etc.

How does it work?

arel watches changes over a set of files. When a file changes, arel notifies the browser (using WebSocket), and an injected client script triggers a page reload. You can register your own reload hooks for any extra server-side operations, such as reloading cached content or re-initializing other server-side resources.

Installation

This is project in early alpha status. Be sure to pin your dependencies.

pip install 'arel==0.1.*'

Quickstart

For a working example using Starlette, see the Example section.

Although the exact instructions to set up hot reload with arel depend on the specifics of your ASGI framework, there are three general steps to follow:

  1. Create an HotReload instance, passing one or more directories of files to watch, and optionally a list of callbacks to call before a reload is triggered:

    import arel
    
    async def reload_data():
        print("Reloading server data...")
    
    hotreload = arel.HotReload(
        paths=[
            arel.Path("./server/data", on_reload=[reload_data]),
            arel.Path("./server/static"),
        ],
    )
    
  2. Mount the hot reload endpoint, and register its startup and shutdown event handlers. If using Starlette, this can be done like this:

    from starlette.applications import Starlette
    from starlette.routing import WebSocketRoute
    
    app = Starlette(
        routes=[WebSocketRoute("/hot-reload", hotreload, name="hot-reload")],
        on_startup=[hotreload.startup],
        on_shutdown=[hotreload.shutdown],
    )
    
  3. Add the JavaScript code to your website HTML. If using Starlette with Jinja templates, you can do this by updating the global environment, then injecting the script into your base template:

    templates.env.globals["DEBUG"] = os.getenv("DEBUG")  # Development flag.
    templates.env.globals["hotreload"] = hotreload
    
    <body>
      <!-- Page content... -->
    
      <!-- Hot reload script -->
      {% if DEBUG %}
        {{ hotreload.script(url_for('hot-reload')) | safe }}
      {% endif %}
    </body>
    

Example

The example directory contains an example Markdown-powered website that uses arel to refresh the browser when Markdown content or HTML templates change.

License

MIT

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog.

0.2.0 - 2020-07-08

Added

  • Add support for watching multiple directories, each with its own reload callbacks. (Pull #15)

Changed

  • arel.HotReload("./directory", on_reload=[...]) should now be written as arel.HotReload(paths=[arel.Path("./directory", on_reload=[...])]). (Pull #15)

0.1.0 - 2020-04-11

Initial release.

Added

  • Add HotReload ASGI application class. (Pull #1)

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

arel-0.2.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

arel-0.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file arel-0.2.0.tar.gz.

File metadata

  • Download URL: arel-0.2.0.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for arel-0.2.0.tar.gz
Algorithm Hash digest
SHA256 754dbd92968067cb5d1598b70159a881d917891ce8f561edd47db7ca4042cd73
MD5 a7bc4b0531583dad8457bad71367295d
BLAKE2b-256 a38c1d7316bf76b312107617604cbccb1514dc7ecc158064384e67687c2ddadc

See more details on using hashes here.

File details

Details for the file arel-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: arel-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for arel-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7875bfafab61dfdc4c338a426ff6d0a2755343278fce18242cad8f07baac2a1f
MD5 e5869730b8e8f24d4004264f4d037ad2
BLAKE2b-256 68b91cd651c2e3d73d2533d58e4954da652aff9ecc90eb5a7cedb029121befe3

See more details on using hashes here.

Supported by

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