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 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 a directory of files to watch:

    import arel
    
    hotreload = arel.HotReload("./path/to/directory")
    
  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=[hot_reaload.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')) }}
      {% endif %}
    </body>
    

Example

The example directory contains an example Markdown-powered website that uses arel to refresh the browser when Markdown content changes.

License

MIT

Changelog

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

The format is based on Keep a Changelog.

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.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

arel-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: arel-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for arel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 11fa0dbdd0a3f8af5b350b43768e8c215b9a428103af663f1c2808c1d81fccce
MD5 7f857523f5cf0ec9cce1de7f037f08a3
BLAKE2b-256 de4991058b82cb7aa375a14257217970ec2bb5f9c06f6a11d4325b2a428365fb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: arel-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for arel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 46843ece267450e8d018485cb5f73c4be3d6d9e58cf5058b6ea01d70f1c777a6
MD5 1e15d60563e5b2024cc0deecb5a90fef
BLAKE2b-256 d292f546ed95af33d9a00bc1023c9309d438ad58ec71cf64357e14be318f4619

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