hs3-baza-zasobow-dashboard/fegen/main.py

45 lines
1.5 KiB
Python
Raw Permalink Normal View History

import os, re, shutil
2026-01-11 18:33:53 +01:00
from jinja2 import Environment, FileSystemLoader
import pandas as pd
def add_download_button(row):
item_id = re.sub(r'<a href.*/([0-9]+)".*', "\\1", row[1])
download_button = (
f'<button id="btn_{item_id}"><i class="fa fa-download"></i> {item_id}</button>'
)
2026-03-24 15:39:18 +01:00
print_button = f'<a href="http://localhost:31337/print/{item_id}"><button id="prn_{item_id}"><i class="fa fa-print"></i> {item_id}</button></a>'
return row + [download_button, print_button]
2026-01-11 18:33:53 +01:00
def generate_dashboard():
"""Generate dashboard from zasoby.csv file"""
2026-01-11 19:25:04 +01:00
print("Generating HTML dashboard")
2026-03-24 15:39:18 +01:00
website_folder = "fegen/docs"
2026-01-11 18:33:53 +01:00
data = pd.read_csv("zasoby.csv")
2026-03-24 15:39:18 +01:00
env = Environment(loader=FileSystemLoader("fegen/template"))
2026-01-11 19:25:04 +01:00
print("Removing old website files")
print("Creating a new website")
2026-03-24 15:39:18 +01:00
shutil.copytree("fegen/template/static", f"{website_folder}/static", dirs_exist_ok=True)
2026-01-11 18:33:53 +01:00
template = env.get_template("_main_layout.html")
with open(f"{website_folder}/index.html", "w+", encoding="utf-8") as file:
2026-03-24 15:39:18 +01:00
header_row = data.columns.values.tolist() + ["label", "print"]
rows = map(
add_download_button,
data.values.tolist(),
)
html = template.render(
title="Baza Zasobów Hackerspace Trójmiasto",
t_header=header_row,
t_body=rows,
)
2026-01-11 18:33:53 +01:00
file.write(html)
if __name__ == "__main__":
2026-03-24 15:39:18 +01:00
from discourse import DiscourseDatabase
2026-01-11 19:25:04 +01:00
DiscourseDatabase()
2026-01-11 18:33:53 +01:00
generate_dashboard()
print("Done!")