-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
311 lines (256 loc) · 10.2 KB
/
app.py
File metadata and controls
311 lines (256 loc) · 10.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
"""
Streamlit App - Terminal Style SQL Interface
"""
import streamlit as st
import pandas as pd
from analytics_engine import sql_agent, execute_query, get_complete_schema_dict
from viz_engine import create_visualization
# --- CONFIGURATION & STYLING ---
st.set_page_config(
page_title="SQL_TERMINAL",
page_icon="💻",
layout="wide",
initial_sidebar_state="expanded"
)
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;700&display=swap');
.stApp {
background-color: #0e1117;
font-family: 'Fira Code', monospace;
}
/* Global Font Enforcement */
h1, h2, h3, h4, h5, h6, p, .stMarkdown, .stTextInput > div > div > input, .stTextArea textarea {
font-family: 'Fira Code', monospace !important;
color: #e6edf3;
}
h1, h2, h3 {
color: #00ff41 !important;
}
/* Buttons */
.stButton button {
background-color: #21262d;
color: #00ff41;
border: 1px solid #00ff41;
border-radius: 0px;
font-family: 'Fira Code', monospace !important;
}
.stButton button:hover {
background-color: #00ff41;
color: #000000;
border: 1px solid #00ff41;
}
/* Text Area (SQL Editor) */
.stTextArea textarea {
background-color: #0d1117;
color: #ff7b72;
border: 1px solid #30363d;
}
[data-testid="stSidebar"] {
background-color: #010409;
border-right: 1px solid #30363d;
}
code {
color: #ff7b72;
font-family: 'Fira Code', monospace !important;
}
</style>
""", unsafe_allow_html=True)
# --- SESSION STATE INITIALIZATION ---
if "saved_charts" not in st.session_state:
st.session_state.saved_charts = []
if "query_history" not in st.session_state:
st.session_state.query_history = []
if "schema_dict" not in st.session_state:
st.session_state.schema_dict = None
if "active_analysis" not in st.session_state:
st.session_state.active_analysis = None
# --- CALLBACKS ---
def save_chart_callback():
"""Handles saving the chart to history. Triggered by Button OR Enter key."""
if st.session_state.active_analysis and 'fig' in st.session_state.active_analysis:
title = st.session_state.save_title_input
st.session_state.saved_charts.append({
"title": title,
"fig": st.session_state.active_analysis['fig'],
"sql": st.session_state.active_analysis['sql']
})
st.toast(f"MODULE '{title}' SAVED", icon="💾")
def trigger_analysis():
"""Sets the flag to run analysis. Used by both Enter key and Execute button."""
if st.session_state.query_in_widget:
st.session_state.run_analysis_flag = True
# --- SIDEBAR ---
with st.sidebar:
st.markdown("## SYSTEM_STATUS")
if st.button("[ REFRESH SCHEMA ]"):
with st.spinner("SCANNING DATABASE STRUCTURE..."):
try:
st.session_state.schema_dict = get_complete_schema_dict()
st.success("SCHEMA LOADED")
except Exception as e:
st.error(f"ERR: {str(e)}")
st.markdown("---")
if st.session_state.schema_dict and st.session_state.schema_dict.get('tables'):
st.markdown("## DATABASE_TOPOLOGY")
tables = st.session_state.schema_dict['tables']
for table_name, info in tables.items():
with st.expander(f"TABLE: {table_name}"):
pks = info.get('primary_keys', [])
if pks:
st.markdown("**PRIMARY KEYS:**")
for pk in pks: st.code(f"{pk}", language="text")
fks = info.get('foreign_keys', [])
if fks:
st.markdown("**RELATIONSHIPS:**")
for fk in fks: st.caption(f"{fk['column']} -> {fk['references_table']}.{fk['references_column']}")
st.markdown("**COLUMNS:**")
for col in info.get('columns', []):
col_name = col['name']
col_type = col['type']
prefix = "🔑 " if col_name in pks else "🔗 " if any(fk['column'] == col_name for fk in fks) else ""
st.text(f"{prefix}{col_name} ({col_type})")
else:
st.text("WAITING FOR SCHEMA...")
st.markdown("---")
st.markdown("## SAVED_MODULES")
if st.session_state.saved_charts:
for i, chart in enumerate(st.session_state.saved_charts):
with st.expander(f"MOD_{i+1}: {chart['title']}", expanded=False):
st.plotly_chart(chart['fig'], use_container_width=True, key=f"saved_chart_{i}")
st.code(chart['sql'], language='sql')
if st.button("DELETE", key=f"del_{i}"):
st.session_state.saved_charts.pop(i)
st.rerun()
else:
st.text("NO MODULES SAVED")
st.markdown("---")
st.markdown("## LOGS")
if st.session_state.query_history:
for i, hist in enumerate(reversed(st.session_state.query_history[-10:])):
smart_title = (hist['question'][:35] + '..') if len(hist['question']) > 35 else hist['question']
with st.expander(f"{i+1} > {smart_title}"):
st.code(hist['sql'], language='sql')
st.text(f"Rows: {hist['result_count']}")
st.text(f"Status: {hist['status']}")
# --- MAIN INTERFACE ---
st.markdown("""
<div style="
font-family: 'Fira Code', monospace;
white-space: pre;
line-height: 1.1;
color: #00ff41;
font-size: 14px;
overflow-x: auto;
margin-bottom: 20px;
">
/$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$$$$$ /$$$$$$$$
/$$__ $$ /$$__ $$| $$ | $$ /$ | $$|_ $$_/|_____ $$
| $$ \__/| $$ \ $$| $$ | $$ /$$$| $$ | $$ /$$/
| $$$$$$ | $$ | $$| $$ | $$/$$ $$ $$ | $$ /$$/
\____ $$| $$ | $$| $$ | $$$$_ $$$$ | $$ /$$/
/$$ \ $$| $$ \ $$| $$ | $$$/ \ $$$ | $$ /$$/
| $$$$$$/| $$$$$$$| $$$$$$$$| $$/ \ $$ /$$$$$$ /$$$$$$$$
\______/ \____ $$|________/|__/ \__/|______/|________/
\__/
</div>
""", unsafe_allow_html=True)
st.markdown("### > ANALYTICS_CORE_ONLINE")
# --- INPUT AREA ---
col_in, col_btn = st.columns([4, 1], vertical_alignment="bottom")
with col_in:
st.text_input(
"INPUT_COMMAND:",
placeholder="Enter natural language query...",
key="query_in_widget",
on_change=trigger_analysis
)
with col_btn:
st.button("[ EXECUTE ]", use_container_width=True, on_click=trigger_analysis)
# --- EXECUTION LOGIC ---
if st.session_state.get('run_analysis_flag', False):
st.session_state.run_analysis_flag = False
user_query = st.session_state.query_in_widget
if not st.session_state.schema_dict:
with st.spinner("INITIALIZING SCHEMA..."):
st.session_state.schema_dict = get_complete_schema_dict()
with st.spinner("PROCESSING..."):
sql_query, df, status, error, debug = sql_agent(user_query)
st.session_state.active_analysis = {
"query": user_query,
"sql": sql_query,
"df": df,
"status": status,
"error": error,
"debug": debug,
"timestamp": pd.Timestamp.now()
}
st.session_state.query_history.append({
"question": user_query,
"sql": sql_query,
"result_count": len(df),
"status": status
})
# --- DISPLAY & EDIT LOGIC ---
if st.session_state.active_analysis:
analysis = st.session_state.active_analysis
st.markdown("### > SQL_WORKBENCH")
# 1. EDITABLE SQL
edited_sql = st.text_area(
"SOURCE_CODE (EDITABLE):",
value=analysis['sql'],
height=150,
key=f"sql_editor_{str(analysis.get('timestamp'))}"
)
if st.button("[ RUN EDITED QUERY ]", key="run_edit"):
with st.spinner("EXECUTING CUSTOM QUERY..."):
result_str, new_df, new_error, new_debug = execute_query(edited_sql)
status = "error" if new_error else "success"
st.session_state.active_analysis.update({
"sql": edited_sql,
"df": new_df,
"status": status,
"error": new_error,
"debug": new_debug
})
st.rerun()
# 2. RESULTS
if analysis['status'] == "error":
st.error(f"EXECUTION FAILED: {analysis['error']}")
with st.expander("DEBUG_TRACE"):
st.json(analysis['debug'])
else:
rows = len(analysis['df'])
st.markdown(f"### > DATA_RETURN (ROWS: {rows})")
st.dataframe(analysis['df'], use_container_width=True)
st.markdown("---")
# 3. VISUALIZATION
viz_container = st.container()
col_v1, col_v2 = st.columns([1, 4])
with col_v1:
if st.button("[ GENERATE VISUALIZATION ]"):
with st.spinner("RENDERING GRAPHICS..."):
fig = create_visualization(analysis['df'], analysis['query'])
st.session_state.active_analysis['fig'] = fig
if 'fig' in st.session_state.active_analysis and st.session_state.active_analysis['fig']:
fig = st.session_state.active_analysis['fig']
fig.update_layout(
paper_bgcolor='#0e1117',
plot_bgcolor='#0e1117',
font_color='#00ff41',
title_font_color='#00ff41',
legend_font_color='#ffffff'
)
st.plotly_chart(fig, use_container_width=True, key="active_viz")
# 4. SAVE MODULE
st.markdown("#### > SAVE_MODULE")
c1, c2 = st.columns([3, 1], vertical_alignment="bottom")
with c1:
st.text_input(
"MODULE_NAME",
value=analysis['query'][:50],
key="save_title_input",
on_change=save_chart_callback
)
with c2:
st.button("[ SAVE ]", use_container_width=True, on_click=save_chart_callback)