Giter VIP home page Giter VIP logo

arel's Introduction

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=[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')) }}
      {% 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

arel's People

Contributors

florimondmanca avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.