-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
93 lines (73 loc) · 2.35 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import pandas as pd
import sqlite3
import streamlit as st
import base64
from sqlalchemy import (
create_engine,
MetaData,
Table,
Column,
String,
Integer,
Float,
DateTime,
)
# Infer column types from CSV
def infer_column_types(df):
type_map = {
"int64": Integer,
"float64": Float,
"datetime64": DateTime,
"object": String,
}
return [type_map[str(df[col].dtype)] for col in df.columns]
# Import CSV data into the database
def csv_to_sqlite(df, db_file, table_name):
# Create a connection to the SQLite database
conn = sqlite3.connect(db_file)
# Write the contents of the DataFrame to the SQLite database
df.to_sql(table_name, conn, if_exists="replace", index=False)
# Commit and close the connection
conn.commit()
conn.close()
def get_binary_file_downloader_html(bin_file, file_label="File"):
with open(bin_file, "rb") as f:
data = f.read()
bin_str = base64.b64encode(data).decode()
href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{bin_file}">{file_label}</a>' # noqa: E501
return href
st.set_page_config(
page_title="CSV to SQLite",
page_icon="🔗",
layout="wide",
initial_sidebar_state="collapsed",
)
st.title("CSV to SQLite Converter")
st.markdown("Service by [NGMI.ai](https://ngmi.ai)")
st.markdown("Follow Us on [Mastodon](https://mastodon.online/@ngmi)")
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
if uploaded_file:
# Read CSV file using pandas
df = pd.read_csv(uploaded_file)
# Get input file name and replace the extension with .sqlite
db_file = uploaded_file.name.replace(".csv", ".sqlite")
table_name = "input_table"
# Create database schema
engine = create_engine(f"sqlite:///{db_file}")
metadata_obj = MetaData()
column_types = infer_column_types(df)
wallet_stats_table = Table(
table_name,
metadata_obj,
*[
Column(column_name, column_type)
for column_name, column_type in zip(df.columns, column_types)
],
)
metadata_obj.create_all(engine)
csv_to_sqlite(df, db_file, table_name)
st.success(f"CSV data has been imported into {db_file}.")
st.markdown(
get_binary_file_downloader_html(db_file, "Download SQLite Database"),
unsafe_allow_html=True,
)