diff --git a/piggy/devtools.py b/piggy/devtools.py new file mode 100644 index 0000000..2bfbf31 --- /dev/null +++ b/piggy/devtools.py @@ -0,0 +1,23 @@ +from flask import Flask + + +def inject_devtools(app: Flask): + # Inject livereload script into HTML responses + @app.after_request + def after_request(response): + if response.status_code != 200: + return response + + mimetype = response.mimetype or "" + if not mimetype.startswith("text/html"): + return response + + if not isinstance(response.response, list): + return response + + script = '' + body = b"".join(response.response).decode() + body = body.replace("", f"{script}") + response.response = [body.encode("utf8")] + response.content_length = len(response.response[0]) + return response diff --git a/pyproject.toml b/pyproject.toml index 73782dd..0a87328 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ py-modules = [] [project.optional-dependencies] dev = [ "ruff", + "livereload", ] [tool.ruff] diff --git a/run.py b/run.py index 068ca00..be1ef30 100644 --- a/run.py +++ b/run.py @@ -22,13 +22,22 @@ def checkout_branch(branch): if __name__ == "__main__": + from livereload import Server + from piggy.devtools import inject_devtools + # checkout_branch("test-output") os.environ["FLASK_DEBUG"] = "1" os.environ["USE_CACHE"] = "0" + app = create_app() - app.config["TEMPLATES_AUTO_RELOAD"] = True + inject_devtools(app) + run_tailwind(reload=True) - app.run(port=5001) + + server = Server(app) + server.watch("piggy/**/*", ignore=lambda *_: False) + server.watch("piggybank/*", ignore=lambda *_: False) + server.serve(host="127.0.0.1", port=5001, debug=False) else: checkout_branch("output") run_tailwind()