-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.py
87 lines (70 loc) · 2.48 KB
/
app2.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
import cachetools
import cv2
import numpy as np
import os
try:
from enum import Enum
from io import BytesIO
import os
import pandas as pd
import streamlit as st
import shutil
except Exception as e:
print(e)
STYLE = """
<style>
img {
max-width: 100%;
}
</style>
"""
class FileType(Enum):
CSV = "csv"
PNG = "png"
JPG = "jpg"
class FileUpload(object):
def __init__(self):
self.fileTypes = [FileType.PNG, FileType.JPG]
self.uploaded_files_dir = "uploaded_imges" # Replace with your desired directory name
def save_uploaded_file(self, file, filename):
os.makedirs(self.uploaded_files_dir, exist_ok=True)
filepath = os.path.join(self.uploaded_files_dir, filename)
with open(filepath, 'wb') as f:
f.write(file.getvalue())
return filepath
def run(self):
"""
Upload File on Streamlit Code
:return:
"""
st.info(__doc__)
st.markdown(STYLE, unsafe_allow_html=True)
file = st.file_uploader("Upload file", type=[file_type.value for file_type in self.fileTypes],accept_multiple_files=True)
print(os.getcwd())
#file_array=np.array(file)
#parent_directory=r'C:\Users\glitcher\Desktop\Rj_hackathon_2\venv\uploaded_imges'
file_path=os.path.join(os.getcwd(),'venv','uploaded_imges')
#cv2.imwrite(file_path, file_array)
if file is not None:
file_details = {"FileName":file.name,"FileType":file.type}
st.write(file_details)
with open(os.path.join(file_path,file.name),"wb") as f:
f.write(file.getbuffer())
st.success("Saved File")
show_file = st.empty()
if not file:
show_file.info(f"Please upload a file of type: {', '.join(file_type.value for file_type in self.fileTypes)}")
return
content = file.getvalue()
if isinstance(file, BytesIO):
show_file.image(file)
# Save the uploaded file to the directory
uploaded_filepath = self.save_uploaded_file(file, file.filename)
st.success(f"File successfully uploaded and saved to: {uploaded_filepath}")
else:
data = pd.read_csv(file)
st.dataframe(data.head(10))
file.close()
if __name__ == "__main__":
helper = FileUpload()
helper.run()