Skip to content

Tobi-De/litestar-browser-reload

Repository files navigation

litestar-browser-reload

PyPI - Version PyPI - Python Version


Important

This plugin currently contains minimal features and is a work-in-progress

Automatic browser reload plugin for Litestar, designed for development use.

Table of Contents

Installation

pip install litestar-browser-reload

Usage

Parameters of BrowserReloadPlugin:

  • watch_paths: Sequence[Union[str, Path]]: Paths to watch for changes.
  • watch_filter: BaseFilter | None = None: A filter to exclude certain directories or patterns.
from pathlib import Path

from litestar import Litestar
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.template.config import TemplateConfig
from litestar_browser_reload import BrowserReloadPlugin
from watchfiles import DefaultFilter

templates_path = Path("templates")
browser_reload = BrowserReloadPlugin(
    watch_paths=(Path("templates"),),
    watch_filter=DefaultFilter(ignore_dirs=(".git", ".hg", ".svn", ".tox")),
)

app = Litestar(
    route_handlers=[],
    debug=True,
    plugins=[browser_reload],
    template_config=TemplateConfig(
        directory=templates_path,
        engine=JinjaTemplateEngine,
    ),
)

Add the following to your base template:

<head>
    ...
    {% if request.app.debug %}
    <script
        src="/__reload__/static/reload-listener.js"
        data-worker-script-path="/__reload__/static/reload-worker.js"
        data-ws-path="/__reload__"
        defer
    ></script>
    {% endif %}
 </head>

License

litestar-browser-reload is distributed under the terms of the MIT license.

Credits

Much of this was copied from django-browser-reload and foxglove