Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
803ff18
1
Parent(s):
c00a199
Update app.py
Browse files
app.py
CHANGED
@@ -203,18 +203,20 @@ def view_database():
|
|
203 |
|
204 |
# Function to download database or HTML file
|
205 |
def download_file(choice):
|
206 |
-
|
|
|
|
|
|
|
|
|
207 |
|
208 |
-
|
209 |
-
# Check if the database file exists
|
210 |
-
if os.path.exists(db_file_path):
|
211 |
-
# Return the database file path directly
|
212 |
-
return db_file_path
|
213 |
-
else:
|
214 |
-
raise FileNotFoundError(f"{db_file_path} not found.")
|
215 |
|
|
|
|
|
|
|
|
|
216 |
elif choice == "Database (.html)":
|
217 |
-
# Connect to the
|
218 |
conn = sqlite3.connect(db_file_path)
|
219 |
|
220 |
try:
|
@@ -229,7 +231,7 @@ def download_file(choice):
|
|
229 |
conn.close()
|
230 |
|
231 |
# Define the path for the HTML file
|
232 |
-
html_file_path = os.path.join(
|
233 |
|
234 |
# Save the DataFrame as an HTML file
|
235 |
df.to_html(html_file_path, index=False)
|
@@ -263,15 +265,12 @@ def download_interface(choice):
|
|
263 |
# Get the file path
|
264 |
file_path = download_file(choice)
|
265 |
|
266 |
-
#
|
267 |
-
|
268 |
-
file_content = file.read()
|
269 |
-
file_name = os.path.basename(file_path)
|
270 |
-
return file_content, file_name # Return as a tuple for Gradio file component
|
271 |
except FileNotFoundError as e:
|
272 |
-
return str(e)
|
273 |
except ValueError as e:
|
274 |
-
return str(e)
|
275 |
|
276 |
# Gradio Blocks
|
277 |
with gr.Blocks() as demo:
|
@@ -308,7 +307,7 @@ with gr.Blocks() as demo:
|
|
308 |
download_btn = gr.Button("Download")
|
309 |
download_output = gr.File(label="Download File")
|
310 |
|
311 |
-
download_btn.click(fn=download_interface, inputs=[download_choice], outputs=
|
312 |
|
313 |
# Launch the Gradio app
|
314 |
demo.launch()
|
|
|
203 |
|
204 |
# Function to download database or HTML file
|
205 |
def download_file(choice):
|
206 |
+
directory = tempfile.gettempdir()
|
207 |
+
|
208 |
+
# Ensure the directory exists
|
209 |
+
if not os.path.exists(directory):
|
210 |
+
os.makedirs(directory)
|
211 |
|
212 |
+
db_file_path = os.path.join(directory, 'results.db')
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
|
214 |
+
if choice == "Database (.db)":
|
215 |
+
# Simply return the path to the database file
|
216 |
+
return db_file_path
|
217 |
+
|
218 |
elif choice == "Database (.html)":
|
219 |
+
# Connect to the SQLite database
|
220 |
conn = sqlite3.connect(db_file_path)
|
221 |
|
222 |
try:
|
|
|
231 |
conn.close()
|
232 |
|
233 |
# Define the path for the HTML file
|
234 |
+
html_file_path = os.path.join(directory, "results.html")
|
235 |
|
236 |
# Save the DataFrame as an HTML file
|
237 |
df.to_html(html_file_path, index=False)
|
|
|
265 |
# Get the file path
|
266 |
file_path = download_file(choice)
|
267 |
|
268 |
+
# Return the file path (string) directly for the Gradio component to handle
|
269 |
+
return file_path
|
|
|
|
|
|
|
270 |
except FileNotFoundError as e:
|
271 |
+
return str(e) # Handle file not found error gracefully
|
272 |
except ValueError as e:
|
273 |
+
return str(e) # Handle any value errors (e.g., invalid choice)
|
274 |
|
275 |
# Gradio Blocks
|
276 |
with gr.Blocks() as demo:
|
|
|
307 |
download_btn = gr.Button("Download")
|
308 |
download_output = gr.File(label="Download File")
|
309 |
|
310 |
+
download_btn.click(fn=download_interface, inputs=[download_choice], outputs=gr.File())
|
311 |
|
312 |
# Launch the Gradio app
|
313 |
demo.launch()
|