-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_io.py
48 lines (36 loc) · 1.61 KB
/
utils_io.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
import streamlit as st
import pandas as pd
def upload_csvs_to_dfs():
if 'is_uploaded' not in st.session_state:
set_upload_csv_state()
if not st.session_state.is_uploaded:
csv_files = st.file_uploader("Upload CSV files - bank statements, credit card statements, etc.",
accept_multiple_files=True, type=['csv'])
if csv_files:
for f in csv_files:
try:
df = pd.read_csv(f)
st.session_state.all_dfs.append(df)
st.session_state.uploaded_files.append(f.name)
except Exception as e:
st.error(f"Error processing {f.name}: {e}")
st.session_state.is_uploaded = True
st.rerun()
else:
file_list = '<br>'.join(st.session_state.uploaded_files)
st.write(f"Uploaded files:<br>{file_list}", unsafe_allow_html=True)
st.write(f"Please note: if you close or refresh this page, all unsaved changes will be lost.",
unsafe_allow_html=True)
return st.session_state.all_dfs
def set_upload_csv_state():
st.session_state.is_uploaded = False
st.session_state.all_dfs = []
st.session_state.uploaded_files = []
def save_df_to_csv(df):
csv = df.to_csv(index=False).encode('utf-8')
st.download_button(label="Download current CSV",
data=csv,
file_name='expenses_formated.csv',
mime='text/csv')
st.warning('''Save your work by downloading the CSV
Make sure you don't select unwanted filters!''')