home · contact · privacy
Add Jinja2 templating, and requirements.txt infrastructure.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 16 Mar 2024 21:44:07 +0000 (22:44 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 16 Mar 2024 21:44:07 +0000 (22:44 +0100)
requirements.txt [new file with mode: 0644]
scripts/setup_dev_environment.sh
task.py
templates/base.html [new file with mode: 0644]
templates/index.html [new file with mode: 0644]

diff --git a/requirements.txt b/requirements.txt
new file mode 100644 (file)
index 0000000..cd1737d
--- /dev/null
@@ -0,0 +1 @@
+Jinja2==3.1.3
index 8ea3a443cd865cc1a0d2d8166978391cddd7e868..32848d94510170fd58462ff62b1e2b9e0f3438c4 100755 (executable)
@@ -3,7 +3,11 @@ set -e
 
 if [ ! -d '.git' ]; then
   echo "Cannot find .git/, please run me from your cloned repo's top directory!" 
-elif [ ! -f '.git/hooks/pre-commit' ]; then
+fi
+
+pip install -r requirements.txt
+
+if [ ! -f '.git/hooks/pre-commit' ]; then
   echo 'Linking pre-commit hook.' 
   cd .git/hooks/
   ln -s ../../scripts/pre-commit
diff --git a/task.py b/task.py
index a1510b0a202571796a5da96262fc115fb3e85a84..2a6c699f99cb956192a33727a69cb7f808cd8ff2 100755 (executable)
--- a/task.py
+++ b/task.py
@@ -4,8 +4,10 @@ plom's task manager
 """
 from http.server import BaseHTTPRequestHandler
 from http.server import HTTPServer
+from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
 
 HTTP_PORT = 8082
+TEMPLATES_DIR = 'templates'
 
 
 class HandledException(Exception):
@@ -19,13 +21,14 @@ class TaskHandler(BaseHTTPRequestHandler):
         """Handle any GET request."""
         self.send_response(200)
         self.end_headers()
-        html = '<html><body><p>hi there!</p></body></html>'
+        html = self.server.jinja.get_template('index.html').render()
         self.wfile.write(bytes(html, 'utf-8'))
 
 
 def main():
     """Main loop."""
     server = HTTPServer(('localhost', HTTP_PORT), TaskHandler)
+    server.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
     print(f'running at port {HTTP_PORT}')
     try:
         server.serve_forever()
diff --git a/templates/base.html b/templates/base.html
new file mode 100644 (file)
index 0000000..508281e
--- /dev/null
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<html>
+<meta charset="UTF-8">
+<body>
+{% block content %}
+{% endblock %}
+</body>
+</html>
diff --git a/templates/index.html b/templates/index.html
new file mode 100644 (file)
index 0000000..72d81d4
--- /dev/null
@@ -0,0 +1,5 @@
+{% extends 'base.html' %}
+
+{% block content %}
+Hi there!
+{% endblock %}