Python LiveReload server that automatically reloads web pages when source files change
—
A Python implementation of LiveReload functionality that automatically reloads web pages when source files change. It provides a tornado-based development server with file watching capabilities, enabling rapid web development workflows without manual browser refreshing.
pip install livereloadfrom livereload import Server, shellAlternative server-only import:
from livereload import ServerAdvanced imports for custom handlers:
from livereload.handlers import LiveReloadHandler, StaticFileHandler
from livereload.watcher import get_watcher_class, Watcherfrom livereload import Server
# Create a server instance
server = Server()
# Watch current directory for changes
server.watch('.')
# Start serving on default port 5500
server.serve()from livereload import Server, shell
server = Server()
# Watch LESS files and compile them when changed
server.watch('styles/*.less', shell('lessc styles/main.less styles/main.css'))
# Watch Python files and restart server
server.watch('*.py')
# Serve with custom configuration
server.serve(port=8000, host='0.0.0.0', open_url_delay=1)# Serve current directory with live reload
livereload
# Serve specific directory on custom port
livereload --port 8080 /path/to/project
# Watch specific files with custom delay
livereload --target "*.css" --wait 0.5LiveReload consists of several key components:
Core server functionality including HTTP serving, WebSocket communication, and development server features with support for custom headers, debug modes, and automatic browser opening.
class Server:
def __init__(self, app=None, watcher=None): ...
def serve(self, port=5500, liveport=None, host=None, root=None, debug=None,
open_url=False, restart_delay=2, open_url_delay=None,
live_css=True, default_filename='index.html'): ...
def watch(self, filepath, func=None, delay=None, ignore=None): ...
def setHeader(self, name, value): ...Cross-platform file system monitoring with support for files, directories, and glob patterns. Includes polling-based fallback and Linux inotify optimization for efficient change detection.
class Watcher:
def __init__(self): ...
def watch(self, path, func=None, delay=0, ignore=None): ...
def is_changed(self, path, ignore=None): ...
def start(self, callback): ...
def get_watcher_class(): ...Execute shell commands as file change callbacks with support for build tools, preprocessors, and custom development workflows.
def shell(cmd, output=None, mode='w', cwd=None, shell=False): ...Command-line tool for quick development server setup and Django framework integration for seamless development workflow.
def main(argv=None): ...
class Command(BaseCommand):
def handle(self, *args, **options): ...Common parameter types used throughout the LiveReload API:
# File change callback function
def callback_function():
"""Callback function invoked when file changes are detected."""
pass
# Path specifications (files, directories, or glob patterns)
path_spec = "file.py" # Single file
path_spec = "directory/" # Directory
path_spec = "**/*.css" # Glob pattern
# Ignore function for filtering files
def ignore_function(filename):
"""
Custom function to determine which files should be ignored.
Args:
filename (str): File name to check
Returns:
bool: True if file should be ignored, False otherwise
"""
return filename.endswith('.tmp')Install with Tessl CLI
npx tessl i tessl/pypi-livereload