Update app.py
Browse files
app.py
CHANGED
@@ -33,7 +33,7 @@ def process_sql_file(file_path):
|
|
33 |
if statement.strip(): # Skip empty statements
|
34 |
conn.execute(text(statement))
|
35 |
|
36 |
-
return True, "SQL file successfully executed!
|
37 |
|
38 |
except Exception as e:
|
39 |
return False, f"Error processing SQL file: {str(e)}"
|
@@ -57,7 +57,7 @@ def process_csv_file(file_path):
|
|
57 |
records = df.to_dict('records')
|
58 |
insert_rows_into_table(records, table)
|
59 |
|
60 |
-
return True, "CSV file successfully loaded!
|
61 |
|
62 |
except Exception as e:
|
63 |
return False, f"Error processing CSV file: {str(e)}"
|
@@ -176,13 +176,13 @@ def query_sql(user_query: str) -> str:
|
|
176 |
except ValueError:
|
177 |
return result
|
178 |
|
|
|
179 |
with gr.Blocks() as demo:
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
with upload_interface:
|
186 |
gr.Markdown("""
|
187 |
# Data Query Interface
|
188 |
|
@@ -208,10 +208,12 @@ with gr.Blocks() as demo:
|
|
208 |
file_types=[".csv", ".sql"],
|
209 |
type="filepath"
|
210 |
)
|
211 |
-
|
|
|
212 |
|
213 |
-
#
|
214 |
-
|
|
|
215 |
gr.Markdown("""
|
216 |
## Data Query Interface
|
217 |
|
@@ -234,47 +236,67 @@ with gr.Blocks() as demo:
|
|
234 |
|
235 |
schema_display = gr.Markdown(value="Loading schema...")
|
236 |
|
237 |
-
def update_schema():
|
238 |
-
schema = get_table_schema()
|
239 |
-
if schema:
|
240 |
-
return f"### Current Schema:\n```\n{schema}\n```"
|
241 |
-
return "No data loaded"
|
242 |
-
|
243 |
-
user_input.change(
|
244 |
-
fn=query_sql,
|
245 |
-
inputs=[user_input],
|
246 |
-
outputs=[query_output]
|
247 |
-
)
|
248 |
-
|
249 |
with gr.Row():
|
250 |
refresh_table_btn = gr.Button("Refresh Table")
|
251 |
refresh_schema_btn = gr.Button("Refresh Schema")
|
252 |
-
|
253 |
-
refresh_table_btn.click(
|
254 |
-
fn=get_data_table,
|
255 |
-
outputs=[data_table]
|
256 |
-
)
|
257 |
-
|
258 |
-
refresh_schema_btn.click(
|
259 |
-
fn=update_schema,
|
260 |
-
outputs=[schema_display]
|
261 |
-
)
|
262 |
-
|
263 |
-
query_interface.load(
|
264 |
-
fn=update_schema,
|
265 |
-
outputs=[schema_display]
|
266 |
-
)
|
267 |
|
268 |
def handle_upload(file):
|
269 |
success, message = process_uploaded_file(file)
|
270 |
-
|
271 |
-
|
272 |
-
|
|
|
|
|
|
|
|
|
273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
file_input.upload(
|
275 |
fn=handle_upload,
|
276 |
inputs=[file_input],
|
277 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
)
|
279 |
|
280 |
if __name__ == "__main__":
|
|
|
33 |
if statement.strip(): # Skip empty statements
|
34 |
conn.execute(text(statement))
|
35 |
|
36 |
+
return True, "SQL file successfully executed! Click 'Continue' to proceed to query interface..."
|
37 |
|
38 |
except Exception as e:
|
39 |
return False, f"Error processing SQL file: {str(e)}"
|
|
|
57 |
records = df.to_dict('records')
|
58 |
insert_rows_into_table(records, table)
|
59 |
|
60 |
+
return True, "CSV file successfully loaded! Click 'Continue' to proceed to query interface..."
|
61 |
|
62 |
except Exception as e:
|
63 |
return False, f"Error processing CSV file: {str(e)}"
|
|
|
176 |
except ValueError:
|
177 |
return result
|
178 |
|
179 |
+
# Create the Gradio interface
|
180 |
with gr.Blocks() as demo:
|
181 |
+
current_state = gr.State("upload")
|
182 |
+
|
183 |
+
# Upload Interface Components
|
184 |
+
upload_group = gr.Group(visible=True)
|
185 |
+
with upload_group:
|
|
|
186 |
gr.Markdown("""
|
187 |
# Data Query Interface
|
188 |
|
|
|
208 |
file_types=[".csv", ".sql"],
|
209 |
type="filepath"
|
210 |
)
|
211 |
+
upload_status = gr.Textbox(label="Status", interactive=False)
|
212 |
+
continue_btn = gr.Button("Continue", visible=False)
|
213 |
|
214 |
+
# Query Interface Components
|
215 |
+
query_group = gr.Group(visible=False)
|
216 |
+
with query_group:
|
217 |
gr.Markdown("""
|
218 |
## Data Query Interface
|
219 |
|
|
|
236 |
|
237 |
schema_display = gr.Markdown(value="Loading schema...")
|
238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
with gr.Row():
|
240 |
refresh_table_btn = gr.Button("Refresh Table")
|
241 |
refresh_schema_btn = gr.Button("Refresh Schema")
|
242 |
+
back_btn = gr.Button("Upload New File")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
def handle_upload(file):
|
245 |
success, message = process_uploaded_file(file)
|
246 |
+
return message, gr.Button.update(visible=success)
|
247 |
+
|
248 |
+
def update_schema():
|
249 |
+
schema = get_table_schema()
|
250 |
+
if schema:
|
251 |
+
return f"### Current Schema:\n```\n{schema}\n```"
|
252 |
+
return "No data loaded"
|
253 |
|
254 |
+
def switch_to_query():
|
255 |
+
return {
|
256 |
+
upload_group: gr.Group.update(visible=False),
|
257 |
+
query_group: gr.Group.update(visible=True),
|
258 |
+
schema_display: update_schema()
|
259 |
+
}
|
260 |
+
|
261 |
+
def switch_to_upload():
|
262 |
+
return {
|
263 |
+
upload_group: gr.Group.update(visible=True),
|
264 |
+
query_group: gr.Group.update(visible=False),
|
265 |
+
continue_btn: gr.Button.update(visible=False),
|
266 |
+
upload_status: gr.Textbox.update(value="")
|
267 |
+
}
|
268 |
+
|
269 |
+
# Event handlers
|
270 |
file_input.upload(
|
271 |
fn=handle_upload,
|
272 |
inputs=[file_input],
|
273 |
+
outputs=[upload_status, continue_btn]
|
274 |
+
)
|
275 |
+
|
276 |
+
continue_btn.click(
|
277 |
+
fn=switch_to_query,
|
278 |
+
outputs=[upload_group, query_group, schema_display]
|
279 |
+
)
|
280 |
+
|
281 |
+
back_btn.click(
|
282 |
+
fn=switch_to_upload,
|
283 |
+
outputs=[upload_group, query_group, continue_btn, upload_status]
|
284 |
+
)
|
285 |
+
|
286 |
+
user_input.change(
|
287 |
+
fn=query_sql,
|
288 |
+
inputs=[user_input],
|
289 |
+
outputs=[query_output]
|
290 |
+
)
|
291 |
+
|
292 |
+
refresh_table_btn.click(
|
293 |
+
fn=get_data_table,
|
294 |
+
outputs=[data_table]
|
295 |
+
)
|
296 |
+
|
297 |
+
refresh_schema_btn.click(
|
298 |
+
fn=update_schema,
|
299 |
+
outputs=[schema_display]
|
300 |
)
|
301 |
|
302 |
if __name__ == "__main__":
|