python-livereload

repository·master·Indexed 21 days ago

https://github.com/lepture/python-livereload

A Python-based tool for web developers that automatically reloads webpages in the browser when file changes are detected. It provides a Server class for WSGI applications, a CLI for serving directories, and specific integrations for Bottle, Flask, and Django. The library allows watching files or directories to trigger browser reloads, execute Python callables, or run shell commands via the livereload.shell helper.

Tokens
2.3K
Snippets
14
Records
17
Agent score
72%

What's inside python-livereload

  1. Install LiveReload via pip

    master

    LiveReload is a tool for web developers using Python that allows webpages to reload automatically when changes are detected, eliminating the need to manually refresh the browser. You can install it using pip from PyPI.

    $ pip install livereload
  2. Integrate LiveReload with Bottle

    master

    You can use the livereload.Server class to serve a Bottle application and enable automatic reloading.

    Important: You must enable bottle.debug(True) to ensure that templates are reloaded when they change.

    import bottle
    from livereload import Server
    
    # debug mode is required for templates to be reloaded
    bottle.debug(True)
    
    app = bottle.Bottle()
    
    # Wrap the Bottle app with LiveReload Server
    server = Server(app)
    
    # Watch specific files or directories
    server.watch('templates/')
    
    # Start the server
    server.serve()
  3. Use the livereload CLI to start a live-reloading server

    master

    The livereload command allows you to start a live-reloading server that serves a specific directory. By default, the server listens on port 35729, which is the standard port used by most LiveReload browser extensions.

    Note: As of version 2.0.0, Guardfile is no longer supported. If you need custom file watching logic, you should write a Python script using the library's API instead of using the CLI with a guardfile.

    livereload
  4. Integrate LiveReload with Flask

    master

    You can use the livereload.Server class to wrap a Flask application's WSGI application. This allows LiveReload to monitor file changes and trigger browser reloads while running your Flask app.

    Requirements:

    • You must enable Flask's debug mode (app.debug = True) to ensure that templates are reloaded correctly when changes occur.
    from livereload import Server
    
    app = create_app()
    app.debug = True  # debug mode is required for templates to be reloaded
    
    server = Server(app.wsgi_app)
    server.watch('path/to/watch')
    server.serve()
  5. Initialize and run the LiveReload server

    master

    To use LiveReload, initialize a Server instance with your WSGI application and call .serve(). This starts a Tornado-based server that watches for file changes and automatically injects the LiveReload script into your HTML responses.

    from livereload import Server
    from my_app import app  # Your WSGI application
    
    server = Server(app)
    server.serve(port=5500, host='127.0.0.1')
    from livereload import Server
    from my_app import app
    
    server = Server(app)
    server.serve(port=5500, host='127.0.0.1')
  6. Use the livereload.shell helper function

    master
    The livereload.shell class provides a way to run shell commands while automatically triggering a LiveReload event whenever a file change is detected in the working directory. This is useful for automating build processes or running development servers that need to be synchronized with file changes.
  7. Use the livereload.Server class

    master
    The livereload.Server class is the core component of the library. It manages the LiveReload server, handles connections from clients (like browsers), and coordinates the reloading process when files change. You can use it to watch specific directories or files and notify connected clients to reload.
  8. Watch files and execute shell commands on change

    master

    You can instruct the server to watch specific files, directories, or glob patterns. When a change is detected, you can either execute a Python callable or a shell command.

    If you provide a string, it is treated as a shell command. You can use the shell helper to redirect the command's output to a file.

    server = Server(app)
    
    # Execute a shell command (e.g., compiling LESS to CSS)
    server.watch('static/style.less', 'lessc static/style.less static/style.css')
    
    # Execute a Python function
    def my_callback():
        print('File changed!')
    server.watch('config.json', my_callback)
    
    server.serve()
    server.watch('static/style.less', 'lessc static/style.less static/style.css')
    
    def my_callback():
        print('File changed!')
    server.watch('config.json', my_callback)
    
    server.serve()
  9. Configure the Server.serve() method

    master

    The serve method controls how the server operates. Key parameters include:

    • port (int, default: 5500): The port to serve the web application on.
    • liveport (int, optional): A separate port for LiveReload communication. If provided, the server runs two separate Tornado applications.
    • host (str, default: '127.0.0.1'): The hostname to bind to.
    • root (str, optional): The directory to serve static files from.
    • debug (bool, optional): Enables Tornado's autoreload mode (polling). Defaults to True if a WSGI app is provided.
    • open_url_delay (float, optional): Delay in seconds before automatically opening the browser to the server URL.
    • live_css (bool, default: True): If True, uses LiveReload's CSS injection (no full page reload) for .css files.
    server.serve(
        port=5500,
        host='0.0.0.0',
        root='./public',
        open_url_delay=2,
        live_css=True
    )
  10. Add custom HTTP headers to static files

    master

    Use setHeader(name, value) to add or override HTTP headers for all static file requests served by the server. This is useful for setting CORS headers.

    server = Server(app)
    server.setHeader('Access-Control-Allow-Origin', '*')
    server.serve()
    server = Server(app)
    server.setHeader('Access-Control-Allow-Origin', '*')
    server.serve()