A enterprise-grade Streamlit application for Manufacturing OEE (Overall Equipment Effectiveness) Conversational Analytics, powered by Snowflake Cortex Agent REST API, Snowflake Semantic Views, and database-backed application management.
The application consists of two independent core workflows: Conversational Analytics Chat Flow and Sidebar Navigation & Admin Access Control Flow.
User Question / Suggested Chip Click
│
▼
Snowflake Cortex Agent REST API
(POST /api/v2/cortex/agent:run SSE Stream)
│
├──► Live Status Progress Updates ("🧠 Analyzing...", "⚡ Executing SQL...", "📊 Rendering chart...")
│
▼
SSE Event Collector & Delta Stream Processor
(collect_response() in services/cortex_agent.py)
│
├───────────────────────────────┬───────────────────────────────┐
│ │ │
▼ ▼ ▼
Answer Text & Badges Vega-Lite Chart Spec Executed SQL & Data Table
(format_oee_markdown()) (st.vega_lite_chart()) (Collapsible Expander Blocks)
│ │ │
└───────────────────────────────┴───────────────────────────────┘
│
▼
Streamlit Chat Interface
│
▼
PDF Report Conversation Export
(services/pdf_generator.py)
Sidebar Navigation Radio Selection
│
▼
Admin Privilege Verification
(check_is_admin() via SP_TRAKSYS_IS_ADMIN)
│
├─────────────────────────────────────────┐
│ │
▼ ▼
[If Admin = True] [If Admin = False]
Full Settings Page Access Settings Navigation Option
(ui/settings_page.py) Hidden / Restricted
-
REST SSE Streaming Integration: Communicates directly with Snowflake's
/api/v2/cortex/agent:runREST endpoint using native OAuth tokens mounted in Snowflake Container Runtime (/snowflake/session/token) or active Snowpark session contexts. - Live Status Feedback: Displays dynamic real-time progress indicators (🧠 Analyzing question..., 🛠️ Formulating SQL query..., ⚡ Executing query on Snowflake..., ✍️ Synthesizing insights...) that automatically disappear before rendering final answer blocks.
-
Sanitized Response Formatting:
- Automatically cleans mojibake encoding artifacts (
×$\rightarrow$ ×). - Formats section headers with relevant emoji icons (
oee-section-header). - Highlights percentage metrics in clean neutral badge spans (
oee-badge-value). - Deduplicates repetitive assistant response paragraphs.
- Automatically cleans mojibake encoding artifacts (
- Database View Driven: Queries
{DB}.{ANALYTICS_SCHEMA}.VW_OEE_KPI_CARDSvia active Snowflake session. - Period-over-Period Comparison: Displays current period metrics (OEE, Availability, Performance, Quality, Downtime Hours) compared against previous period values.
- Date Badges & Custom Delta Chips:
- Renders explicit comparison dates (
latest_datevs.previous_date). - Displays direction arrows and delta chips (
up,down, orno change). - Applies inverse delta coloring to Downtime Hours (downtime increases rendered in red).
- Displays neutral text (
delta_color="off") without direction arrows when metric changes are zero (abs(diff) < 0.05).
- Renders explicit comparison dates (
- Admin Privilege Access Control: Access to the
⚙️ Settingspage is restricted strictly to active administrators verified via stored procedure{DB}.{APP_SCHEMA}.SP_TRAKSYS_IS_ADMIN. - Tabbed Settings Page (
ui/settings_page.py):- 💡 Suggested Questions: Add, edit question text, change display order, or soft-delete sample question chips shown at the top of the chat assistant.
- 🖼️ Logo Management: Upload custom PNG/JPG logo images saved directly into Snowflake DB as binary blobs (
BINARYdata type). - ⚙️ Connection & Agent: Configure Semantic View Name, Warehouse Name, Cortex Analyst Tool Name, Orchestration Model Name, Historical Response Context Count, and PDF Export Filename Template.
- 🏷️ Header & Banner: Customize the hero banner title and subtitle displayed at the top of the main page.
- 👥 Admin Users: Add new admin usernames, toggle active/inactive privileges, or remove administrator records.
- Flash Messages: Uses session state flash messaging (
_flash()) to render success or warning banners cleanly across Streamlit script reruns.
- Custom Branding: Exports the full conversation thread to a PDF report featuring company logo, custom banner title, subtitle, and download timestamp.
- Rich Elements: Renders markdown formatted text, executed SQL code blocks, high-resolution chart images, and structured data tables using ReportLab.
-
Dynamic Date Filenames: Auto-replaces placeholders in settings templates (e.g.,
OEE_Conversation_Report_{YYYYMMDD}.pdf$\rightarrow$ OEE_Conversation_Report_20260301.pdf).
- Self-Contained Spec Rendering: Renders chart specifications returned by Cortex Agent using
st.vega_lite_chart()with multi-color categorical palettes and mouseover pop-out hover animations. - Time-Series Continuous Line Safeguards: Automatically detects date/time temporal fields and prevents them from being assigned as nominal color fields on line/area charts, eliminating timestamp epoch legends and broken line charts.
- Role: Coordinates session state, page configuration, custom CSS injection, sidebar navigation, user authentication checks, and layout orchestration.
- Key Functions & Responsibilities:
- Sets up global Streamlit configuration (
st.set_page_config) and applies enterprise brand styles (BRANDCSS theme). - Initializes database settings, custom branding logo, and suggested questions from Snowflake stored procedures on startup.
- Verifies whether the logged-in user has active admin privileges via
check_is_admin()and conditionally displays the⚙️ Settingsnavigation radio button. - Loads Plant Performance Overview KPI metrics from Snowflake view
{DB}.{ANALYTICS_SCHEMA}.VW_OEE_KPI_CARDSand passes them torender_kpi_cards(). - Manages chat prompt state (
st.chat_input, button callbacks viaon_click=set_pending_question), auto-scrolls to the conversation section when queries run, and invokescall_agent()andcollect_response()with live progress callbacks. - Renders sidebar export options for downloading full conversation reports as styled PDFs.
- Sets up global Streamlit configuration (
- Role: Centralizes database parameters and schema names (
APP_SCHEMA,DB,TRACKSYS_SCHEMA,ANALYTICS_SCHEMA).
- Role: Handles REST API communication with Snowflake Cortex Agent, SSE event streaming, message text formatting, and Vega-Lite chart rendering.
-
Key Functions & Responsibilities:
-
get_agent_auth_config(): Resolves Snowflake host and OAuth tokens mounted in Snowflake Container Runtime (/snowflake/session/token) or active Snowpark connections. -
call_agent(messages): Constructs payload JSON with orchestration model (claude-sonnet-4-5), semantic view resource, warehouse, and tools (cortex_analyst_text_to_sql,sql_exec,data_to_chart). SendsPOSTrequests tohttps://{host}/api/v2/cortex/agent:runwith SSE streaming (stream=True). Truncates chat history based on settinghistory_countto ensure context windows always begin cleanly with ausermessage. -
collect_response(events, status_callback): Processes SSE streaming events. Invokesstatus_callbackwith user progress updates (🧠 Analyzing question..., 🛠️ Formulating SQL query..., ⚡ Executing query on Snowflake..., ✍️ Synthesizing insights...). Filters out internal agent scratchpad reasoning/thinking events (response.thinking). Extracts answer deltas (response.text.delta), queried tool result DataFrames (response.table), chart specs (response.chart), and follow-up queries (response.suggested_queries). -
format_oee_markdown(text): Converts standalone section titles into styled headers with emoji icons (oee-section-header) and wraps percentage values in neutral badge spans (oee-badge-value). -
clean_encoding_artifacts(text): Replaces mojibake encoding glitches (×$\rightarrow$ ×,â��$\rightarrow$ –). -
deduplicate_paragraphs(text): Removes duplicate repeated paragraphs from assistant outputs. -
render_chart(spec_or_fig, df, key): Formats and displays Vega-Lite JSON specs viast.vega_lite_chart(). Enforces multi-color categorical scale ranges (#242B6B,#E15241,#A63A96,#E0A438), donut inner radius formatting (innerRadius=55), hover selection parameters, and safeguards time-series line charts against nominal color field assignments.
-
- Role: Executes stored procedures on Snowflake database to manage application parameters, suggested questions, and admin users.
- Key Functions & Responsibilities:
check_is_admin(username): Invokes{DB}.{APP_SCHEMA}.SP_TRAKSYS_IS_ADMIN(username)to verify if the active user possesses active admin rights.load_admin_users_from_db(),save_admin_user_to_db(username, is_active),delete_admin_user_from_db(username): Invokes admin stored procedures (SP_TRAKSYS_GET_ADMIN_USERS,SP_TRAKSYS_SAVE_ADMIN_USER,SP_TRAKSYS_DELETE_ADMIN_USER).load_app_settings_from_db(),save_app_setting_to_db(key, val): Manages configuration key-value strings stored inREF_TRAKSYS_SETTINGSviaSP_TRAKSYS_GET_APP_SETTINGSandSP_TRAKSYS_SAVE_APP_SETTING.save_app_logo_to_db(logo_bytes): Converts binary image bytes to hexadecimal strings and persists them as native SnowflakeBINARYblobs viaSP_TRAKSYS_SAVE_APP_SETTING_BLOB.load_suggested_questions_from_db(),save_suggested_question_to_db(q_id, text, order),delete_suggested_question_from_db(q_id): Manages suggested sample question chips viaSP_TRAKSYS_GET_QUESTIONS,SP_TRAKSYS_SAVE_QUESTION, andSP_TRAKSYS_DELETE_QUESTION._row_val(): Provides case-insensitive column lookups for DataFrame result rows returned by Snowflake stored procedures.
- Role: Auto-detects Snowflake runtime environments and establishes active connections.
- Key Functions & Responsibilities:
get_snowflake_session(): First checks for Streamlit in Snowflake active session (snowflake.snowpark.context.get_active_session()). If unavailable, attempts container token detection or falls back to Snowpark session connection.get_snowflake_token(): Reads container runtime OAuth tokens mounted at/snowflake/session/token.
- Role: Formats and builds downloadable PDF conversation reports.
- Key Functions & Responsibilities:
generate_conversation_pdf(messages, logo_bytes, title, subtitle): Uses ReportLabSimpleDocTemplateandParagraphflowables to build multi-page PDF documents.- Embeds custom company logo images, document headers, subtitle, and dynamic date timestamps.
- Iterates through chat turns, converting markdown text to HTML, formatting SQL query blocks in code styling, converting Vega-Lite/Plotly chart specs into high-resolution PNG images via PIL/Vega-Altair, and rendering data tables.
- Role: Provides administrative settings controls divided across tabs.
- Key Functions & Responsibilities:
- Renders tabbed interface:
💡 Suggested Questions,🖼️ Logo,⚙️ Connection & Agent,🏷️ Header & Banner, and👥 Admin Users. - Uses
_flash(kind, message)and_render_flash()to stash notification messages in session state across Streamlit script reruns. - Clears widget keys (
sq_text_in_,sq_order_in_) when reloading questions from the database to force input fields to refresh with database rows. - Provides inputs for managing sample questions, uploading PNG/JPG logo images, modifying Cortex Agent DB parameters, setting hero banner text, and toggling active/inactive status for admin usernames.
- Renders tabbed interface:
- Role: Renders custom HTML/CSS KPI summary cards, suggested question buttons, and metric table conditional formatting.
- Key Functions & Responsibilities:
render_kpi_cards(kpi_data, brand): Uses custom HTML/CSS grid layout (_KPI_CSS,.kpi-strip,.kpi-grid,.kpi-cell) to render OEE, Availability, Performance, Quality, and Downtime Hours cards. Shows comparison date subtitles (latest_date,previous_date), period progress bars, and delta chips (up,down, orflat). Formats Downtime Hours as rounded integers and applies inverse delta coloring for downtime increases.render_sample_questions(on_click_callback): Dynamically loads sample question buttons fromst.session_state.suggested_questions_listand renders them as compact pill buttons (use_container_width=False) with Streamliton_clickcallbacks.style_dataframe_metrics(df, metric_colors): Applies pandas CSS styling to highlight data table metric columns matching user-configured color thresholds when enabled.
- Role: Generates fallback Plotly figure specifications when data DataFrames are returned without explicit Vega-Lite specs.
- Key Functions & Responsibilities:
generate_chart(question, df): Inspects DataFrame column types and constructs appropriate Plotly bar, line, pie, or scatter figures formatted with brand navy color palettes (#242B6B).
The database initialization script (data/init_settings_db.sql) manages settings, questions, and admin users via dedicated tables and stored procedures:
REF_TRAKSYS_QUESTIONS: Stores suggested question text, display order, active flag, andCREATED_BY/UPDATED_BYaudit tracking.REF_TRAKSYS_SETTINGS: Stores key-value configuration strings and binary logo blobs (SETTING_BLOB BINARY).REF_TRAKSYS_ADMIN_USERS: Stores admin usernames (USERNAME VARCHAR PRIMARY KEY), active status (IS_ACTIVE BOOLEAN), and audit fields.
SP_TRAKSYS_GET_APP_SETTINGS(): Returns key-value pairs and logo blob indicators.SP_TRAKSYS_SAVE_APP_SETTING(P_KEY, P_VAL, P_USER): Merges setting string values.SP_TRAKSYS_SAVE_APP_SETTING_BLOB(P_KEY, P_BLOB, P_USER): Merges binary image blobs.SP_TRAKSYS_GET_QUESTIONS(): Returns active suggested questions ordered byDISPLAY_ORDER.SP_TRAKSYS_SAVE_QUESTION(P_ID, P_TEXT, P_ORDER, P_USER): Inserts or updates suggested questions.SP_TRAKSYS_DELETE_QUESTION(P_ID, P_USER): Soft-deletes suggested questions.SP_TRAKSYS_IS_ADMIN(P_USERNAME): ReturnsBOOLEANindicating if the user has active admin privileges.SP_TRAKSYS_GET_ADMIN_USERS(): Returns all admin user records.SP_TRAKSYS_SAVE_ADMIN_USER(P_USERNAME, P_ACTIVE, P_USER): Adds or updates admin users.SP_TRAKSYS_DELETE_ADMIN_USER(P_USERNAME, P_USER): Removes an admin user.
Ensure dependencies are installed:
pip install -r requirements.txtLaunch the Streamlit application:
streamlit run app.py