API Reference#
Helper Functions#
- class livereload.shell(cmd, output=None, mode='w', cwd=None, shell=False)#
Execute a shell command.
You can add a shell command:
server.watch( 'style.less', shell('lessc style.less', output='style.css') )
- Parameters
cmd – a shell command, string or list
output – output stdout to the given file
mode – only works with output, mode
w
means write, modea
means appendcwd – set working directory before command is executed.
shell – if true, on Unix the executable argument specifies a replacement shell for the default
/bin/sh
.
Server#
- class livereload.Server(app=None, watcher=None)#
Livereload server interface.
Initialize a server and watch file changes:
server = Server(wsgi_app) server.serve()
- Parameters
app – a WSGI application instance
watcher – A Watcher instance, you don’t have to initialize it by yourself. Under Linux, you will want to install pyinotify and use INotifyWatcher() to avoid wasted CPU usage.
- setHeader(name, value)#
- Add or override HTTP headers at the at the beginning of the
request.
Once you have initialized a server, you can add one or more headers before starting the server:
server.setHeader('Access-Control-Allow-Origin', '*') server.setHeader('Access-Control-Allow-Methods', '*') server.serve()
- Parameters
name – The name of the header field to be defined.
value – The value of the header field to be defined.
- watch(filepath, func=None, delay=None, ignore=None)#
Add the given filepath for watcher list.
Once you have initialized a server, watch file changes before serve the server:
server.watch('static/*.stylus', 'make static') def alert(): print('foo') server.watch('foo.txt', alert) server.serve()
- Parameters
filepath – files to be watched, it can be a filepath, a directory, or a glob pattern
func – the function to be called, it can be a string of shell command, or any callable object without parameters
delay – Delay sending the reload message. Use ‘forever’ to not send it. This is useful to compile sass files to css, but reload on changed css files then only.
ignore – A function return True to ignore a certain pattern of filepath.
- serve(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')#
Start serve the server with the given port.
- Parameters
port – serve on this port, default is 5500
liveport – live reload on this port
host – serve on this hostname, default is 127.0.0.1
root – serve static on this root directory
debug – set debug mode, which autoreloads the app on code changes via Tornado (and causes polling). Defaults to True when
self.app
is set, otherwise False.open_url_delay – open webbrowser after the delay seconds
live_css – whether to use live css or force reload on css. Defaults to True
default_filename – launch this file from the selected root on startup