Skip to main content

Hot reload for Starlette app with smart asset updates and HTML injection

Project description

Starlette Hot Reload

starlette-hot-reload is a lightweight hot reload utility for Starlette that provides fast in-browser reloads for templates and static files.

It integrates directly with your Starlette app and provides:

  • Automatic HTML injection via middleware.
  • Server-Sent Events (SSE) based live reload (no WebSocket dependencies).
  • Smart updates, CSS changes reload without a full page refresh.
  • Automatic reconnection with exponential backoff.
  • Fully typed, following Starlette patterns.
  • Zero additional dependencies beyond Starlette.

Installation

uv add starlette-hot-reload
# or
pip install starlette-hot-reload

Example

from starlette.applications import Starlette
from starlette.routing import Route

from starlette_hot_reload import HotReload

routes = [
    # your routes
]

app = Starlette(
    debug=True,
    routes=routes,
)

# Set up hot reload (only active when debug=True)
hot_reload = HotReload(watch_dirs=["templates", "static"])
hot_reload.setup(app)

Run the application using uvicorn:

$ uvicorn main:app

How it works

starlette-hot-reload updates the browser without restarting the server.

It integrates into your app using middleware and a Server-Sent Events (SSE) endpoint.

  • HTML responses are automatically modified to include a small client script.
  • The client connects to an SSE stream exposed by the app.
  • File changes trigger reload events:
    • CSS changes update stylesheets in-place.
    • Other changes trigger a full page reload.
  • The client automatically reconnects if the connection is lost.

It complements:

  • ASGI server reload (uvicorn --reload)
  • Frontend build tools (Vite, Webpack, etc.)

Usage

Hot reload is only active when debug=True.

app = Starlette(debug=True)

You can control which directories are watched:

HotReload(watch_dirs=["templates", "static", "assets"])

You can customize the SSE endpoint path (default is /__starlette_hot_reload):

hot_reload = HotReload(
    watch_dirs=["templates", "static"],
    events_path="/custom-events-path",
)

You can also tune how often the watcher scans for changes. Lower values make reloads feel faster, at the cost of a bit more filesystem polling:

hot_reload = HotReload(
    watch_dirs=["templates", "static"],
    poll_interval=0.25,
)

Debug Logging

To enable debug logging, configure Python's logging module:

import logging
import sys

logging.basicConfig(
    level=logging.DEBUG,
    # ...
)

License

MIT

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

starlette_hot_reload-0.1.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_hot_reload-0.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: starlette_hot_reload-0.1.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for starlette_hot_reload-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4a407f70867783ad3099d82487eecd54b338eefe24aa2961dd65bc7fe770bd77
MD5 7d08016c4785120224ae48f34752392a
BLAKE2b-256 a8ff8d9e923b12117935e6182aef4e37c57ddffe3dc85fb10c3079df764aeaa3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: starlette_hot_reload-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for starlette_hot_reload-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f731f584cc7dc6f325f96a6383b63734eb970cbd5631cb89ef4f40f8994a5bac
MD5 6cb9243e15ac30583fb82f833a9df680
BLAKE2b-256 45a2221f2b261878fc419744c668261be4f8fba73abd6f7febf8ef1a296016c7

See more details on using hashes here.

Supported by

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