Ninad077 commited on
Commit
434ad17
·
verified ·
1 Parent(s): ff0c37e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from typing import List
4
+ from io import BytesIO
5
+ from tempfile import NamedTemporaryFile
6
+
7
+ # Function to append files
8
+ def append_files(files: List[BytesIO]):
9
+ dfs = []
10
+ file_dimensions = []
11
+ file_names = []
12
+
13
+ for file in files:
14
+ try:
15
+ if file.name.endswith('.xls') or file.name.endswith('.xlsx'):
16
+ df = pd.read_excel(file)
17
+ elif file.name.endswith('.csv'):
18
+ df = pd.read_csv(file)
19
+
20
+ # Store dimensions and filename of each file
21
+ file_dimensions.append(df.shape)
22
+ file_names.append(file.name)
23
+ dfs.append(df)
24
+
25
+ except Exception as e:
26
+ st.error(f"Error reading file {file.name}: {e}")
27
+ return None, [], []
28
+
29
+ if len(dfs) == 0:
30
+ st.warning("No valid files found to append.")
31
+ return None, [], []
32
+
33
+ try:
34
+ appended_df = pd.concat(dfs, ignore_index=True)
35
+ appended_dimensions = appended_df.shape
36
+ except Exception as e:
37
+ st.error(f"Error appending files: {e}")
38
+ return None, [], []
39
+
40
+ return appended_df, file_names, file_dimensions, appended_dimensions
41
+
42
+ # Streamlit UI
43
+ def main():
44
+ st.title(':blue[Append Excel/CSV Files]')
45
+ st.write("")
46
+ uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True)
47
+
48
+ if st.button("Append Files") and uploaded_files:
49
+ st.write("")
50
+ files_data = [file for file in uploaded_files]
51
+ appended_data, file_names, file_dimensions, appended_dimensions = append_files(files_data)
52
+
53
+ if appended_data is not None:
54
+ st.subheader(":blue[Dimensions of Uploaded Files:]")
55
+ for i, (filename, dim) in enumerate(zip(file_names, file_dimensions), start=1):
56
+ st.write(f"{filename}: {dim[0]} rows, {dim[1]} columns")
57
+
58
+ st.subheader("\n:blue[Dimensions of Appended Data:]")
59
+ st.write(f"Appended Data: {appended_dimensions[0]} rows, {appended_dimensions[1]} columns")
60
+ st.write("")
61
+
62
+ st.subheader("\n:blue[Appended Data:]")
63
+ st.write(appended_data)
64
+
65
+ # Allow user to download the appended file as CSV
66
+
67
+ col1, col2 = st.columns([0.5, 1.2])
68
+
69
+ with col1:
70
+
71
+ with NamedTemporaryFile(delete=False, suffix='.csv') as temp_csv:
72
+ appended_data.to_csv(temp_csv.name, index=False)
73
+ st.download_button(label="Download Appended CSV", data=temp_csv.name, file_name='appended_data.csv', mime='text/csv')
74
+
75
+ with col2:
76
+ # Allow user to download the appended file as Excel (xlsx)
77
+ with NamedTemporaryFile(delete=False, suffix='.xlsx') as temp_xlsx:
78
+ appended_data.to_excel(temp_xlsx.name, index=False)
79
+ st.download_button(label="Download Appended Excel", data=temp_xlsx.name, file_name='appended_data.xlsx', mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
80
+
81
+ else:
82
+ st.warning("Unable to append files. Please check your files and try again.")
83
+
84
+ if __name__ == '__main__':
85
+ main()