ai_econsult_demo / src /modal_templates.py
Cardiosense-AG's picture
Update src/modal_templates.py
0a5ac10 verified
raw
history blame
2.11 kB
# src/modal_templates.py
# -----------------------------------------------------------------------------
# Safe modal rendering with fallback for older Streamlit versions (<1.52)
# -----------------------------------------------------------------------------
from __future__ import annotations
from typing import Any, Dict, Callable
import json
import streamlit as st
def _get_st():
try:
import streamlit as _st # noqa
return _st
except Exception:
return None
def _dialog_or_fallback(title: str) -> Callable[[Callable[[], None]], None]:
"""Return a callable context renderer that works even if st.dialog() is unsupported."""
st = _get_st()
if st is None:
# completely headless fallback
def no_op(fn: Callable[[], None]) -> None:
fn()
return no_op
# Check if st.dialog exists and is callable
if hasattr(st, "dialog") and callable(getattr(st, "dialog")):
def wrapper(fn: Callable[[], None]) -> None:
try:
with st.dialog(title):
fn()
except Exception:
# Defensive: fallback if context manager fails
st.subheader(title)
fn()
return wrapper
# Streamlit < 1.52: use expander instead
def wrapper(fn: Callable[[], None]) -> None:
st.subheader(title)
with st.expander("Preview", expanded=True):
fn()
return wrapper
def show_consult_note_preview(note_md: str) -> None:
"""Render a consult note preview safely (modal or fallback)."""
st = _get_st()
renderer = _dialog_or_fallback("Consult Note Preview")
def body() -> None:
if st is not None:
st.markdown(note_md)
renderer(body)
def show_837_claim_preview(claim: Dict[str, Any]) -> None:
"""Render a claim JSON preview safely (modal or fallback)."""
st = _get_st()
renderer = _dialog_or_fallback("837 Claim Preview")
def body() -> None:
if st is not None:
st.code(json.dumps(claim, indent=2), language="json")
renderer(body)