Spaces:
Sleeping
Sleeping
Initial Draft
Browse files- MigrationUtility.py +65 -0
MigrationUtility.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import re
|
| 4 |
+
import pandasql as psql
|
| 5 |
+
import base64 # For file download
|
| 6 |
+
|
| 7 |
+
################################
|
| 8 |
+
####### Generic functions ######
|
| 9 |
+
################################
|
| 10 |
+
def process_input():
|
| 11 |
+
try:
|
| 12 |
+
if uploaded_files is not None:
|
| 13 |
+
for file in uploaded_files:
|
| 14 |
+
df = pd.read_csv(file)
|
| 15 |
+
|
| 16 |
+
pattern = r'([^/]+)\.csv$'
|
| 17 |
+
match = re.search(pattern, file.name)
|
| 18 |
+
file_name = match.group(1)
|
| 19 |
+
|
| 20 |
+
globals()['%s' % file_name] = df
|
| 21 |
+
except Exception as e:
|
| 22 |
+
st.error(f"Error processing csv: {str(e)}")
|
| 23 |
+
|
| 24 |
+
################################
|
| 25 |
+
####### Display of data ########
|
| 26 |
+
################################
|
| 27 |
+
|
| 28 |
+
# Streamlit Settings
|
| 29 |
+
st.set_page_config(layout='wide')
|
| 30 |
+
col1, col2, col3 = st.columns([1.5, 2, 0.5])
|
| 31 |
+
with col2:
|
| 32 |
+
st.title("Migration Utility")
|
| 33 |
+
|
| 34 |
+
# Data Display Layout
|
| 35 |
+
col1, col2, col3 = st.columns([1.25, 2, 0.5])
|
| 36 |
+
with col1:
|
| 37 |
+
options = ["csv", "xlsx"]
|
| 38 |
+
file_type = st.selectbox(
|
| 39 |
+
"Select Input File Type",
|
| 40 |
+
options, key="file_type"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# -- Layout for File Upload
|
| 44 |
+
col1, col2, col3 = st.columns([1, 1, 1])
|
| 45 |
+
with col1:
|
| 46 |
+
uploaded_files = st.file_uploader(
|
| 47 |
+
"Choose a file",
|
| 48 |
+
type=file_type,
|
| 49 |
+
key="uploaded_files",
|
| 50 |
+
accept_multiple_files=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# -- Layout for query and result
|
| 54 |
+
sql_query = st.text_area(label="Sql Query", value="", key="sql_query", height=200)
|
| 55 |
+
if st.button("Run SQL Query"):
|
| 56 |
+
process_input()
|
| 57 |
+
try:
|
| 58 |
+
result_df = psql.sqldf(sql_query, globals())
|
| 59 |
+
st.write("Query Result")
|
| 60 |
+
st.dataframe(result_df)
|
| 61 |
+
csv_data = result_df.to_csv(index=False)
|
| 62 |
+
b64 = base64.b64encode(csv_data.encode()).decode()
|
| 63 |
+
st.markdown(f'<a href="data:file/csv;base64,{b64}" download="result.csv">Download Result CSV</a>', unsafe_allow_html=True)
|
| 64 |
+
except Exception as e:
|
| 65 |
+
st.error(f"Error executing SQL query: {str(e)}")
|