rairo commited on
Commit
a9578e3
·
verified ·
1 Parent(s): 54f8fa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -1,8 +1,8 @@
1
  ##############################################################################
2
  # Sozo Business Studio · 10-Jul-2025
3
- # • REFACTORED: Removed lazy-loading to ensure stability on Streamlit.
4
- # • Report generation is now a single, synchronous process.
5
- # • Unified output under a single `st.session_state.bundle` for both modes.
6
  # • This is the complete, unabridged code with no functions skipped.
7
  ##############################################################################
8
 
@@ -290,19 +290,9 @@ def generate_report_bundle(buf: bytes, name: str, ctx: str, key: str):
290
  # 3. Assemble the final report bundle
291
  pdf_bytes = build_pdf(md, chart_paths)
292
 
293
- def _substitute_tags_for_preview(match):
294
- desc = match.group("d").strip()
295
- path = chart_paths.get(desc)
296
- if path:
297
- b64 = base64.b64encode(Path(path).read_bytes()).decode()
298
- return f'<img src="data:image/png;base64,{b64}" style="max-width:100%;">'
299
- return f"*Chart '{desc}' could not be generated.*"
300
-
301
- preview_md = TAG_RE.sub(_substitute_tags_for_preview, md)
302
-
303
  return {
304
- "type": "report", "key": key, "preview_md": preview_md,
305
- "pdf": pdf_bytes, "raw_md": md
306
  }
307
 
308
  # ─── ANIMATION HELPERS ────────────────────────────────────────
@@ -546,7 +536,27 @@ if (bundle := st.session_state.get("bundle")):
546
  if bundle.get("type") == "report":
547
  st.subheader("📄 Generated Report")
548
  with st.expander("View Report", expanded=True):
549
- st.markdown(bundle["preview_md"], unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
550
 
551
  c1, c2 = st.columns(2)
552
  with c1:
 
1
  ##############################################################################
2
  # Sozo Business Studio · 10-Jul-2025
3
+ # • REFACTORED: Implemented a robust rendering loop to fix image display issues.
4
+ # • Uses st.image() for charts instead of embedding HTML in Markdown.
5
+ # • This guarantees rendering on platforms like Hugging Face Spaces.
6
  # • This is the complete, unabridged code with no functions skipped.
7
  ##############################################################################
8
 
 
290
  # 3. Assemble the final report bundle
291
  pdf_bytes = build_pdf(md, chart_paths)
292
 
 
 
 
 
 
 
 
 
 
 
293
  return {
294
+ "type": "report", "key": key, "raw_md": md,
295
+ "charts": chart_paths, "pdf": pdf_bytes
296
  }
297
 
298
  # ─── ANIMATION HELPERS ────────────────────────────────────────
 
536
  if bundle.get("type") == "report":
537
  st.subheader("📄 Generated Report")
538
  with st.expander("View Report", expanded=True):
539
+ # This robust rendering loop iterates through the report text and
540
+ # uses native st.image() for charts, guaranteeing correct display.
541
+ report_md = bundle["raw_md"]
542
+ charts = bundle["charts"]
543
+ last_end = 0
544
+ for match in TAG_RE.finditer(report_md):
545
+ # Render the text that comes before the chart tag
546
+ st.markdown(report_md[last_end:match.start()])
547
+
548
+ # Render the chart using st.image
549
+ desc = match.group("d").strip()
550
+ chart_path = charts.get(desc)
551
+ if chart_path and Path(chart_path).exists():
552
+ st.image(chart_path)
553
+ else:
554
+ st.warning(f"Could not render chart: '{desc}'")
555
+
556
+ last_end = match.end()
557
+
558
+ # Render any remaining text after the last chart
559
+ st.markdown(report_md[last_end:])
560
 
561
  c1, c2 = st.columns(2)
562
  with c1: