Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,37 @@ from database import (
|
|
12 |
get_table_schema
|
13 |
)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def get_table_info():
|
16 |
"""
|
17 |
Gets the current table name and column information.
|
@@ -206,12 +237,6 @@ def query_sql(user_query: str) -> str:
|
|
206 |
except ValueError:
|
207 |
return result
|
208 |
|
209 |
-
def update_schema():
|
210 |
-
schema = get_table_schema()
|
211 |
-
if schema:
|
212 |
-
return f"### Current Schema:\n```\n{schema}\n```"
|
213 |
-
return "No data loaded"
|
214 |
-
|
215 |
# Create the Gradio interface
|
216 |
with gr.Blocks() as demo:
|
217 |
with gr.Group() as upload_group:
|
@@ -261,24 +286,32 @@ with gr.Blocks() as demo:
|
|
261 |
|
262 |
def handle_upload(file_obj):
|
263 |
if file_obj is None:
|
264 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
success, message = process_uploaded_file(file_obj)
|
267 |
if success:
|
268 |
df = get_data_table()
|
269 |
-
|
|
|
|
|
|
|
|
|
270 |
return (
|
271 |
message,
|
272 |
df,
|
273 |
-
schema,
|
274 |
-
f"### Current Schema:\n```\n{schema}\n```" if schema else "No schema available",
|
275 |
gr.update(visible=False),
|
276 |
gr.update(visible=True)
|
277 |
)
|
278 |
return (
|
279 |
message,
|
280 |
None,
|
281 |
-
None,
|
282 |
"No schema available",
|
283 |
gr.update(visible=True),
|
284 |
gr.update(visible=False)
|
@@ -286,8 +319,12 @@ with gr.Blocks() as demo:
|
|
286 |
|
287 |
def refresh_data():
|
288 |
df = get_data_table()
|
289 |
-
|
290 |
-
|
|
|
|
|
|
|
|
|
291 |
|
292 |
# Event handlers
|
293 |
file_input.upload(
|
@@ -297,7 +334,6 @@ with gr.Blocks() as demo:
|
|
297 |
status,
|
298 |
data_table,
|
299 |
schema_display,
|
300 |
-
schema_display,
|
301 |
upload_group,
|
302 |
query_group
|
303 |
]
|
|
|
12 |
get_table_schema
|
13 |
)
|
14 |
|
15 |
+
def get_data_table():
|
16 |
+
"""
|
17 |
+
Fetches all data from the current table and returns it as a Pandas DataFrame.
|
18 |
+
"""
|
19 |
+
try:
|
20 |
+
# Get list of tables
|
21 |
+
with engine.connect() as con:
|
22 |
+
tables = con.execute(text(
|
23 |
+
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
|
24 |
+
)).fetchall()
|
25 |
+
|
26 |
+
if not tables:
|
27 |
+
return pd.DataFrame()
|
28 |
+
|
29 |
+
# Use the first table found
|
30 |
+
table_name = tables[0][0]
|
31 |
+
|
32 |
+
with engine.connect() as con:
|
33 |
+
result = con.execute(text(f"SELECT * FROM {table_name}"))
|
34 |
+
rows = result.fetchall()
|
35 |
+
|
36 |
+
if not rows:
|
37 |
+
return pd.DataFrame()
|
38 |
+
|
39 |
+
columns = result.keys()
|
40 |
+
df = pd.DataFrame(rows, columns=columns)
|
41 |
+
return df
|
42 |
+
|
43 |
+
except Exception as e:
|
44 |
+
return pd.DataFrame({"Error": [str(e)]})
|
45 |
+
|
46 |
def get_table_info():
|
47 |
"""
|
48 |
Gets the current table name and column information.
|
|
|
237 |
except ValueError:
|
238 |
return result
|
239 |
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
# Create the Gradio interface
|
241 |
with gr.Blocks() as demo:
|
242 |
with gr.Group() as upload_group:
|
|
|
286 |
|
287 |
def handle_upload(file_obj):
|
288 |
if file_obj is None:
|
289 |
+
return (
|
290 |
+
"Please upload a file.",
|
291 |
+
None,
|
292 |
+
"No schema available",
|
293 |
+
gr.update(visible=True),
|
294 |
+
gr.update(visible=False)
|
295 |
+
)
|
296 |
|
297 |
success, message = process_uploaded_file(file_obj)
|
298 |
if success:
|
299 |
df = get_data_table()
|
300 |
+
_, _, column_info = get_table_info()
|
301 |
+
schema = "\n".join([
|
302 |
+
f"- {col} ({info['type']}){' primary key' if info['is_primary'] else ''}"
|
303 |
+
for col, info in column_info.items()
|
304 |
+
])
|
305 |
return (
|
306 |
message,
|
307 |
df,
|
308 |
+
f"### Current Schema:\n```\n{schema}\n```",
|
|
|
309 |
gr.update(visible=False),
|
310 |
gr.update(visible=True)
|
311 |
)
|
312 |
return (
|
313 |
message,
|
314 |
None,
|
|
|
315 |
"No schema available",
|
316 |
gr.update(visible=True),
|
317 |
gr.update(visible=False)
|
|
|
319 |
|
320 |
def refresh_data():
|
321 |
df = get_data_table()
|
322 |
+
_, _, column_info = get_table_info()
|
323 |
+
schema = "\n".join([
|
324 |
+
f"- {col} ({info['type']}){' primary key' if info['is_primary'] else ''}"
|
325 |
+
for col, info in column_info.items()
|
326 |
+
])
|
327 |
+
return df, f"### Current Schema:\n```\n{schema}\n```"
|
328 |
|
329 |
# Event handlers
|
330 |
file_input.upload(
|
|
|
334 |
status,
|
335 |
data_table,
|
336 |
schema_display,
|
|
|
337 |
upload_group,
|
338 |
query_group
|
339 |
]
|