0
# CLI and Framework Integrations
1
2
Command-line interface and framework integrations for quick development server setup and seamless integration with existing development workflows.
3
4
## Capabilities
5
6
### Command Line Interface
7
8
Quick development server setup through the `livereload` command with comprehensive configuration options.
9
10
```python { .api }
11
def main(argv=None):
12
"""
13
Main entry point for livereload command-line interface.
14
15
Args:
16
argv (list): Command line arguments (optional, uses sys.argv if None)
17
"""
18
```
19
20
**Command Syntax:**
21
22
```bash
23
livereload [options] [directory]
24
```
25
26
**Available Options:**
27
28
- `--host HOST`: Hostname to run server on (default: '127.0.0.1')
29
- `-p, --port PORT`: Port to run server on (default: 35729)
30
- `directory`: Directory to serve files from (default: current directory)
31
- `-t, --target TARGET`: File or directory to watch for changes
32
- `-w, --wait SECONDS`: Time delay before reloading (default: 0.0)
33
- `-o, --open-url-delay SECONDS`: Auto-open browser after delay
34
- `-d, --debug`: Enable Tornado debug logging
35
36
**Usage Examples:**
37
38
```bash
39
# Serve current directory with defaults
40
livereload
41
42
# Serve specific directory
43
livereload /path/to/project
44
45
# Custom port and host
46
livereload --host 0.0.0.0 --port 8080
47
48
# Watch specific files with delay
49
livereload --target "*.css" --wait 0.5
50
51
# Auto-open browser after 2 seconds
52
livereload --open-url-delay 2
53
54
# Enable debug logging
55
livereload --debug
56
57
# Complex configuration
58
livereload --host 0.0.0.0 --port 3000 --target "src/**/*.js" --wait 1.0 --debug ./dist
59
```
60
61
### Django Framework Integration
62
63
Django management command for running development server with LiveReload functionality integrated into Django's development workflow.
64
65
```python { .api }
66
class Command(BaseCommand):
67
"""
68
Django management command extending BaseCommand.
69
70
Provides 'livereload' management command that integrates
71
LiveReload server with Django's development server.
72
"""
73
74
def add_arguments(self, parser):
75
"""
76
Add command-line arguments for Django management command.
77
78
Args:
79
parser: Django argument parser
80
"""
81
82
def handle(self, *args, **options):
83
"""
84
Handle Django management command execution.
85
86
Args:
87
*args: Command arguments
88
**options: Command options dictionary
89
"""
90
```
91
92
**Django Command Syntax:**
93
94
```bash
95
python manage.py livereload [addrport] [options]
96
```
97
98
**Available Options:**
99
100
- `addrport`: Host and port for Django server (default: '127.0.0.1:8000')
101
- `-l, --liveport PORT`: LiveReload server port (default: 35729)
102
103
**Usage Examples:**
104
105
```bash
106
# Run Django with LiveReload on default ports
107
python manage.py livereload
108
109
# Custom Django server address
110
python manage.py livereload 0.0.0.0:8080
111
112
# Custom LiveReload port
113
python manage.py livereload --liveport 35730
114
115
# Full custom configuration
116
python manage.py livereload 192.168.1.100:9000 --liveport 35731
117
```
118
119
### Programmatic CLI Usage
120
121
Access command-line functionality programmatically for custom development tools and scripts.
122
123
**Usage Examples:**
124
125
```python
126
from livereload.cli import main
127
import sys
128
129
# Run with custom arguments
130
sys.argv = ['livereload', '--port', '8080', '--debug', './static']
131
main()
132
133
# Direct argument passing
134
main(['--host', '0.0.0.0', '--port', '3000', '/path/to/project'])
135
136
# Integration with custom build tools
137
def start_dev_server(project_path, port=5500):
138
args = ['--port', str(port), '--debug', project_path]
139
main(args)
140
141
start_dev_server('./my-project', port=8080)
142
```
143
144
### Framework Integration Patterns
145
146
Integration patterns for popular Python web frameworks beyond Django.
147
148
**Flask Integration:**
149
150
```python
151
from flask import Flask
152
from livereload import Server
153
import os
154
155
app = Flask(__name__)
156
app.config['DEBUG'] = True
157
158
@app.route('/')
159
def hello():
160
return 'Hello World!'
161
162
if __name__ == '__main__':
163
if os.environ.get('FLASK_ENV') == 'development':
164
# Use LiveReload in development
165
server = Server(app.wsgi_app)
166
server.watch('templates/')
167
server.watch('static/')
168
server.watch('*.py')
169
server.serve(port=5000, debug=True)
170
else:
171
# Standard Flask server in production
172
app.run()
173
```
174
175
**FastAPI Integration:**
176
177
```python
178
from fastapi import FastAPI
179
from livereload import Server
180
import uvicorn
181
import os
182
183
app = FastAPI()
184
185
@app.get("/")
186
def read_root():
187
return {"Hello": "World"}
188
189
if __name__ == "__main__":
190
if os.environ.get('ENVIRONMENT') == 'development':
191
# Use LiveReload for development
192
server = Server()
193
server.watch('*.py')
194
server.watch('templates/')
195
server.watch('static/')
196
197
# Start FastAPI with uvicorn in a separate thread
198
import threading
199
def run_fastapi():
200
uvicorn.run(app, host="127.0.0.1", port=8000)
201
202
fastapi_thread = threading.Thread(target=run_fastapi)
203
fastapi_thread.daemon = True
204
fastapi_thread.start()
205
206
# Start LiveReload server
207
server.serve(port=5500)
208
else:
209
uvicorn.run(app, host="0.0.0.0", port=8000)
210
```
211
212
**Generic WSGI Integration:**
213
214
```python
215
from livereload import Server
216
import os
217
218
def create_app():
219
# Your WSGI application factory
220
from myapp import create_application
221
return create_application()
222
223
def run_development_server():
224
app = create_app()
225
server = Server(app)
226
227
# Watch application files
228
server.watch('myapp/')
229
server.watch('templates/')
230
server.watch('static/')
231
server.watch('config.py')
232
233
# Custom build steps
234
from livereload import shell
235
server.watch('assets/*.scss', shell('sass assets/main.scss static/css/main.css'))
236
server.watch('assets/*.js', shell('browserify assets/main.js -o static/js/bundle.js'))
237
238
server.serve(
239
port=int(os.environ.get('PORT', 5000)),
240
host=os.environ.get('HOST', '127.0.0.1'),
241
debug=True,
242
open_url_delay=1
243
)
244
245
if __name__ == '__main__':
246
run_development_server()
247
```
248
249
## Advanced Integration Scenarios
250
251
### Docker Development Environment
252
253
```python
254
# docker-entrypoint.py
255
from livereload import Server
256
import os
257
258
def main():
259
server = Server()
260
261
# Watch mounted volumes
262
server.watch('/app/src/')
263
server.watch('/app/templates/')
264
server.watch('/app/static/')
265
266
# Production-like serving for Docker
267
server.serve(
268
host='0.0.0.0', # Allow external connections
269
port=int(os.environ.get('PORT', 5000)),
270
debug=os.environ.get('DEBUG', 'false').lower() == 'true',
271
open_url=False # No browser in Docker
272
)
273
274
if __name__ == '__main__':
275
main()
276
```
277
278
```dockerfile
279
# Dockerfile
280
FROM python:3.9-slim
281
WORKDIR /app
282
COPY requirements.txt .
283
RUN pip install -r requirements.txt
284
COPY . .
285
EXPOSE 5000
286
CMD ["python", "docker-entrypoint.py"]
287
```
288
289
```bash
290
# Run with live reloading
291
docker run -p 5000:5000 -v $(pwd):/app -e DEBUG=true myapp
292
```
293
294
### Multi-Project Development
295
296
```python
297
# dev-server.py
298
from livereload import Server
299
import subprocess
300
import threading
301
import time
302
303
def start_multiple_services():
304
# Start backend API
305
def run_api():
306
subprocess.run(['python', 'api/main.py'])
307
308
# Start frontend build watcher
309
def run_frontend():
310
subprocess.run(['npm', 'run', 'watch'], cwd='frontend/')
311
312
# Start services in background
313
api_thread = threading.Thread(target=run_api)
314
frontend_thread = threading.Thread(target=run_frontend)
315
316
api_thread.daemon = True
317
frontend_thread.daemon = True
318
319
api_thread.start()
320
frontend_thread.start()
321
322
# Give services time to start
323
time.sleep(3)
324
325
# Start LiveReload server
326
server = Server()
327
server.watch('api/')
328
server.watch('frontend/src/')
329
server.watch('shared/')
330
331
server.serve(
332
port=3000,
333
host='127.0.0.1',
334
open_url_delay=2,
335
root='frontend/dist'
336
)
337
338
if __name__ == '__main__':
339
start_multiple_services()
340
```
341
342
### Custom Development Tool
343
344
```python
345
#!/usr/bin/env python3
346
# devtool.py - Custom development tool with LiveReload
347
import argparse
348
import os
349
from livereload import Server, shell
350
351
def create_parser():
352
parser = argparse.ArgumentParser(description='Custom Development Tool')
353
parser.add_argument('--port', type=int, default=5500, help='Server port')
354
parser.add_argument('--host', default='127.0.0.1', help='Server host')
355
parser.add_argument('--watch', action='append', help='Additional paths to watch')
356
parser.add_argument('--build-cmd', help='Build command to run on changes')
357
parser.add_argument('--no-browser', action='store_true', help='Disable auto-open browser')
358
parser.add_argument('project_dir', help='Project directory to serve')
359
return parser
360
361
def main():
362
parser = create_parser()
363
args = parser.parse_args()
364
365
server = Server()
366
367
# Watch project directory
368
server.watch(args.project_dir)
369
370
# Watch additional paths
371
if args.watch:
372
for path in args.watch:
373
server.watch(path)
374
375
# Add build command if specified
376
if args.build_cmd:
377
server.watch(args.project_dir, shell(args.build_cmd))
378
379
# Start server
380
server.serve(
381
port=args.port,
382
host=args.host,
383
root=args.project_dir,
384
open_url_delay=None if args.no_browser else 1
385
)
386
387
if __name__ == '__main__':
388
main()
389
```
390
391
```bash
392
# Usage examples
393
./devtool.py ./my-project
394
./devtool.py --port 8080 --build-cmd "npm run build" ./frontend
395
./devtool.py --watch assets/ --watch config/ --no-browser ./app
396
```