Spaces:
Sleeping
Sleeping
seriouspark
commited on
Commit
β’
7b1cbee
1
Parent(s):
0aa934a
sql_uploader and practier
Browse files- app.py +21 -0
- make_db.py +81 -0
- requirements.txt +5 -0
- sql_training.py +30 -0
app.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from streamlit import config
|
3 |
+
import make_db
|
4 |
+
#import text2sql
|
5 |
+
import sql_training
|
6 |
+
|
7 |
+
|
8 |
+
# νμ΄μ§ μ€μ
|
9 |
+
#st.set_page_config(page_title='text2sql', layout = 'wide')
|
10 |
+
|
11 |
+
PAGES = {
|
12 |
+
'Excel to DataBase': make_db,
|
13 |
+
'SQL Training' : sql_training,
|
14 |
+
# 'Text2SQL' : text2sql
|
15 |
+
}
|
16 |
+
|
17 |
+
st.sidebar.title('λ©λ΄')
|
18 |
+
selection = st.sidebar.radio('Go to', list(PAGES.keys()))
|
19 |
+
|
20 |
+
page = PAGES[selection]
|
21 |
+
page.app()
|
make_db.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import sqlite3
|
4 |
+
import os
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
|
8 |
+
def app():
|
9 |
+
st.title('Excel to DataBase')
|
10 |
+
st.write('μμ
μ λ£μ΄ λ°μ΄ν°λ² μ΄μ€λ₯Ό λ§λ€μ΄λ΄
μλ€.')
|
11 |
+
file_name = st.text_input('νμΌλͺ
μ§μ νκΈ°')
|
12 |
+
# μμ
νμΌ μ
λ‘λ
|
13 |
+
uploaded_file = st.file_uploader('Choose an Excel file', type = ['xlsx','xls','csv'])
|
14 |
+
|
15 |
+
if uploaded_file is not None:
|
16 |
+
# μμ
νμΌμ λ°μ΄ν°νλ μμΌλ‘ λ³ν
|
17 |
+
try:
|
18 |
+
df = pd.read_csv(uploaded_file)
|
19 |
+
|
20 |
+
except:
|
21 |
+
df = pd.read_excel(uploaded_file)
|
22 |
+
|
23 |
+
|
24 |
+
# κ° μ΄μ λν λ°μ΄ν° νμ
μ ν μ΅μ
μ 곡
|
25 |
+
data_types = {'object': 'String (TEXT)',
|
26 |
+
'float' : 'Float (REAL)',
|
27 |
+
'int' : 'Integer (INT)',
|
28 |
+
'datetime': 'Datetime (TEXT)',
|
29 |
+
'bool' : 'Bool',
|
30 |
+
}
|
31 |
+
selected_data_types = {}
|
32 |
+
for column in df.columns:
|
33 |
+
data_type = st.selectbox(f"SELECT data type for column '{column}'",
|
34 |
+
options = list(data_types.keys()),
|
35 |
+
format_func = lambda x : data_types[x],
|
36 |
+
key = column)
|
37 |
+
selected_data_types[column] = data_type
|
38 |
+
# μλ int / float κ²λ€ μ€μμ object λ‘ λ³νν΄μΌ ν κ²λ€μ object λ‘ λ°κΎΈμ΄μ£ΌκΈ°
|
39 |
+
if st.button('λ°μ΄ν° λ³ννκ³ μ μ₯νκΈ°'):
|
40 |
+
for column, data_type in selected_data_types.items():
|
41 |
+
if data_type == 'float':
|
42 |
+
try:
|
43 |
+
df[column] = df[column].str.replace(',','')
|
44 |
+
except:
|
45 |
+
continue
|
46 |
+
df[column] = pd.to_numeric(df[column], errors = 'coerce')
|
47 |
+
elif data_type == 'int':
|
48 |
+
try:
|
49 |
+
df[column] = df[column].str.replace(',','')
|
50 |
+
except:
|
51 |
+
continue
|
52 |
+
df[column] = pd.to_numeric(df[column].str.replace(',',''), errors = 'coerce').fillna(0).astype(int)
|
53 |
+
elif data_type == 'datetime':
|
54 |
+
df[column] = pd.to_datetime(df[column], errors = 'coerce')
|
55 |
+
elif data_type == 'bool':
|
56 |
+
df[column] = df[column].astype(bool)
|
57 |
+
elif data_type == 'object':
|
58 |
+
if df[column].dtypes == float or df[column].dtypes == int:
|
59 |
+
df[column] = df[column].astype(str).str.replace('.0','')
|
60 |
+
else:
|
61 |
+
df[column] = df[column]
|
62 |
+
|
63 |
+
next = True
|
64 |
+
|
65 |
+
|
66 |
+
if next:
|
67 |
+
# sql lite λ°μ΄ν°λ² μ΄μ€ μ°κ²° λ° μμ±
|
68 |
+
conn = sqlite3.connect(file_name)
|
69 |
+
c = conn.cursor()
|
70 |
+
|
71 |
+
# λ°μ΄ν°νλ μμ SQLν
μ΄λΈλ‘ λ³ν
|
72 |
+
|
73 |
+
df.to_sql(f'{file_name}', conn, dtype = selected_data_types, if_exists = 'replace', index = False)
|
74 |
+
|
75 |
+
st.success(f'νμΌμ μ±κ³΅μ μΌλ‘ λ°μ΄ν°λ² μ΄μ€λ‘ μ μ₯λμμ΅λλ€. λ°μ΄ν°λ² μ΄μ€λͺ
[{file_name}]')
|
76 |
+
|
77 |
+
# μ°κ²° μ’
λ£
|
78 |
+
conn.close()
|
79 |
+
|
80 |
+
|
81 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
openpyxl
|
5 |
+
pandas
|
sql_training.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import sqlite3
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
def app():
|
7 |
+
st.title('SQL Training')
|
8 |
+
st.write('SQL μ°μ΅μ ν΄λ΄
μλ€.')
|
9 |
+
file_name = st.text_input('file name:', )
|
10 |
+
|
11 |
+
# μμ
νμΌ μ
λ‘λ
|
12 |
+
|
13 |
+
|
14 |
+
user_query = st.text_area('Enter your SQL query:', height = 100)
|
15 |
+
if st.button('쿼리 μ€ν'):
|
16 |
+
try:
|
17 |
+
# 쿼리 μ€ν λ° κ²°κ³Ό μΆλ ₯
|
18 |
+
conn = sqlite3.connect(file_name)
|
19 |
+
c = conn.cursor()
|
20 |
+
query_results = pd.read_sql_query(user_query, conn)
|
21 |
+
if not query_results.empty:
|
22 |
+
st.dataframe(query_results)
|
23 |
+
else:
|
24 |
+
st.write('쿼리λ μ±κ³΅μ μΌλ‘ μ€νλμμ΅λλ€. κ·Έλ¬λ κ²°κ³Όκ° μλ€μ.')
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f'μλ¬κ° λ°μνμ΅λλ€: {e}')
|
27 |
+
finally:
|
28 |
+
conn.close()
|
29 |
+
|
30 |
+
|