Skip to main content

asgi-autoreload

Development middleware for Python ASGI frameworks that automatically reloads the browser when files change.

It works by injecting a small SSE client script into every HTML response. When a watched file changes, the server pushes a reload event and the browser refreshes — no manual F5 needed.

Compatible with Django, FastAPI, Starlette, and Litestar.


Installation

# Django (via Daphne)
pip install asgi-autoreload[django]

# FastAPI
pip install asgi-autoreload[fastapi]

# Starlette
pip install asgi-autoreload[starlette]

# Litestar
pip install asgi-autoreload[litestar]

Usage

FastAPI

Use the fastapi-autoreload CLI as a drop-in replacement for fastapi dev. It accepts all the same options, plus:

  • --watch — paths to monitor (default: .)
  • -- <command> — build command to run on startup and on each file change
fastapi-autoreload dev main.py
fastapi-autoreload dev main.py --watch src --watch templates
fastapi-autoreload dev main.py --host 0.0.0.0 --port 8080

# Run a build step before each browser reload
fastapi-autoreload dev main.py -- npm run build
fastapi-autoreload dev main.py --watch src -- npx tailwindcss -i src/input.css -o static/output.css

See examples/fastapi/ for a complete working example.

Django

Add asgi_autoreload.django to INSTALLED_APPS, then use the custom management command instead of runserver:

# settings.py
INSTALLED_APPS = [
    ...
    "asgi_autoreload.django",
]
python manage.py autoreload_runserver
python manage.py autoreload_runserver --watch src --watch templates

# Run a build step before each browser reload
python manage.py autoreload_runserver -- npm run build
python manage.py autoreload_runserver --watch src -- npx tailwindcss -i src/input.css -o static/output.css

This wraps Daphne's ASGI server with the autoreload middleware. Make sure your project is configured with an ASGI application (ASGI_APPLICATION in settings).

See examples/django/ for a complete working example.

Starlette

Wrap your application manually with AutoreloadMiddleware:

from starlette.applications import Starlette
from asgi_autoreload import AutoreloadMiddleware

app = Starlette(...)
app = AutoreloadMiddleware(
    app,
    watch_paths=["src", "templates"],
    build_command=["npm", "run", "build"],  # optional
)
uvicorn main:app

Litestar

Same approach as Starlette — wrap the app directly:

from litestar import Litestar
from asgi_autoreload import AutoreloadMiddleware

app = Litestar([...])
app = AutoreloadMiddleware(
    app,
    watch_paths=["src", "templates"],
    build_command=["npm", "run", "build"],  # optional
)
uvicorn main:app

See examples/litestar/ for a complete working example.


How it works

  • HTML injection — a <script> tag opening an EventSource('/_autoreload') connection is injected before </body> in every text/html response. Compressed responses (Content-Encoding: gzip, etc.) are left untouched.
  • SSE endpoint — /_autoreload is handled by the middleware itself and streams reload events to connected browsers.
  • File watcher — watchfiles monitors the specified paths and triggers a broadcast on any change.
  • Build command — if provided, the command runs once on startup and again before each browser reload, so assets are always up to date when the page refreshes.
  • ASGI lifecycle — the watcher starts on lifespan.startup and stops cleanly on lifespan.shutdown. If lifespan is not supported by the server, it starts lazily on the first request.

Metadata

Release files for asgi-autoreload 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asgi-autoreload 0.1.2
File Size Uploaded
asgi_autoreload-0.1.2.tar.gz 60.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asgi-autoreload 0.1.2
File Interpreter ABI Platform
asgi_autoreload-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 71.0 kB

Release files / asgi_autoreload-0.1.2.tar.gz

Download URL asgi_autoreload-0.1.2.tar.gz
Size 60.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f521d11ecf47fcb2f4167e41aa8261cc562fdc362af8050794ad6a4332bc7e22
BLAKE2b-256 checksum
How to use checksums
b202af333be01c703700891531f62d02cd38d6e67ba7ea1cf1ff7f1d4dede7c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.4.28

Release files / asgi_autoreload-0.1.2-py3-none-any.whl

Download URL asgi_autoreload-0.1.2-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0146db3026e36230b85eaf4f52442805c6d810737bc84b092ab3233106c27c59
BLAKE2b-256 checksum
How to use checksums
f107cbfb42c6599732dd12abfe057df703faafbd4a57e00081591db55525e032
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.4.28

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page