forked from Ravise/misc-scripts
postbox - jinja2 html
This commit is contained in:
parent
a9c6e9da9c
commit
cfb957b3e9
@ -16,3 +16,4 @@ Requirements:
|
||||
License:
|
||||
- included code CC-0
|
||||
- matplotlib Copyright (c) 2012- Matplotlib Development Team; All Rights Reserved
|
||||
- Jinja2 Copyright 2007 Pallets, used under BSD-3-Clause License
|
84
postoffice/post_office_jinja/__init__.py
Normal file
84
postoffice/post_office_jinja/__init__.py
Normal file
@ -0,0 +1,84 @@
|
||||
import collections
|
||||
import csv
|
||||
import dataclasses
|
||||
import datetime
|
||||
import pathlib
|
||||
import sys
|
||||
from html_minifier.minify import Minifier
|
||||
from bs4 import BeautifulSoup as bs
|
||||
import jinja2
|
||||
|
||||
LOW = 13
|
||||
HIGH = 15
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Postbox:
|
||||
owner: str | None
|
||||
x: int
|
||||
y: int
|
||||
z: int
|
||||
|
||||
def to_dict(self):
|
||||
return vars(self)
|
||||
|
||||
|
||||
def main():
|
||||
env = jinja2.Environment(loader=jinja2.PackageLoader("post_office_jinja"))
|
||||
|
||||
postboxes = get_postboxes("data.csv")
|
||||
postbox_map = collections.defaultdict(lambda: collections.defaultdict(dict))
|
||||
for box in postboxes:
|
||||
postbox_map[box.x][box.y][box.z] = box
|
||||
|
||||
minx, maxx, minz, maxz = get_limits(postboxes)
|
||||
|
||||
entries = []
|
||||
|
||||
for z in range(minz, maxz + 1):
|
||||
superrow = []
|
||||
for y in (HIGH, LOW):
|
||||
row = []
|
||||
for x in range(minx, maxx + 1):
|
||||
try:
|
||||
pb: Postbox = postbox_map[x][y][z]
|
||||
row.append(pb)
|
||||
except KeyError:
|
||||
row.append(Postbox(None, x, y, z))
|
||||
superrow.append(row)
|
||||
entries.append(superrow)
|
||||
|
||||
tpl = env.get_template("postbox.j2.html")
|
||||
dat = tpl.render(postboxes=entries, today=datetime.date.today().isoformat())
|
||||
|
||||
minifier = Minifier(dat)
|
||||
dat = minifier.minify()
|
||||
|
||||
soup = bs(dat, features="html.parser")
|
||||
dat = soup.prettify()
|
||||
|
||||
with open("out.html", "wt", encoding="utf-8") as fw:
|
||||
fw.write(dat)
|
||||
|
||||
|
||||
def get_postboxes(fname):
|
||||
postboxes = []
|
||||
|
||||
with open(fname, "rt", newline="") as fr:
|
||||
for line in csv.reader(fr):
|
||||
x, y, z, owner = line
|
||||
pb = Postbox(owner, int(x), int(y), int(z))
|
||||
postboxes.append(pb)
|
||||
|
||||
return postboxes
|
||||
|
||||
|
||||
def get_limits(postboxes):
|
||||
minz, maxz = min([pb.z for pb in postboxes]), max([pb.z for pb in postboxes])
|
||||
minx, maxx = min([pb.x for pb in postboxes]), max([pb.x for pb in postboxes])
|
||||
|
||||
return minx, maxx, minz, maxz
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
0
postoffice/post_office_jinja/templates/__init__.py
Normal file
0
postoffice/post_office_jinja/templates/__init__.py
Normal file
40
postoffice/post_office_jinja/templates/postbox.j2.html
Normal file
40
postoffice/post_office_jinja/templates/postbox.j2.html
Normal file
@ -0,0 +1,40 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Haven Post Office layout</title>
|
||||
<style>
|
||||
h1, h2 {text-align: center; }
|
||||
table {border-collapse: collapse; }
|
||||
td {border: 1px solid black; text-align: center;white-space:nowrap;}
|
||||
|
||||
.pb_empty {background: green; font-style: italic;}
|
||||
.pb_occupied {background: red; }
|
||||
.pb_none {background: white; }
|
||||
|
||||
.coords {font-size: x-small; }
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Haven Post Office</h1>
|
||||
<h2>{{ today }}</h2>
|
||||
<table>
|
||||
{% for X in postboxes %}
|
||||
{% for Y in X %}
|
||||
<tr>
|
||||
{% for pb in Y %}
|
||||
{% if pb.owner is none %}
|
||||
<td class='pb_none'> <br><span class="coords"> </span></td>
|
||||
{% elif pb.owner %}
|
||||
<td class='pb_occupied'>{{pb.owner}}<br><span class="coords">{{pb.x}}, {{pb.y}}, {{pb.z}}</span></td>
|
||||
{% else %}
|
||||
<td class='pb_empty'>empty<br><span class="coords">{{pb.x}}, {{pb.y}}, {{pb.z}}</span></td>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
@ -1 +1,3 @@
|
||||
matplotlib~=3.7.1
|
||||
matplotlib~=3.7.1
|
||||
Jinja2~=3.1.2
|
||||
beautifulsoup4~=4.12.2
|
Loading…
Reference in New Issue
Block a user