hs3-baza-zasobow-dashboard/main.py

46 lines
1.4 KiB
Python
Raw Normal View History

import os, re, shutil
2026-01-11 18:33:53 +01:00
from jinja2 import Environment, FileSystemLoader
import pandas as pd
2026-01-11 19:25:04 +01:00
from discourse import DiscourseDatabase
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>'
)
return row + [download_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-01-11 18:33:53 +01:00
website_folder = "docs"
data = pd.read_csv("zasoby.csv")
env = Environment(loader=FileSystemLoader("template"))
2026-01-11 19:25:04 +01:00
print("Removing old website files")
2026-01-11 18:33:53 +01:00
shutil.rmtree(f"./{website_folder}")
os.mkdir(f"./{website_folder}")
2026-01-11 19:25:04 +01:00
print("Creating a new website")
2026-01-11 18:33:53 +01:00
shutil.copytree("template/static", f"{website_folder}/static")
template = env.get_template("_main_layout.html")
with open(f"{website_folder}/index.html", "w+", encoding="utf-8") as file:
header_row = data.columns.values.tolist() + ["label"]
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-01-11 19:25:04 +01:00
DiscourseDatabase()
2026-01-11 18:33:53 +01:00
generate_dashboard()
print("Done!")