Update tools/report_tool.py
Browse files- tools/report_tool.py +64 -25
tools/report_tool.py
CHANGED
|
@@ -1,25 +1,64 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# space/tools/report_tool.py
|
| 2 |
+
import os
|
| 3 |
+
from typing import Optional, Dict, Any
|
| 4 |
+
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from jinja2 import Environment, FileSystemLoader
|
| 7 |
+
|
| 8 |
+
from utils.tracing import Tracer
|
| 9 |
+
from utils.config import AppConfig
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class ReportTool:
|
| 13 |
+
def __init__(self, cfg: AppConfig, tracer: Tracer):
|
| 14 |
+
self.cfg = cfg
|
| 15 |
+
self.tracer = tracer
|
| 16 |
+
|
| 17 |
+
# Resolve the /space/templates directory reliably whether run as script or module
|
| 18 |
+
templates_dir = os.path.abspath(
|
| 19 |
+
os.path.join(os.path.dirname(__file__), "..", "templates")
|
| 20 |
+
)
|
| 21 |
+
self.env = Environment(loader=FileSystemLoader(templates_dir), autoescape=False)
|
| 22 |
+
self.templates_dir = templates_dir # keep for any future file refs
|
| 23 |
+
|
| 24 |
+
def render_and_save(
|
| 25 |
+
self,
|
| 26 |
+
user_query: str,
|
| 27 |
+
sql_preview: Optional[pd.DataFrame],
|
| 28 |
+
predict_preview: Optional[pd.DataFrame],
|
| 29 |
+
explain_images: Dict[str, str],
|
| 30 |
+
plan: Dict[str, Any],
|
| 31 |
+
) -> str:
|
| 32 |
+
"""
|
| 33 |
+
Render a simple HTML report from the Markdown-like template, then save it next to app.py.
|
| 34 |
+
Returns the relative path to the generated HTML file.
|
| 35 |
+
"""
|
| 36 |
+
tmpl = self.env.get_template("report_template.md")
|
| 37 |
+
|
| 38 |
+
html_body = tmpl.render(
|
| 39 |
+
user_query=user_query,
|
| 40 |
+
plan=plan,
|
| 41 |
+
sql_preview=sql_preview.to_markdown(index=False) if isinstance(sql_preview, pd.DataFrame) else "",
|
| 42 |
+
predict_preview=predict_preview.to_markdown(index=False) if isinstance(predict_preview, pd.DataFrame) else "",
|
| 43 |
+
explain_images=explain_images or {},
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Name the output in the working dir (HF Spaces serves from cwd)
|
| 47 |
+
out_name = f"report_{pd.Timestamp.utcnow().strftime('%Y%m%d_%H%M%S')}.html"
|
| 48 |
+
out_path = os.path.abspath(os.path.join(os.getcwd(), out_name))
|
| 49 |
+
|
| 50 |
+
# Link stylesheet relative to the app working dir (matches scaffold layout)
|
| 51 |
+
css_link = "templates/report_styles.css"
|
| 52 |
+
html = f'<link rel="stylesheet" href="{css_link}">\n' + html_body
|
| 53 |
+
|
| 54 |
+
with open(out_path, "w", encoding="utf-8") as f:
|
| 55 |
+
f.write(html)
|
| 56 |
+
|
| 57 |
+
# trace
|
| 58 |
+
try:
|
| 59 |
+
self.tracer.trace_event("report", {"path": out_name})
|
| 60 |
+
except Exception:
|
| 61 |
+
pass
|
| 62 |
+
|
| 63 |
+
# Return a relative path so Gradio can offer it as a download if needed
|
| 64 |
+
return out_name
|