Python LiveReload server that automatically reloads web pages when source files change
—
Command-line interface and framework integrations for quick development server setup and seamless integration with existing development workflows.
Quick development server setup through the livereload command with comprehensive configuration options.
def main(argv=None):
"""
Main entry point for livereload command-line interface.
Args:
argv (list): Command line arguments (optional, uses sys.argv if None)
"""Command Syntax:
livereload [options] [directory]Available Options:
--host HOST: Hostname to run server on (default: '127.0.0.1')-p, --port PORT: Port to run server on (default: 35729)directory: Directory to serve files from (default: current directory)-t, --target TARGET: File or directory to watch for changes-w, --wait SECONDS: Time delay before reloading (default: 0.0)-o, --open-url-delay SECONDS: Auto-open browser after delay-d, --debug: Enable Tornado debug loggingUsage Examples:
# Serve current directory with defaults
livereload
# Serve specific directory
livereload /path/to/project
# Custom port and host
livereload --host 0.0.0.0 --port 8080
# Watch specific files with delay
livereload --target "*.css" --wait 0.5
# Auto-open browser after 2 seconds
livereload --open-url-delay 2
# Enable debug logging
livereload --debug
# Complex configuration
livereload --host 0.0.0.0 --port 3000 --target "src/**/*.js" --wait 1.0 --debug ./distDjango management command for running development server with LiveReload functionality integrated into Django's development workflow.
class Command(BaseCommand):
"""
Django management command extending BaseCommand.
Provides 'livereload' management command that integrates
LiveReload server with Django's development server.
"""
def add_arguments(self, parser):
"""
Add command-line arguments for Django management command.
Args:
parser: Django argument parser
"""
def handle(self, *args, **options):
"""
Handle Django management command execution.
Args:
*args: Command arguments
**options: Command options dictionary
"""Django Command Syntax:
python manage.py livereload [addrport] [options]Available Options:
addrport: Host and port for Django server (default: '127.0.0.1:8000')-l, --liveport PORT: LiveReload server port (default: 35729)Usage Examples:
# Run Django with LiveReload on default ports
python manage.py livereload
# Custom Django server address
python manage.py livereload 0.0.0.0:8080
# Custom LiveReload port
python manage.py livereload --liveport 35730
# Full custom configuration
python manage.py livereload 192.168.1.100:9000 --liveport 35731Access command-line functionality programmatically for custom development tools and scripts.
Usage Examples:
from livereload.cli import main
import sys
# Run with custom arguments
sys.argv = ['livereload', '--port', '8080', '--debug', './static']
main()
# Direct argument passing
main(['--host', '0.0.0.0', '--port', '3000', '/path/to/project'])
# Integration with custom build tools
def start_dev_server(project_path, port=5500):
args = ['--port', str(port), '--debug', project_path]
main(args)
start_dev_server('./my-project', port=8080)Integration patterns for popular Python web frameworks beyond Django.
Flask Integration:
from flask import Flask
from livereload import Server
import os
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/')
def hello():
return 'Hello World!'
if __name__ == '__main__':
if os.environ.get('FLASK_ENV') == 'development':
# Use LiveReload in development
server = Server(app.wsgi_app)
server.watch('templates/')
server.watch('static/')
server.watch('*.py')
server.serve(port=5000, debug=True)
else:
# Standard Flask server in production
app.run()FastAPI Integration:
from fastapi import FastAPI
from livereload import Server
import uvicorn
import os
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
if __name__ == "__main__":
if os.environ.get('ENVIRONMENT') == 'development':
# Use LiveReload for development
server = Server()
server.watch('*.py')
server.watch('templates/')
server.watch('static/')
# Start FastAPI with uvicorn in a separate thread
import threading
def run_fastapi():
uvicorn.run(app, host="127.0.0.1", port=8000)
fastapi_thread = threading.Thread(target=run_fastapi)
fastapi_thread.daemon = True
fastapi_thread.start()
# Start LiveReload server
server.serve(port=5500)
else:
uvicorn.run(app, host="0.0.0.0", port=8000)Generic WSGI Integration:
from livereload import Server
import os
def create_app():
# Your WSGI application factory
from myapp import create_application
return create_application()
def run_development_server():
app = create_app()
server = Server(app)
# Watch application files
server.watch('myapp/')
server.watch('templates/')
server.watch('static/')
server.watch('config.py')
# Custom build steps
from livereload import shell
server.watch('assets/*.scss', shell('sass assets/main.scss static/css/main.css'))
server.watch('assets/*.js', shell('browserify assets/main.js -o static/js/bundle.js'))
server.serve(
port=int(os.environ.get('PORT', 5000)),
host=os.environ.get('HOST', '127.0.0.1'),
debug=True,
open_url_delay=1
)
if __name__ == '__main__':
run_development_server()# docker-entrypoint.py
from livereload import Server
import os
def main():
server = Server()
# Watch mounted volumes
server.watch('/app/src/')
server.watch('/app/templates/')
server.watch('/app/static/')
# Production-like serving for Docker
server.serve(
host='0.0.0.0', # Allow external connections
port=int(os.environ.get('PORT', 5000)),
debug=os.environ.get('DEBUG', 'false').lower() == 'true',
open_url=False # No browser in Docker
)
if __name__ == '__main__':
main()# Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "docker-entrypoint.py"]# Run with live reloading
docker run -p 5000:5000 -v $(pwd):/app -e DEBUG=true myapp# dev-server.py
from livereload import Server
import subprocess
import threading
import time
def start_multiple_services():
# Start backend API
def run_api():
subprocess.run(['python', 'api/main.py'])
# Start frontend build watcher
def run_frontend():
subprocess.run(['npm', 'run', 'watch'], cwd='frontend/')
# Start services in background
api_thread = threading.Thread(target=run_api)
frontend_thread = threading.Thread(target=run_frontend)
api_thread.daemon = True
frontend_thread.daemon = True
api_thread.start()
frontend_thread.start()
# Give services time to start
time.sleep(3)
# Start LiveReload server
server = Server()
server.watch('api/')
server.watch('frontend/src/')
server.watch('shared/')
server.serve(
port=3000,
host='127.0.0.1',
open_url_delay=2,
root='frontend/dist'
)
if __name__ == '__main__':
start_multiple_services()#!/usr/bin/env python3
# devtool.py - Custom development tool with LiveReload
import argparse
import os
from livereload import Server, shell
def create_parser():
parser = argparse.ArgumentParser(description='Custom Development Tool')
parser.add_argument('--port', type=int, default=5500, help='Server port')
parser.add_argument('--host', default='127.0.0.1', help='Server host')
parser.add_argument('--watch', action='append', help='Additional paths to watch')
parser.add_argument('--build-cmd', help='Build command to run on changes')
parser.add_argument('--no-browser', action='store_true', help='Disable auto-open browser')
parser.add_argument('project_dir', help='Project directory to serve')
return parser
def main():
parser = create_parser()
args = parser.parse_args()
server = Server()
# Watch project directory
server.watch(args.project_dir)
# Watch additional paths
if args.watch:
for path in args.watch:
server.watch(path)
# Add build command if specified
if args.build_cmd:
server.watch(args.project_dir, shell(args.build_cmd))
# Start server
server.serve(
port=args.port,
host=args.host,
root=args.project_dir,
open_url_delay=None if args.no_browser else 1
)
if __name__ == '__main__':
main()# Usage examples
./devtool.py ./my-project
./devtool.py --port 8080 --build-cmd "npm run build" ./frontend
./devtool.py --watch assets/ --watch config/ --no-browser ./appInstall with Tessl CLI
npx tessl i tessl/pypi-livereload