berangerthomas commited on
Commit
590a51c
·
1 Parent(s): 95917b6

Fix sqlite3 export

Browse files
Files changed (1) hide show
  1. 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.success("File parsed and filtered successfully!")
 
 
 
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
- try:
99
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
100
- sqlite_path = os.path.join(
101
- tempfile.gettempdir(), f"log_data_{timestamp}.sqlite"
102
- )
103
- sqlite_conn_string = f"sqlite:///{sqlite_path}"
104
- original_filename = os.path.splitext(uploaded_file.name)[0]
105
- st.session_state.parsed_df.write_database(
106
- table_name=original_filename,
107
- connection=sqlite_conn_string,
108
- if_table_exists="replace",
109
- )
110
- with open(sqlite_path, "rb") as file:
111
- sqlite_data = file.read()
112
- st.success("SQLite file created successfully!")
113
- st.download_button(
114
- label="Download SQLite file",
115
- file_name=f"log_file_{original_filename}_{timestamp}.sqlite",
116
- mime="application/octet-stream",
117
- )
118
- except Exception as e:
119
- st.error(f"Error converting to SQLite: {e}")
120
- finally:
121
- if os.path.exists(sqlite_path):
122
- os.unlink(sqlite_path)
 
 
 
 
 
 
 
 
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!")