This package provides client-side hot reload functionality for FastAPI applications.
pip install fast-hot-reload
Import FastHotReload and instantiate it, passing your FastAPI app instance:
from fastapi import FastAPI
from fast_hot_reload import FastHotReload
app = FastAPI()
# Basic usage
FastHotReload(app)
# If not connecting, try alternate config:
FastHotReload(app=app, use_alternate_config=True)
# For non-standard, proxies or remote dev servers
FastHotReload(
app,
ws_host_override="custom_host",
ws_port_override=8000,
)
# Note: use_alternate_config is incompatible with host/port overridesNow when you run your app with uvicorn --reload, changes will trigger an automatic browser reload.
on_reload: Callback invoked when a reload is triggereduse_alternate_config: Try this if reload is not working as expectedws_host_override: Websocket host if using a proxyws_port_override: Websocket port if using a custom port
The FastHotReload constructor accepts serveral advanced keyword arguments to further customize event language:
Here is a section on the advanced kwargs usage:
FastHotReload(
app,
# Custom messaging
ws_message_prefix="[Hot Reload]",
ws_connected_message="Connected!",
ws_reconnect_message="Reconnecting in ${reconnectInterval} seconds...",
# Performance
_ws_reconnect_interval=5, # seconds
)ws_message_prefix- Prefix for websocket messagesws_connected_message- Message on initial connectws_reconnect_message- Message on reconnect.${reconnectInterval}is dynamically replaced._ws_reconnect_interval- Interval in sec between reconnect attempts
These can be used to integrate with a non-standard dev server setup.
- Implements a FastAPI middleware
- Opens a websocket connection to the dev server
- Listens for file change events
- Triggers browser reload when changes are detected
Contributions welcome! Please open issues and/or PRs.
MIT