Skip to main content

Python LiveReload is an awesome tool for web developers

Project description

This is a brand new LiveReload in version 2.0.0.

Download on PyPi

Installation

Python LiveReload is designed for web developers who know Python.

Install Python LiveReload with pip:

$ pip install livereload

If you don’t have pip installed, try easy_install:

$ easy_install livereload

Command Line Interface

Python LiveReload provides a command line utility, livereload, for starting a server in a directory.

By default, it will listen to port 35729, the common port for LiveReload browser extensions.

$ livereload --help
usage: livereload [-h] [-p PORT] [-w WAIT] [directory]

Start a `livereload` server

positional arguments:
  directory             Directory to watch for changes

optional arguments:
  -h, --help            show this help message and exit
  -p PORT, --port PORT  Port to run `livereload` server on
  -w WAIT, --wait WAIT  Time delay before reloading

Older versions of Python LiveReload used a Guardfile to describe optional additional rules for files to watch and build commands to run on changes. This conflicted with other tools that used the same file for their configuration and is no longer supported since Python LiveReload version 2.0.0. Instead of a Guardfile you can now write a Python script using very similar syntax and run it instead of the command line application.

Script example: Sphinx

Here’s a simple example script that rebuilds Sphinx documentation:

#!/usr/bin/env python
from livereload import Server, shell
server = Server()
server.watch('docs/*.rst', shell('make html', cwd='docs'))
server.serve(root='docs/_build/html')

Run it, then open http://localhost:5500/ and you can see the documentation changes in real time.

Developer Guide

The new livereload server is designed for developers. It can power a wsgi application now:

from livereload import Server, shell

server = Server(wsgi_app)

# run a shell command
server.watch('static/*.stylus', 'make static')

# run a function
def alert():
    print('foo')
server.watch('foo.txt', alert)

# output stdout into a file
server.watch('style.less', shell('lessc style.less', output='style.css'))

server.serve()

The Server class accepts parameters:

  • app: a wsgi application

  • watcher: a watcher instance, you don’t have to create one

server.watch

server.watch can watch a filepath, a directory and a glob pattern:

server.watch('path/to/file.txt')
server.watch('directory/path/')
server.watch('glob/*.pattern')

You can also use other library (for example: formic) for more powerful file adding:

for filepath in formic.FileSet(include="**.css"):
    server.watch(filepath, 'make css')

You can delay a certain seconds to send the reload signal:

# delay 2 seconds for reloading
server.watch('path/to/file', delay=2)

server.setHeader

`server.setHeader` can be used to add one or more headers to the HTTP response:

server.setHeader('Access-Control-Allow-Origin', '*')
server.setHeader('Access-Control-Allow-Methods', '*')

server.serve

Setup a server with server.serve method. It can create a static server and a livereload server:

# use default settings
server.serve()

# livereload on another port
server.serve(liveport=35729)

# use custom host and port
server.serve(port=8080, host='localhost')

# open the web browser on startup, based on $BROWSER environment variable
server.serve(open_url_delay=5, debug=False)

# set a custom default file to open
server.serve(default_filename='example.html')

shell

The powerful shell function will help you to execute shell commands. You can use it with server.watch:

# you can redirect command output to a file
server.watch('style.less', shell('lessc style.less', output='style.css'))

# commands can be a list
server.watch('style.less', shell(['lessc', 'style.less'], output='style.css'))

# working with Makefile
server.watch('assets/*.styl', shell('make assets', cwd='assets'))

Frameworks Integration

Livereload can work seamlessly with your favorite framework.

Django

For Django there is a management command included.

To use simply

  • add 'livereload' to your INSTALLED_APPS and

  • then run ./manage.py livereload.

For available options like host and ports please refer to ./manage.py livereload -h.

To automagically serve static files like the native runserver command you have to use dj-static. (follow the simple instructions there).

Flask

Wrap Flask with livereload is much simpler:

# app is a Flask object
app = create_app()

# remember to use DEBUG mode for templates auto reload
# https://github.com/lepture/python-livereload/issues/144
app.debug = True

server = Server(app.wsgi_app)
# server.watch
server.serve()

Bottle

Wrap the Bottle app with livereload server:

# Without this line templates won't auto reload because of caching.
# http://bottlepy.org/docs/dev/tutorial.html#templates
bottle.debug(True)

app = Bottle()
server = Server(app)
# server.watch
server.serve()

Security Report

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

livereload-2.6.3.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

livereload-2.6.3-py2.py3-none-any.whl (24.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file livereload-2.6.3.tar.gz.

File metadata

  • Download URL: livereload-2.6.3.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.2

File hashes

Hashes for livereload-2.6.3.tar.gz
Algorithm Hash digest
SHA256 776f2f865e59fde56490a56bcc6773b6917366bce0c267c60ee8aaf1a0959869
MD5 4b68646bf76c7be88f584dfe04288a91
BLAKE2b-256 bd606640b819e858562ef6684abac60593b7369fe0a8a064df426d3ab0ab894d

See more details on using hashes here.

File details

Details for the file livereload-2.6.3-py2.py3-none-any.whl.

File metadata

  • Download URL: livereload-2.6.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for livereload-2.6.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ad4ac6f53b2d62bb6ce1a5e6e96f1f00976a32348afedcb4b6d68df2a1d346e4
MD5 61bf1146162b994b0dbea7beb6c5f329
BLAKE2b-256 e305ed67ccf462fff0b559e6ea7b3e3fcb20dec9d57bf90b5c5e72a6f316183e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page