"""Centralized CSS + UI primitives for the QDB Knowledge Bot. Importing `inject_styles()` once per page applies the full design system. """ import base64 from functools import lru_cache from pathlib import Path import streamlit as st _LOGO_PATH = Path(__file__).parent / "qdbank_logo.jpeg" @lru_cache(maxsize=1) def _logo_data_uri() -> str: """Load the QDB logo as a base64 data URI for inline HTML use.""" if not _LOGO_PATH.exists(): return "" data = base64.b64encode(_LOGO_PATH.read_bytes()).decode() return f"data:image/jpeg;base64,{data}" _CSS = """ """ def inject_styles() -> None: """Inject the QDB design system CSS into the current Streamlit page.""" st.markdown(_CSS, unsafe_allow_html=True) def brand_header() -> None: """Render the QDB sidebar brand block.""" logo = _logo_data_uri() logo_html = ( f'QDB' if logo else '' ) st.markdown( f"""
{logo_html}
مساعد المعرفة
مؤسسة بنك تنمية المهارات
""", unsafe_allow_html=True, ) def logo_html(size_px: int = 64, with_bg: bool = True) -> str: """Reusable HTML snippet rendering the official QDB logo at a given size.""" logo = _logo_data_uri() if not logo: return f'' bg = "background:white; padding:8px;" if with_bg else "" return ( f'QDB' ) def page_header(title: str, subtitle: str = "") -> None: """Render a styled page title + subtitle.""" st.markdown( f"""
{title}
{subtitle}
""", unsafe_allow_html=True, ) def stat_card(label: str, value: str | int, icon: str = "") -> None: """Render a single stat card.""" st.markdown( f"""
{icon}
{label}
{value}
""", unsafe_allow_html=True, ) def user_badge(name: str, email: str, is_admin: bool) -> None: """Render the user badge in the sidebar.""" initial = (name or email)[0].upper() role_html = '
مشرف
' if is_admin else "" st.markdown( f"""
{initial}
{name}
{email}
{role_html}
""", unsafe_allow_html=True, ) def citation_block(citations: list[dict]) -> None: """Render a styled citations block. Built as a single-line HTML string and rendered via st.html() to avoid Streamlit's markdown processor treating indented HTML as a code block. """ if not citations: return items = [] for i, c in enumerate(citations, 1): sec = c.get("section", "") score = c.get("score", 0) section_html = f' — {sec}' if sec else "" items.append( f'
' f'{i}' f'{c["source_file"]}' f'{section_html}' f'{score:.0%}' f'
' ) html = f'
{"".join(items)}
' try: st.html(html) except AttributeError: st.markdown(html, unsafe_allow_html=True) def empty_state(icon: str, title: str, description: str) -> None: """Render a friendly empty state.""" st.markdown( f"""
{icon}
{title}
{description}
""", unsafe_allow_html=True, ) def divider() -> None: st.markdown('
', unsafe_allow_html=True)