-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
1333 lines (1180 loc) · 51.2 KB
/
Copy pathapp.py
File metadata and controls
1333 lines (1180 loc) · 51.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import pandas as pd
import plotly.express as px
import json
from pathlib import Path
from datetime import datetime
from ui.styles import inject_custom_css, LUCIDE
# ─── Auth constants ───
PASSWORDS = {"admin": "admin123", "analyst": "analyst", "viewer": "viewer"}
ROLE_DISPLAY = {"admin": "Administrator", "analyst": "Data Analyst", "viewer": "Viewer"}
ROLE_DESC = {
"admin": "Full access. View all data, export, manage apps.",
"analyst": "Analyze data, create apps, export CSV/JSON.",
"viewer": "View public dashboards only.",
}
# ─── Session state defaults ───
DEFAULTS = {
"logged_in": False,
"user_role": None,
"pipeline_result": None,
"current_app": None,
"active_filters": None,
"messages": [],
"show_engine": False,
"current_page": "chat",
"uploaded_tables": {}, # {table_name: {"rows": int, "columns": int, "file_name": str}}
}
# ─── Templates ───
TEMPLATES = [
{
"name": "Supplier Performance",
"prompt": (
"Create a supplier performance dashboard with KPIs for total orders, "
"average defect rate, and on-time delivery percentage. Include a bar chart "
"of defect rate by supplier, a line chart of delivery trends over time, "
"a pie chart of orders by region, and a supplier details table. "
"Add filters for region and category."
),
},
{
"name": "Cost Analysis",
"prompt": (
"Create a cost analysis dashboard with KPIs for total cost, average unit "
"cost, and total shipping cost. Include a bar chart of costs by region, "
"a pie chart of cost distribution by category, and a detailed cost table. "
"Add filters for region and shipping mode."
),
},
{
"name": "Quality Control",
"prompt": (
"Build a quality control dashboard with KPIs for average defect rate and "
"total defective orders. Include a bar chart of defect rates by supplier, "
"a scatter plot of defect rate vs unit cost, and a line chart of defect "
"trends over time. Add filters for region and supplier."
),
},
{
"name": "Logistics Overview",
"prompt": (
"Create a logistics dashboard with KPIs for total shipping cost and "
"average delivery days. Include a bar chart of shipping cost by shipping "
"mode, a line chart of order volume over time, and a shipment details "
"table. Add filters for region and shipping mode."
),
},
{
"name": "Regional Analysis",
"prompt": (
"Show me a regional analysis dashboard with KPIs for total orders and "
"average defect rate. Include a bar chart comparing regions by total cost, "
"a pie chart of order distribution by region, and a table of regional "
"metrics. Add filters for category and shipping mode."
),
},
{
"name": "Executive Summary",
"prompt": (
"Build an executive KPI summary with total orders, total cost, average "
"defect rate, and average shipping cost as KPI cards. Include a bar chart "
"of costs by region and a line chart of monthly order trends. "
"Add a region filter."
),
},
]
# ============================================================================
# PIPELINE
# ============================================================================
def call_pipeline(user_prompt: str) -> dict:
from engine.pipeline import run_pipeline
return run_pipeline(
user_message=user_prompt,
existing_app=st.session_state.get("current_app"),
filters=st.session_state.get("active_filters"),
role=st.session_state.get("user_role", "analyst"),
)
def _save_graph_output(result: dict, prompt: str) -> Path:
"""Persist a pipeline result to graphs/<timestamp>.json and return the path."""
graphs_dir = Path("graphs")
graphs_dir.mkdir(exist_ok=True)
ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
out_path = graphs_dir / f"output_{ts}.json"
payload = {
"saved_at": datetime.now().isoformat(),
"prompt": prompt,
"result": result,
}
with open(out_path, "w") as f:
json.dump(payload, f, default=str, indent=2)
return out_path
def process_prompt(prompt: str):
"""Run pipeline, store results, add to chat with inline result."""
st.session_state.messages.append({
"role": "user", "content": prompt,
"timestamp": datetime.now().strftime("%H:%M:%S"),
})
result = call_pipeline(prompt)
st.session_state.pipeline_result = result
st.session_state.current_app = result.get("app_definition")
# Persist to graphs/ folder
_save_graph_output(result, prompt)
gov = result.get("governance", {})
app_def = result.get("app_definition", {})
exec_results = result.get("execution_results", {})
if gov.get("passed", True):
n = len(app_def.get("components", []))
title = app_def.get("app_title", "dashboard")
comps = app_def.get("components", [])
comp_types = [c.get("type", "").replace("_", " ") for c in comps]
type_summary = ", ".join(dict.fromkeys(comp_types)) # unique, ordered
# Check if all components returned empty data
empty_count = 0
for comp in comps:
cid = comp.get("id", "")
comp_result = exec_results.get(cid, {})
data = comp_result.get("data", []) if isinstance(comp_result, dict) else (comp_result if isinstance(comp_result, list) else [])
if not data:
empty_count += 1
if empty_count == n and n > 0:
# All components empty — give an intelligent message
msg = (
f"I built <strong>{title}</strong>, but all {n} queries returned empty results. "
f"This usually means the filters or date range in your request don't match the "
f"available data. Try rephrasing without specific time constraints, or check the "
f"<strong>Your Data</strong> section to see what's loaded."
)
elif empty_count > 0:
msg = (
f"I've built <strong>{title}</strong> with {n} components — "
f"including {type_summary}. "
f"Note: {empty_count} component(s) returned no data — some filters may not "
f"match the dataset. Expand below to explore."
)
else:
msg = (
f"I've built <strong>{title}</strong> with {n} components — "
f"including {type_summary}. "
f"Expand the dashboard below to explore the data interactively."
)
else:
msg = "Governance blocked this request. See the details below for more information."
st.session_state.messages.append({
"role": "assistant", "content": msg,
"timestamp": datetime.now().strftime("%H:%M:%S"),
"pipeline_result": result,
"stream": True, # flag for streaming animation
})
# ============================================================================
# PLOTLY CONFIG
# ============================================================================
PLOTLY_DEFAULTS = dict(
template="simple_white",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font=dict(color="#6b7280", family="DM Sans, -apple-system, sans-serif"),
title_font=dict(color="#111111", size=14),
height=380,
margin=dict(l=40, r=20, t=50, b=40),
)
PLOTLY_GRID = dict(gridcolor="#e5e7eb")
ACCENT_COLORS = ["#3b82f6", "#8b5cf6", "#06b6d4", "#f59e0b", "#10b981", "#ef4444", "#ec4899", "#6366f1"]
# ============================================================================
# DATA HELPER
# ============================================================================
def _get_data(comp_result):
if isinstance(comp_result, dict):
return comp_result.get("data", [])
elif isinstance(comp_result, list):
return comp_result
return []
# ============================================================================
# RENDERERS
# ============================================================================
def _clean_label(col_name: str) -> str:
"""Turn 'total_shipping_cost' into 'Total Shipping Cost'."""
if not col_name:
return col_name
return col_name.replace("_", " ").strip().title()
def _format_kpi_value(val, fmt):
"""Format a KPI value, handling None/NaN gracefully."""
import math
if val is None:
return "—"
try:
val = float(val)
if math.isnan(val) or math.isinf(val):
return "—"
except (TypeError, ValueError):
return str(val)
if fmt == "percentage":
return f"{val:.1f}%"
elif fmt == "currency":
if abs(val) >= 1_000_000:
return f"${val/1_000_000:,.1f}M"
elif abs(val) >= 1_000:
return f"${val/1_000:,.1f}K"
return f"${val:,.2f}"
else:
if abs(val) >= 1_000_000:
return f"{val/1_000_000:,.1f}M"
elif abs(val) >= 10_000:
return f"{val/1_000:,.1f}K"
return f"{val:,.2f}"
def render_kpi(component, data, chart_key=None):
cfg = component.get("config", {})
value_col = cfg.get("value_column", "")
label = cfg.get("metric_name", component.get("title", "KPI"))
fmt = cfg.get("format", "")
if data:
row = data[0]
val = row.get(value_col) if value_col and value_col in row else list(row.values())[0]
display = _format_kpi_value(val, fmt)
else:
display = "—"
st.metric(label=label, value=display)
def _render_empty_state(component):
"""Show a styled empty-state message when a chart has no data."""
title = component.get("title", "Chart")
st.markdown(
f'<div style="text-align:center;padding:40px 16px;color:#9ca3af;font-size:13px">'
f'<p style="font-weight:600;color:#6b7280;margin-bottom:4px">{title}</p>'
f'No data returned — the query filters may not match the dataset.'
f'</div>',
unsafe_allow_html=True,
)
def render_bar_chart(component, data, chart_key=None):
cfg = component.get("config", {})
df = pd.DataFrame(data)
if df.empty:
_render_empty_state(component)
return
x = cfg.get("x_axis", df.columns[0])
y = cfg.get("y_axis", df.columns[1] if len(df.columns) > 1 else df.columns[0])
color_col = None
for c in df.columns:
if c != x and c != y and df[c].dtype == "object":
color_col = c
break
_labels = {x: _clean_label(x), y: _clean_label(y)}
if color_col:
_labels[color_col] = _clean_label(color_col)
fig = px.bar(df, x=x, y=y, color=color_col, title=component.get("title", ""),
barmode="group", color_discrete_sequence=ACCENT_COLORS, labels=_labels)
fig.update_layout(**PLOTLY_DEFAULTS, xaxis=PLOTLY_GRID, yaxis=PLOTLY_GRID)
st.plotly_chart(fig, use_container_width=True, key=chart_key)
def render_table(component, data, chart_key=None):
df = pd.DataFrame(data)
if df.empty:
_render_empty_state(component)
return
cfg = component.get("config", {})
sort_col = cfg.get("sort_column")
if sort_col and sort_col in df.columns:
df = df.sort_values(sort_col, ascending=(cfg.get("sort_order", "asc") == "asc"))
# Clean column headers
df.columns = [_clean_label(c) for c in df.columns]
st.markdown(
f"<p style='font-weight:600;color:#111111;margin-bottom:4px;font-size:14px'>"
f"{component.get('title', 'Table')}</p>",
unsafe_allow_html=True,
)
st.dataframe(df, use_container_width=True, height=380, key=chart_key)
def render_line_chart(component, data, chart_key=None):
df = pd.DataFrame(data)
if df.empty:
_render_empty_state(component)
return
cfg = component.get("config", {})
x = cfg.get("x_axis", df.columns[0])
y = cfg.get("y_axis", df.columns[1] if len(df.columns) > 1 else df.columns[0])
fig = px.line(df, x=x, y=y, title=component.get("title", ""), markers=True,
color_discrete_sequence=ACCENT_COLORS,
labels={x: _clean_label(x), y: _clean_label(y)})
fig.update_layout(**PLOTLY_DEFAULTS, xaxis=PLOTLY_GRID, yaxis=PLOTLY_GRID)
st.plotly_chart(fig, use_container_width=True, key=chart_key)
def render_pie_chart(component, data, chart_key=None):
df = pd.DataFrame(data)
if df.empty:
_render_empty_state(component)
return
cfg = component.get("config", {})
names = cfg.get("names", cfg.get("x_axis", df.columns[0]))
values = cfg.get("values", cfg.get("y_axis", df.columns[1] if len(df.columns) > 1 else df.columns[0]))
fig = px.pie(df, names=names, values=values, title=component.get("title", ""),
color_discrete_sequence=ACCENT_COLORS,
labels={names: _clean_label(names), values: _clean_label(values)})
fig.update_layout(**PLOTLY_DEFAULTS)
fig.update_traces(marker=dict(line=dict(color="#ffffff", width=2)))
st.plotly_chart(fig, use_container_width=True, key=chart_key)
def render_scatter(component, data, chart_key=None):
df = pd.DataFrame(data)
if df.empty:
_render_empty_state(component)
return
cfg = component.get("config", {})
x = cfg.get("x_axis", df.columns[0])
y = cfg.get("y_axis", df.columns[1] if len(df.columns) > 1 else df.columns[0])
fig = px.scatter(df, x=x, y=y, title=component.get("title", ""),
color_discrete_sequence=ACCENT_COLORS,
labels={x: _clean_label(x), y: _clean_label(y)})
fig.update_layout(**PLOTLY_DEFAULTS, xaxis=PLOTLY_GRID, yaxis=PLOTLY_GRID)
st.plotly_chart(fig, use_container_width=True, key=chart_key)
def render_area_chart(component, data, chart_key=None):
df = pd.DataFrame(data)
if df.empty:
_render_empty_state(component)
return
cfg = component.get("config", {})
x = cfg.get("x_axis", df.columns[0])
y = cfg.get("y_axis", df.columns[1] if len(df.columns) > 1 else df.columns[0])
fig = px.area(df, x=x, y=y, title=component.get("title", ""),
color_discrete_sequence=ACCENT_COLORS,
labels={x: _clean_label(x), y: _clean_label(y)})
fig.update_layout(**PLOTLY_DEFAULTS, xaxis=PLOTLY_GRID, yaxis=PLOTLY_GRID)
st.plotly_chart(fig, use_container_width=True, key=chart_key)
def render_metric_highlight(component, data, chart_key=None):
cfg = component.get("config", {})
label = cfg.get("metric_name", component.get("title", "Metric"))
fmt = cfg.get("format", "")
if data:
row = data[0]
value_col = cfg.get("value_column", "")
val = row.get(value_col) if value_col and value_col in row else list(row.values())[0]
display = _format_kpi_value(val, fmt)
else:
display = "—"
st.metric(label=label, value=display)
RENDERERS = {
"kpi_card": render_kpi,
"bar_chart": render_bar_chart,
"table": render_table,
"line_chart": render_line_chart,
"pie_chart": render_pie_chart,
"scatter_plot": render_scatter,
"area_chart": render_area_chart,
"metric_highlight": render_metric_highlight,
}
# ============================================================================
# GOVERNANCE DISPLAY HELPERS
# ============================================================================
def _indicator(passed: bool) -> str:
"""Return a colored dot HTML span."""
cls = "ind-pass" if passed else "ind-fail"
return f'<span class="{cls}">●</span>'
def _render_gov_checks(gov: dict):
"""Render governance check rows using HTML (no emojis)."""
gcols = st.columns(3)
sql_safety = gov.get("sql_safety", {})
col_access = gov.get("column_access", {})
comp_perms = gov.get("component_permissions", {})
with gcols[0]:
safe = sql_safety.get("safe", True)
st.markdown(
f'<p class="check-row">{_indicator(safe)} <strong>SQL Safety</strong></p>',
unsafe_allow_html=True,
)
with gcols[1]:
allowed = col_access.get("allowed", True)
st.markdown(
f'<p class="check-row">{_indicator(allowed)} <strong>Column Access</strong></p>',
unsafe_allow_html=True,
)
blocked_cols = col_access.get("blocked_columns", [])
if blocked_cols:
st.markdown(
f'<p class="check-row" style="font-size:11px">Blocked: {", ".join(blocked_cols)}</p>',
unsafe_allow_html=True,
)
with gcols[2]:
comp_ok = comp_perms.get("allowed", True)
st.markdown(
f'<p class="check-row">{_indicator(comp_ok)} <strong>Component Perms</strong></p>',
unsafe_allow_html=True,
)
for check in gov.get("checks", []):
is_pass = check.get("passed", check.get("status") == "pass")
name = check.get("name", "?")
detail = check.get("message", check.get("details", ""))
st.markdown(
f'<p class="check-row">{_indicator(is_pass)} <strong>{name}</strong>: {detail}</p>',
unsafe_allow_html=True,
)
# ============================================================================
# ENGINE PANEL
# ============================================================================
def _render_engine_panel(result):
"""Tabbed engine panel below dashboard."""
app_def = result.get("app_definition", {})
exec_results = result.get("execution_results", {})
gov = result.get("governance", {})
validation = result.get("validation", {})
st.markdown("<hr>", unsafe_allow_html=True)
st.markdown(
"<h4 style='margin-bottom:4px;font-size:16px;font-weight:600'>Engine View</h4>",
unsafe_allow_html=True,
)
# Pipeline stepper
st.markdown("""
<div class="stepper">
<span class="step done"><span class="step-dot"></span> Parse</span>
<span class="step-line"></span>
<span class="step done"><span class="step-dot"></span> Execute</span>
<span class="step-line"></span>
<span class="step done"><span class="step-dot"></span> Validate</span>
<span class="step-line"></span>
<span class="step done"><span class="step-dot"></span> Govern</span>
</div>
""", unsafe_allow_html=True)
tab_sql, tab_data, tab_gov, tab_audit = st.tabs(["SQL", "Data", "Governance", "Audit"])
with tab_sql:
for comp in app_def.get("components", []):
title = comp.get("title", comp.get("id", ""))
ctype = comp.get("type", "?")
st.markdown(f"**{title}** `{ctype}`")
st.code(comp.get("sql_query", "-- No SQL"), language="sql")
with tab_data:
for comp in app_def.get("components", []):
cid = comp.get("id", "")
title = comp.get("title", cid)
comp_result = exec_results.get(cid, {})
data = _get_data(comp_result)
st.markdown(f"**{title}**")
if data:
st.dataframe(pd.DataFrame(data).head(20), use_container_width=True)
if isinstance(comp_result, dict):
st.markdown(
f'<p class="check-row" style="font-size:11px">'
f'Rows: {comp_result.get("row_count", len(data))}</p>',
unsafe_allow_html=True,
)
else:
st.markdown(
'<p class="check-row" style="font-size:11px">No data returned</p>',
unsafe_allow_html=True,
)
for vc in validation.get("components", []):
if vc.get("id") == cid:
ok = vc.get("status") == "success"
st.markdown(
f'<p class="check-row">{_indicator(ok)} '
f'{vc.get("explanation", vc.get("status", "?"))}</p>',
unsafe_allow_html=True,
)
with tab_gov:
_render_gov_checks(gov)
with st.expander("Full Governance JSON"):
st.json(gov)
with tab_audit:
audit_id = gov.get("audit_entry_id", "N/A")
st.markdown(f"**Audit Entry ID:** `{audit_id}`")
st.markdown(f"**Role:** {gov.get('role', '?')} ({gov.get('role_display_name', '?')})")
passed = gov.get("passed", True)
st.markdown(
f'<p class="check-row">{_indicator(passed)} '
f'<strong>{"Passed" if passed else "Blocked"}</strong></p>',
unsafe_allow_html=True,
)
pii = gov.get("pii_detected", False)
st.markdown(f"**PII Detected:** {'Yes — redacted' if pii else 'None'}")
warnings = gov.get("warnings", [])
if warnings:
st.markdown("**Warnings:**")
for w in warnings:
st.markdown(
f'<p class="check-row"><span class="ind-warn">●</span> {w}</p>',
unsafe_allow_html=True,
)
with st.expander("Full Pipeline JSON"):
st.json(result)
# ============================================================================
# INLINE DASHBOARD RENDERER
# ============================================================================
def _render_inline_dashboard(result, dashboard_key="dash"):
"""Render a complete dashboard inline in a chat message."""
app_def = result.get("app_definition", {})
exec_results = result.get("execution_results", {})
gov = result.get("governance", {})
overview = result.get("overview", {})
passed = gov.get("passed", True)
role_display = gov.get("role_display_name", gov.get("role", "?"))
# Build narration lookup: component_id → narration text
narration_map = {}
for nc in overview.get("components", []):
nid = nc.get("id", "")
ntxt = nc.get("narration", "")
if nid and ntxt:
narration_map[nid] = ntxt
# ── Governance badge ──
if passed:
st.markdown(
f'<span class="gov-badge gov-pass">Passed · {role_display}</span>',
unsafe_allow_html=True,
)
else:
st.markdown(
f'<span class="gov-badge gov-fail">Blocked · {role_display}</span>',
unsafe_allow_html=True,
)
# ── Governance blocked ──
if not passed:
blocked_reasons = gov.get("blocked_reasons", [])
for reason in blocked_reasons or ["Access denied for this role."]:
st.markdown(
f'<div class="gov-badge gov-fail" style="display:block;margin:4px 0;'
f'border-radius:6px;padding:8px 12px">{reason}</div>',
unsafe_allow_html=True,
)
return
# ── Overview summary ──
summary = overview.get("summary", "")
if summary:
st.markdown(
f'<div class="narration-summary">{summary}</div>',
unsafe_allow_html=True,
)
# ── Render components ──
components = app_def.get("components", [])
kpis = [c for c in components if c.get("type") in ("kpi_card", "metric_highlight")]
charts = [c for c in components if c.get("type") not in ("kpi_card", "metric_highlight")]
if kpis:
for row_start in range(0, len(kpis), 4):
row_kpis = kpis[row_start : row_start + 4]
kpi_cols = st.columns(len(row_kpis))
for col, comp in zip(kpi_cols, row_kpis):
with col:
cid = comp.get("id", "")
data = _get_data(exec_results.get(cid, {}))
renderer = RENDERERS.get(comp.get("type"))
if renderer:
renderer(comp, data, chart_key=f"{dashboard_key}_{cid}")
narration = narration_map.get(cid, "")
if narration:
st.markdown(
f'<div class="narration-component">{narration}</div>',
unsafe_allow_html=True,
)
if charts:
for i in range(0, len(charts), 2):
row_comps = charts[i : i + 2]
cols = st.columns(len(row_comps))
for col, comp in zip(cols, row_comps):
with col:
cid = comp.get("id", "")
ctype = comp.get("type", "")
data = _get_data(exec_results.get(cid, {}))
renderer = RENDERERS.get(ctype)
if renderer:
renderer(comp, data, chart_key=f"{dashboard_key}_{cid}")
else:
st.info(f"Unsupported: {ctype}")
narration = narration_map.get(cid, "")
if narration:
st.markdown(
f'<div class="narration-component">{narration}</div>',
unsafe_allow_html=True,
)
# ── Governance report ──
with st.expander("Governance Report"):
_render_gov_checks(gov)
# ============================================================================
# LOGIN SCREEN
# ============================================================================
def render_login_screen():
"""Minimal login screen — logo, role picker, password, go."""
ROLE_OPTIONS = ["Data Analyst", "Administrator", "Viewer"]
ROLE_KEY_MAP = {"Administrator": "admin", "Data Analyst": "analyst", "Viewer": "viewer"}
# ── Scoped CSS ──
st.markdown("""<style>
section[data-testid="stSidebar"],
[data-testid="stSidebarCollapsedControl"],
div[data-testid="stBottom"],
div[data-testid="stDecoration"],
header[data-testid="stHeader"],
div[data-testid="stToolbar"],
#MainMenu, footer {
display: none !important;
visibility: hidden !important;
height: 0 !important;
overflow: hidden !important;
}
.stMainBlockContainer {
max-width: 320px !important;
margin: 0 auto !important;
padding-top: 14vh !important;
}
.stMainBlockContainer [data-testid="stVerticalBlock"] {
align-items: center !important;
gap: 0.35rem !important;
}
.stMainBlockContainer .stMarkdown { width: 100% !important; }
.stMainBlockContainer .stSelectbox,
.stMainBlockContainer .stTextInput,
.stMainBlockContainer .stButton { width: 100% !important; }
/* Inputs */
.stSelectbox > div > div {
border-radius: 10px !important;
min-height: 44px !important;
font-size: 14px !important;
border: 1px solid #e5e7eb !important;
background: #ffffff !important;
}
.stSelectbox > div > div:focus-within {
border-color: #9ca3af !important;
box-shadow: 0 0 0 3px rgba(0,0,0,0.03) !important;
}
.stSelectbox label { display: none !important; }
.stTextInput > div,
.stTextInput > div > div {
width: 100% !important;
background: transparent !important;
background-color: transparent !important;
border: none !important;
box-shadow: none !important;
padding: 0 !important;
}
.stTextInput input {
border-radius: 10px !important;
height: 44px !important;
min-height: 44px !important;
font-size: 14px !important;
padding: 0 14px !important;
border: 1px solid #e5e7eb !important;
background: #ffffff !important;
background-color: #ffffff !important;
color: #111111 !important;
width: 100% !important;
box-sizing: border-box !important;
}
.stTextInput input:focus {
border-color: #9ca3af !important;
box-shadow: 0 0 0 3px rgba(0,0,0,0.03) !important;
outline: none !important;
}
.stTextInput input::placeholder { color: #9ca3af !important; }
.stTextInput label { display: none !important; }
.stTextInput button { display: none !important; }
/* Button — target ALL buttons on login page to force dark fill */
.stMainBlockContainer .stButton > button,
.stMainBlockContainer .stButton > button[kind="primary"],
.stMainBlockContainer [data-testid="baseButton-primary"],
.stMainBlockContainer [data-testid="baseButton-primaryFormSubmit"] {
width: 100% !important;
height: 44px !important;
background-color: #111111 !important;
background: #111111 !important;
color: #ffffff !important;
border-radius: 10px !important;
font-size: 14px !important;
font-weight: 500 !important;
border: none !important;
margin-top: 6px !important;
cursor: pointer !important;
}
.stMainBlockContainer .stButton > button:hover,
.stMainBlockContainer [data-testid="baseButton-primary"]:hover {
background-color: #333333 !important;
background: #333333 !important;
color: #ffffff !important;
}
</style>""", unsafe_allow_html=True)
# ── Logo ──
st.markdown("""
<div style="text-align:center;margin-bottom:32px">
<div style="width:48px;height:48px;background:#111111;border-radius:12px;
display:inline-flex;align-items:center;justify-content:center;margin-bottom:16px">
<svg width="22" height="22" viewBox="0 0 24 24" fill="none">
<path d="M12 2L21 7V17L12 22L3 17V7L12 2Z" stroke="white" stroke-width="1.5"/>
</svg>
</div>
<div style="font-size:26px;font-weight:700;color:#111111;letter-spacing:-0.5px;
font-family:'DM Sans',-apple-system,sans-serif">StackForge</div>
</div>
""", unsafe_allow_html=True)
# ── Role dropdown (no label — the dropdown speaks for itself) ──
selected_display = st.selectbox(
"Role",
options=ROLE_OPTIONS,
index=0,
key="login_role_select",
label_visibility="collapsed",
)
role_key = ROLE_KEY_MAP.get(selected_display, "analyst")
# ── Password (no label — placeholder is enough) ──
pwd = st.text_input(
"Password",
type="password",
key="login_pwd",
label_visibility="collapsed",
placeholder="Password",
)
if st.session_state.get("login_error"):
st.markdown(
'<p style="color:#dc2626;font-size:12px;text-align:center;margin:0">Incorrect password</p>',
unsafe_allow_html=True,
)
# ── Sign in button ──
if st.button("Sign in", key="login_btn", type="primary", use_container_width=True):
if pwd == PASSWORDS.get(role_key, ""):
st.session_state.logged_in = True
st.session_state.user_role = role_key
st.session_state.messages = []
st.session_state.pipeline_result = None
st.session_state.current_app = None
st.session_state.login_error = False
st.rerun()
else:
st.session_state.login_error = True
st.rerun()
# ── Footer — single line ──
st.markdown(
'<p style="text-align:center;color:#c0c0c0;font-size:11px;margin-top:24px">'
'HackUSU 2026</p>',
unsafe_allow_html=True,
)
# ============================================================================
# MAIN ENTRY POINT
# ============================================================================
def main():
st.set_page_config(
page_title="StackForge",
layout="wide",
initial_sidebar_state="expanded",
)
inject_custom_css(st)
# ── Initialize session state ──
for k, v in DEFAULTS.items():
if k not in st.session_state:
st.session_state[k] = v
# ── STATE 1: Not logged in ──
if not st.session_state["logged_in"]:
render_login_screen()
return # HARD RETURN — nothing else renders
# ── STATE 2: Logged in — everything below is authenticated-only ──
# ── SIDEBAR ──
with st.sidebar:
st.markdown('''
<div class="sidebar-brand">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" style="flex-shrink:0">
<path d="M12 2L21 7V17L12 22L3 17V7L12 2Z" stroke="#111111" stroke-width="1.5"/>
</svg>
<span>StackForge</span>
</div>
''', unsafe_allow_html=True)
role = st.session_state["user_role"]
role_colors = {"admin": "#111111", "analyst": "#3b82f6", "viewer": "#6b7280"}
badge_color = role_colors.get(role, "#6b7280")
st.markdown(
f'<div class="role-badge">'
f'<span class="role-dot" style="background:{badge_color}"></span>'
f'{ROLE_DISPLAY.get(role, role)}</div>',
unsafe_allow_html=True,
)
st.markdown("<hr>", unsafe_allow_html=True)
# ── Home ──
if st.button("Home", key="home_btn", use_container_width=True):
if st.session_state.current_page != "chat":
st.session_state.current_page = "chat"
st.rerun()
# ── Templates ──
st.markdown('<div class="section-label">Quick start</div>', unsafe_allow_html=True)
for idx, tmpl in enumerate(TEMPLATES):
if st.button(tmpl["name"], key=f"tmpl-{idx}", use_container_width=True):
with st.spinner("Building..."):
process_prompt(tmpl["prompt"])
st.rerun()
st.markdown("<hr>", unsafe_allow_html=True)
# ── Data Sources ──
from data.sample_data_loader import get_available_tables, register_uploaded_csv, remove_table
current_tables = get_available_tables()
uploaded = st.session_state.get("uploaded_tables", {})
st.markdown('<div class="section-label">Your Data</div>', unsafe_allow_html=True)
for tbl in current_tables:
if tbl not in uploaded:
continue
info = uploaded[tbl]
st.markdown(
f'<div style="display:flex;align-items:center;justify-content:space-between;'
f'padding:6px 10px;background:#f0fdf4;border:1px solid #bbf7d0;'
f'border-radius:8px;margin-bottom:4px;font-size:12px">'
f'<span style="color:#166534;font-weight:600">'
f'{LUCIDE["database"]} {tbl}</span>'
f'<span style="color:#6b7280">{info["rows"]} rows</span>'
f'</div>',
unsafe_allow_html=True,
)
# ── CSV Upload ──
csv_files = st.file_uploader(
"Upload CSV",
type=["csv"],
accept_multiple_files=True,
key="csv_uploader",
label_visibility="collapsed",
)
if csv_files:
for uploaded_file in csv_files:
file_name = uploaded_file.name
table_name_check = file_name.replace(".csv", "").lower().replace(" ", "_").replace("-", "_")
if table_name_check in uploaded:
continue
try:
df = pd.read_csv(uploaded_file)
table_name = register_uploaded_csv(file_name, df)
st.session_state.uploaded_tables[table_name] = {
"rows": len(df),
"columns": len(df.columns),
"file_name": file_name,
}
st.session_state.pipeline_result = None
st.session_state.current_app = None
st.rerun()
except Exception as e:
st.error(f"Failed to load {file_name}: {e}")
st.markdown("<hr>", unsafe_allow_html=True)
# ── Engine toggle ──
st.session_state.show_engine = st.toggle(
"Show Engine", value=st.session_state.show_engine
)
# ── Navigation ──
st.markdown('<div class="section-label">Navigation</div>', unsafe_allow_html=True)
if st.button("Graph History", key="graph_history_btn", use_container_width=True):
if st.session_state.current_page != "graph_history":
st.session_state.current_page = "graph_history"
st.rerun()
if role == "admin":
if st.button("Audit History", key="audit_history_btn", use_container_width=True):
if st.session_state.current_page != "audit_history":
st.session_state.current_page = "audit_history"
st.rerun()
# ── Sign out at bottom (subtle) ──
st.markdown('<div style="height:24px"></div>', unsafe_allow_html=True)
if st.button("Sign out", key="logout_btn"):
st.session_state["logged_in"] = False
st.session_state["user_role"] = None
st.rerun()
# ── Footer ──
st.markdown(
'<div class="sidebar-footer">StackForge v1.0</div>',
unsafe_allow_html=True,
)
# ── Page routing ──
if st.session_state.current_page == "audit_history" and role == "admin":
_render_audit_history()
elif st.session_state.current_page == "graph_history":
_render_graph_history()
else:
# ── Layout: chat + optional engine right panel ──
_engine_on = st.session_state.show_engine and st.session_state.get("pipeline_result")
if _engine_on:
st.markdown("""<style>
.stMainBlockContainer { max-width: 100% !important; padding-left: 1rem !important; padding-right: 1rem !important; }
</style>""", unsafe_allow_html=True)
_chat_col, _engine_col = st.columns([3, 2], gap="medium")
else:
_chat_col, _engine_col = st.container(), None
with _chat_col:
_render_main_content()
# ── Right engine panel ──
if _engine_col is not None:
with _engine_col:
st.markdown(
f'<div class="engine-panel-header">{LUCIDE["sparkles"]} '
f'<strong>Engine View</strong></div>',
unsafe_allow_html=True,
)
_render_engine_panel(st.session_state.pipeline_result)