Skip to content

Instantly share code, notes, and snippets.

@mikeckennedy
Last active October 15, 2024 18:07
Show Gist options
  • Save mikeckennedy/4e1378477a6d174aa8d59921f8db89c3 to your computer and use it in GitHub Desktop.
Save mikeckennedy/4e1378477a6d174aa8d59921f8db89c3 to your computer and use it in GitHub Desktop.
Example of livereload in python: Creates a file watcher that runs `python build_assets.py`, a utility based on webassets package whenever a css or js file is edited. It bundles a bunch of our CSS files into a single minified one and does the same for our JS files.
from pathlib import Path
# Creates a file watcher that runs `python build_assets.py`, a utility based on
# webassets package whenever a css or js file is edited. It bundles a bunch of
# our CSS files into a single minified one and does the same for our JS files.
def main():
# Only import this if we run it, don't let the webframework try to pull it in prod.
import livereload.server
# This file lives in the webroot/bin/ folder.
root_folder = Path(__file__).parent.parent.absolute()
static_folder = root_folder / "static"
css_folder = static_folder / 'css' / '*.css'
js_folder = static_folder / 'js' / '*.js'
python_exe = root_folder.parent / 'venv' / 'bin' / 'python'
cmd = Path(__file__).parent / 'build_assets.py'
shell = livereload.shell(python_exe.as_posix() + ' ' + cmd.as_posix())
server = livereload.server.Server()
server.watch(css_folder.as_posix(), shell, delay=0.5)
server.watch(js_folder.as_posix(), shell, delay=0.5)
server.serve(debug=False, root=root_folder.as_posix())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment