ASGI middleware that auto-reloads the browser on file changes
Project description
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 anEventSource('/_autoreload')connection is injected before</body>in everytext/htmlresponse. Compressed responses (Content-Encoding: gzip, etc.) are left untouched. - SSE endpoint —
/_autoreloadis handled by the middleware itself and streamsreloadevents to connected browsers. - File watcher —
watchfilesmonitors 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.startupand stops cleanly onlifespan.shutdown. If lifespan is not supported by the server, it starts lazily on the first request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asgi_autoreload-0.1.1.tar.gz.
File metadata
- Download URL: asgi_autoreload-0.1.1.tar.gz
- Upload date:
- Size: 57.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.4.28
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10c8800a7d486ad36c0b716ce01ce6055028206575911eee6c6da67e41ebb4ec
|
|
| MD5 |
5c56871b47b3af0dd26fbfdd4ac3862b
|
|
| BLAKE2b-256 |
10e2b87b0426a17f91813e1f6ecaf2d44caa2fc2214633cf9331d87935894c44
|
File details
Details for the file asgi_autoreload-0.1.1-py3-none-any.whl.
File metadata
- Download URL: asgi_autoreload-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.4.28
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68d487a18ddd230e17a0ee36a634a3cae0d6dd31fce51d9f9967cac724944f36
|
|
| MD5 |
cb7ecae27df0fc4b1af7c664ae2db137
|
|
| BLAKE2b-256 |
628c6fc61df1ca220848b494fb50fe778b9d5d6155afcbe87f7ad8a5aa06d774
|