Skip to content

Commit

Permalink
Add devtools & live reload
Browse files Browse the repository at this point in the history
  • Loading branch information
sondregronas committed Aug 22, 2024
1 parent d4f119e commit 3ca5209
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
23 changes: 23 additions & 0 deletions piggy/devtools.py
Original file line number Diff line number Diff line change
@@ -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 = '<script src="http://localhost:35729/livereload.js"></script>'
body = b"".join(response.response).decode()
body = body.replace("</body>", f"{script}</body>")
response.response = [body.encode("utf8")]
response.content_length = len(response.response[0])
return response
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ py-modules = []
[project.optional-dependencies]
dev = [
"ruff",
"livereload",
]

[tool.ruff]
Expand Down
13 changes: 11 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3ca5209

Please sign in to comment.