Refactor report templates and styles

- Updated CSS styles in report.css for improved readability and consistency.
- Removed deprecated report_class.html and report_group.html files.
- Introduced new Jinja2 templates: report_class.jinja and report_group.jinja for better maintainability.
- Enhanced JavaScript functionality in report_class.js to support dynamic chart rendering using Chart.js.
- Updated report_group.js to streamline print functionality.
This commit is contained in:
2026-05-08 14:38:32 +04:00
parent c11c9e8aa8
commit 74edbde327
9 changed files with 644 additions and 683 deletions
+1 -1
View File
@@ -143,7 +143,7 @@ def create_default_pipeline(workspace_root: Path) -> Pipeline:
pipeline.add_stage(Stage(
name="Generate HTML Reports",
description="Create standalone HTML reports for groups and classes",
module_name="stage_generate_reports_html"
module_name="stage_generate_reports_html_jinja"
))
return pipeline
+70 -51
View File
@@ -163,43 +163,58 @@ def _format_percent(value: int, total: int) -> str:
return f"{value} ({round(value * 100 / total)}%)"
def _build_week_chart(weeks: List[Dict[str, Any]]) -> str:
if not weeks:
return "<div class='muted'>Нет данных.</div>"
def _normalize_day_codes(day: Dict[str, Any]) -> List[str]:
raw = day.get("codes")
if raw is None:
raw = day.get("lessons", "")
codes = list(str(raw or ""))[:8]
while len(codes) < 8:
codes.append("0")
return codes
width = max(720, len(weeks) * 92)
height = 280
bar_width = 42
max_value = max(
max(int(w.get("conducted", 0)), int(w.get("missed", 0)), int(w.get("other", 0)))
for w in weeks
) or 1
def bar_height(value: int) -> int:
return max(2, round((value / max_value) * 180)) if value else 0
pieces = [
f"<svg class='week-chart' viewBox='0 0 {width} {height}' preserveAspectRatio='xMinYMin meet'>"
]
baseline = 210
for index, week in enumerate(weeks):
x = 70 + index * 92
current_bottom = baseline
pieces.append(
f"<text class='chart-week-label' x='{x + bar_width / 2}' y='{baseline + 28}' text-anchor='middle'>{week.get('short')}</text>"
def _normalize_week_data(week: Dict[str, Any]) -> Dict[str, Any]:
normalized_days: List[Dict[str, Any]] = []
for day in week.get("days", []) or []:
normalized_days.append(
{
"date": day.get("date"),
"weekday": day.get("weekday"),
"codes": _normalize_day_codes(day),
}
)
for cls_name, value in [("code-1", int(week.get("missed", 0))), ("code-2", int(week.get("other", 0))), ("code-3", int(week.get("conducted", 0)))]:
h = bar_height(value)
if h:
pieces.append(
f"<rect class='week-bar {cls_name}' x='{x}' y='{current_bottom - h}' width='{bar_width}' height='{h}' rx='5'></rect>"
)
current_bottom -= h
pieces.append(
f"<text class='chart-count' x='{x + bar_width / 2}' y='{current_bottom - 8}' text-anchor='middle'>{int(week.get('total_pairs', 0))}</text>"
)
pieces.append("</svg>")
return "".join(pieces)
code_counts = {"0": 0, "1": 0, "2": 0, "3": 0}
for code, count in (week.get("code_counts", {}) or {}).items():
key = str(code)
if key in code_counts:
code_counts[key] += int(count)
if sum(code_counts.values()) == 0 and normalized_days:
for day in normalized_days:
for code in day["codes"]:
if code not in code_counts:
code_counts[code] = 0
code_counts[code] += 1
total_pairs = int(week.get("total_pairs", 0))
if total_pairs <= 0:
total_pairs = sum(int(v) for v in code_counts.values())
conducted = int(week.get("conducted", code_counts.get("3", 0)))
missed = int(week.get("missed", code_counts.get("1", 0)))
other = int(week.get("other", code_counts.get("2", 0)))
return {
**week,
"days": normalized_days,
"daily_summary": ["".join(day["codes"]) for day in normalized_days],
"code_counts": code_counts,
"total_pairs": total_pairs,
"conducted": conducted,
"missed": missed,
"other": other,
}
def _build_scheme_table(week: Dict[str, Any]) -> str:
@@ -215,16 +230,15 @@ def _build_scheme_table(week: Dict[str, Any]) -> str:
rows.append(f"<tr><th>{day.get('weekday')}</th>{cells}</tr>")
return (
"<table class='scheme-table'><thead><tr><th>День</th>"
+ "".join(f"<th>{i}</th>" for i in range(1, 9))
+ "</tr></thead><tbody>"
"<table class='scheme-table'><tbody>"
+ "".join(rows)
+ "</tbody></table>"
)
def _build_class_block(item: Dict[str, Any]) -> Dict[str, Any]:
weeks = item.get("weeks", []) or []
raw_weeks = item.get("weeks", []) or []
weeks = [_normalize_week_data(week) for week in raw_weeks]
for week in weeks:
total = int(week.get("total_pairs", 0))
week["conducted_display"] = _format_percent(int(week.get("conducted", 0)), total)
@@ -233,7 +247,12 @@ def _build_class_block(item: Dict[str, Any]) -> Dict[str, Any]:
week["scheme_html"] = _build_scheme_table(week)
item["weeks"] = weeks
item["chart_svg"] = _build_week_chart(weeks)
item["chart_data"] = {
"labels": [str(week.get("short") or week.get("long") or "") for week in weeks],
"missed": [int(week.get("missed", 0)) for week in weeks],
"other": [int(week.get("other", 0)) for week in weeks],
"conducted": [int(week.get("conducted", 0)) for week in weeks],
}
return item
@@ -270,18 +289,18 @@ def _build_class_report_model(workspace_root: Path, context: Dict[str, Any] | No
sections: List[Dict[str, Any]] = []
for part in ordered_parts:
section_items: List[Dict[str, Any]] = []
entries: List[Dict[str, Any]] = []
if part == "Факультеты":
faculty_sources = [raw_by_id[g] for g in ["fe", "kl", "km", "lv", "mk", "mu"] if g in raw_by_id]
if faculty_sources:
section_items.append(_aggregate_item(faculty_sources, "before_804", "до 8.04", part))
entries.append(_aggregate_item(faculty_sources, "before_804", "до 8.04", part))
for raw in raw_items:
group_id = str(raw.get("group_id") or "")
if _part_name(classes_df, group_id) != part:
continue
section_items.append(
entries.append(
_build_class_block(
{
"id": group_id,
@@ -296,12 +315,12 @@ def _build_class_report_model(workspace_root: Path, context: Dict[str, Any] | No
)
)
section_items.sort(key=lambda item: (0 if item.get("is_aggregate") else 1, item.get("order", 9999), item.get("label", "")))
if section_items:
sections.append({"id": part, "title": part, "items": section_items})
entries.sort(key=lambda item: (0 if item.get("is_aggregate") else 1, item.get("order", 9999), item.get("label", "")))
if entries:
sections.append({"id": part, "title": part, "entries": entries})
active_section = sections[0]["id"] if sections else ""
active_class = sections[0]["items"][0]["id"] if sections and sections[0]["items"] else ""
active_class = sections[0]["entries"][0]["id"] if sections and sections[0]["entries"] else ""
report = {
"title": "Отчёт по классам",
@@ -341,8 +360,8 @@ def run_stage(workspace_root: Path, context: Dict[str, Any] | None = None) -> Di
"""Generate standalone HTML reports from Jinja templates."""
template_dir = workspace_root / "template"
required = [
template_dir / "report_class.html",
template_dir / "report_group.html",
template_dir / "report_class.jinja",
template_dir / "report_group.jinja",
template_dir / "report.css",
template_dir / "report_class.js",
template_dir / "report_group.js",
@@ -359,7 +378,7 @@ def run_stage(workspace_root: Path, context: Dict[str, Any] | None = None) -> Di
group_report = _build_group_report_model(workspace_root)
class_html = _render_jinja(
template_dir / "report_class.html",
template_dir / "report_class.jinja",
{
"title": class_report["title"],
"hint": f"Период: {class_report['period']['start']} — {class_report['period']['end']}",
@@ -370,7 +389,7 @@ def run_stage(workspace_root: Path, context: Dict[str, Any] | None = None) -> Di
},
)
group_html = _render_jinja(
template_dir / "report_group.html",
template_dir / "report_group.jinja",
{
"title": group_report["title"],
"hint": group_report["note"],
@@ -386,7 +405,7 @@ def run_stage(workspace_root: Path, context: Dict[str, Any] | None = None) -> Di
out_group.write_text(group_html, encoding="utf-8")
out_class.write_text(class_html, encoding="utf-8")
print("✓ HTML reports generated")
print("[OK] HTML reports generated")
print(f" - {out_group.name}")
print(f" - {out_class.name}")