LeonceNsh commited on
Commit
3b785aa
·
verified ·
1 Parent(s): 991b597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py CHANGED
@@ -186,5 +186,39 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
186
  outputs=[sql_query_out, error_out, results_out]
187
  )
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  # Launch the Gradio App
190
  demo.queue().launch()
 
186
  outputs=[sql_query_out, error_out, results_out]
187
  )
188
 
189
+ # Add a Gradio File output component for the download functionality
190
+ download_csv_btn = gr.File(label="📥 Download CSV", visible=False)
191
+
192
+ # Function to save the results to a CSV and return the file path
193
+ def save_to_csv(results_df):
194
+ if results_df is None or results_df.empty:
195
+ return None, "⚠️ No results to download."
196
+ try:
197
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
198
+ results_df.to_csv(temp_file.name, index=False)
199
+ return temp_file.name, ""
200
+ except Exception as e:
201
+ return None, f"❌ Error generating CSV: {e}"
202
+
203
+ # Add functionality to generate and show the download link for the CSV
204
+ def generate_download(results_df):
205
+ file_path, error = save_to_csv(results_df)
206
+ if error:
207
+ return None, f"❌ {error}"
208
+ return file_path, ""
209
+
210
+ # Update the Gradio event handlers
211
+ btn_execute_query.click(
212
+ fn=execute_query,
213
+ inputs=sql_query_out,
214
+ outputs=[results_out, error_out]
215
+ )
216
+
217
+ btn_execute_query.click(
218
+ fn=generate_download,
219
+ inputs=results_out,
220
+ outputs=[download_csv_btn, error_out]
221
+ )
222
+
223
  # Launch the Gradio App
224
  demo.queue().launch()