Spaces:
Sleeping
Sleeping
Commit
·
590a51c
1
Parent(s):
95917b6
Fix sqlite3 export
Browse files- sections/upload.py +37 -26
sections/upload.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import tempfile
|
| 3 |
from datetime import datetime
|
| 4 |
|
|
@@ -88,35 +89,45 @@ if uploaded_file is not None:
|
|
| 88 |
)
|
| 89 |
.drop(["portsrc", "unknown", "fw"])
|
| 90 |
)
|
| 91 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
st.error(f"Error parsing the file: {e}")
|
| 94 |
|
| 95 |
if st.session_state.parsed_df is not None:
|
| 96 |
if st.button("Convert to SQLite"):
|
| 97 |
with st.spinner("Converting to SQLite..."):
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sqlite3
|
| 3 |
import tempfile
|
| 4 |
from datetime import datetime
|
| 5 |
|
|
|
|
| 89 |
)
|
| 90 |
.drop(["portsrc", "unknown", "fw"])
|
| 91 |
)
|
| 92 |
+
row_count = st.session_state.parsed_df.height
|
| 93 |
+
st.success(
|
| 94 |
+
f"File parsed and filtered successfully! After filtering, {row_count:,} rows remain."
|
| 95 |
+
)
|
| 96 |
except Exception as e:
|
| 97 |
st.error(f"Error parsing the file: {e}")
|
| 98 |
|
| 99 |
if st.session_state.parsed_df is not None:
|
| 100 |
if st.button("Convert to SQLite"):
|
| 101 |
with st.spinner("Converting to SQLite..."):
|
| 102 |
+
# Create a temporary file for the SQLite database
|
| 103 |
+
temp_db_file = tempfile.NamedTemporaryFile(delete=False, suffix=".db")
|
| 104 |
+
temp_db_path = temp_db_file.name
|
| 105 |
+
temp_db_file.close()
|
| 106 |
+
|
| 107 |
+
# Connect to SQLite database
|
| 108 |
+
conn = sqlite3.connect(temp_db_path)
|
| 109 |
+
|
| 110 |
+
# Convert Polars DataFrame to Pandas for easy SQLite export
|
| 111 |
+
pandas_df = st.session_state.parsed_df.to_pandas()
|
| 112 |
+
|
| 113 |
+
# Write to SQLite
|
| 114 |
+
pandas_df.to_sql("logs", conn, if_exists="replace", index=False)
|
| 115 |
+
conn.close()
|
| 116 |
+
|
| 117 |
+
# Prepare file for download
|
| 118 |
+
with open(temp_db_path, "rb") as file:
|
| 119 |
+
db_contents = file.read()
|
| 120 |
+
|
| 121 |
+
# Create download button
|
| 122 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 123 |
+
st.download_button(
|
| 124 |
+
label="Download SQLite Database",
|
| 125 |
+
data=db_contents,
|
| 126 |
+
file_name=f"logs_{timestamp}.sqlite3",
|
| 127 |
+
mime="application/octet-stream",
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# Clean up
|
| 131 |
+
os.unlink(temp_db_path)
|
| 132 |
+
|
| 133 |
+
st.success("SQLite conversion complete!")
|