-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (70 loc) · 2.7 KB
/
Copy pathmain.py
File metadata and controls
93 lines (70 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import streamlit as st
from pathlib import Path
import os
import importlib.util
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
from loader import load_csv, save_last_file, load_last_file
TMP_DIR = "tmp"
os.makedirs(TMP_DIR, exist_ok=True)
uploaded_file = st.file_uploader("CSV-Datei wählen", type="csv")
df = None
# Neue Datei hochgeladen → temporär speichern
if uploaded_file is not None:
tmp_path = os.path.join(TMP_DIR, uploaded_file.name)
with open(tmp_path, "wb") as f:
f.write(uploaded_file.getbuffer())
st.info(f"CSV temporär gespeichert: {tmp_path}")
df = load_csv(tmp_path)
save_last_file(tmp_path)
# Falls nichts hochgeladen, aber alte Datei existiert → autoload
elif (last_file := load_last_file()) and Path(last_file).exists():
st.info(f"Letzte Datei automatisch geladen: {last_file}")
df = load_csv(last_file)
if df is None:
st.warning("Bitte CSV-Datei hochladen")
st.stop()
# -------------------------------------------------------
# 🔥 Globaler Datumsbereich-Filter
# -------------------------------------------------------
# Erwartet: df["Datum"] ist pandas datetime
if not "Buchungstag" in df.columns:
st.error("Die CSV benötigt eine Spalte 'Datum'.")
st.stop()
df["Datum"] = df["Buchungstag"].astype("datetime64[ns]")
min_date = df["Datum"].min()
max_date = df["Datum"].max()
date_range = st.date_input(
"Datumsbereich auswählen",
value=(min_date, max_date)
)
if len(date_range) == 2:
start_date, end_date = date_range
df = df[(df["Datum"] >= str(start_date)) & (df["Datum"] <= str(end_date))]
# Falls nach dem Filter keine Daten übrig sind
if df.empty:
st.warning("Keine Daten im ausgewählten Datumsbereich.")
st.stop()
# -------------------------------------------------------
# 🔥 Plots dynamisch laden (plot(df))
# -------------------------------------------------------
plot_dir = "plots"
for filename in os.listdir(plot_dir):
if filename.endswith(".py"):
filepath = os.path.join(plot_dir, filename)
modulname = os.path.splitext(filename)[0]
spec = importlib.util.spec_from_file_location(modulname, filepath)
plot_module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(plot_module)
except Exception as e:
st.error(f"Fehler beim Laden von Plot-Modul `{filename}`: {e}")
continue
if hasattr(plot_module, "plot"):
try:
# globaler Filter → df
plot_module.plot(df)
except Exception as e:
st.error(f"Fehler im Plot `{filename}`: {e}")
import traceback
st.text(traceback.format_exc())