ariankhalfani commited on
Commit
36fa739
1 Parent(s): d781ad4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -18
app.py CHANGED
@@ -203,29 +203,44 @@ def view_database():
203
  return html_content
204
 
205
  # Function to download database or HTML file
206
- def download_file(choice):
207
- # Define a temporary directory to store the database file
208
  directory = tempfile.gettempdir()
209
  db_file_path = os.path.join(directory, 'results.db')
210
-
211
- if choice == "Database (.db)":
212
- # Ensure the database file exists
213
  if not os.path.exists(db_file_path):
214
- # Create and populate the database with dummy data if needed
215
  conn = sqlite3.connect(db_file_path)
216
- # Example table creation (if not already exists)
217
- conn.execute('''CREATE TABLE IF NOT EXISTS results (
218
- id INTEGER PRIMARY KEY,
219
- name TEXT NOT NULL,
220
- value TEXT NOT NULL
221
- );''')
222
- conn.execute("INSERT INTO results (name, value) VALUES ('example', '123')")
223
  conn.commit()
224
  conn.close()
225
-
226
- # Return the database file path for download
227
  return db_file_path
228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  elif choice == "Database (.html)":
230
  # Connect to the SQLite database
231
  conn = sqlite3.connect(db_file_path)
@@ -314,10 +329,12 @@ with gr.Blocks() as demo:
314
 
315
  with gr.Column():
316
  download_choice = gr.Radio(["Database (.db)", "Database (.html)"], label="Choose the file to download:")
317
- download_btn = gr.Button("Download")
318
- download_output = gr.File(label="Download File")
 
319
 
320
- download_btn.click(download_file, inputs=choice, outputs=download_file_output)
 
321
 
322
  # Launch the Gradio app
323
  demo.launch()
 
203
  return html_content
204
 
205
  # Function to download database or HTML file
206
+ def download_file(file_format):
 
207
  directory = tempfile.gettempdir()
208
  db_file_path = os.path.join(directory, 'results.db')
209
+
210
+ if file_format == "Database (.db)":
211
+ # Simulate creating or writing to the database file here
212
  if not os.path.exists(db_file_path):
213
+ # Create a dummy database file for demonstration
214
  conn = sqlite3.connect(db_file_path)
215
+ conn.execute('CREATE TABLE IF NOT EXISTS results (id INTEGER PRIMARY KEY, name TEXT)')
 
 
 
 
 
 
216
  conn.commit()
217
  conn.close()
 
 
218
  return db_file_path
219
 
220
+ elif file_format == "HTML (.html)":
221
+ # Connect to the SQLite database
222
+ conn = sqlite3.connect(db_file_path)
223
+
224
+ try:
225
+ # Attempt to read the results table into a DataFrame
226
+ df = pd.read_sql_query("SELECT * FROM results", conn)
227
+ except pd.errors.DatabaseError as e:
228
+ conn.close()
229
+ raise ValueError("Table 'results' does not exist in the database.") from e
230
+
231
+ # Close the database connection
232
+ conn.close()
233
+
234
+ # Define the path for the HTML file
235
+ html_file_path = os.path.join(directory, "results.html")
236
+
237
+ # Save the DataFrame as an HTML file
238
+ df.to_html(html_file_path, index=False)
239
+
240
+ return html_file_path
241
+ else:
242
+ raise ValueError("Invalid file format specified.")
243
+
244
  elif choice == "Database (.html)":
245
  # Connect to the SQLite database
246
  conn = sqlite3.connect(db_file_path)
 
329
 
330
  with gr.Column():
331
  download_choice = gr.Radio(["Database (.db)", "Database (.html)"], label="Choose the file to download:")
332
+ download_btn_db = gr.Button("Download Database (.db)")
333
+ download_btn_html = gr.Button("Download HTML (.html)")
334
+ download_file_output = gr.File(label="Download File")
335
 
336
+ download_btn_db.click(lambda: download_file("Database (.db)"), outputs=download_file_output)
337
+ download_btn_html.click(lambda: download_file("HTML (.html)"), outputs=download_file_output)
338
 
339
  # Launch the Gradio app
340
  demo.launch()