Skip to content

Commit

Permalink
Small refactor/tidy up including variable names changed to be more de…
Browse files Browse the repository at this point in the history
…scriptive and a key being added to the file uploader.
  • Loading branch information
hreikin committed Mar 14, 2023
1 parent 603df56 commit 343b1be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
48 changes: 25 additions & 23 deletions pages/Uploads.py
Expand Up @@ -44,62 +44,64 @@ class UploadFiles():
expander (bool): A bool() used to set the initial state of the expander, only used when using the "expander" widget_type.
file_extensions (list): A list() containing strings of the file extensions to include in the library, default is (".png", ".jpg", ".jpeg").
info_msg (str): A str() used to set an info message above the uploader, default is "Upload new files here.".
label (str): A str() used to set the label of the "expander" or the header in the "container" type widget, default is "Upload Files", can be set to None to not display it.
upload_label (str): A str() used to set the label of the file uploader widget, default is "Upload Files".
widget_type (str): A str() defining the type of widget to use to display te file uploader, options are "container" or "expander", default is "container".
header (str): A str() used to set the header of the "expander" or the header in the "container" type widget, default is "Upload Files", can be set to None to not display it.
uid (str): A str() containing a unique identifier allowing you to create multiple file uploaders on the same page.
upload_label (str): A str() used to set the label of the file uploader widget, default is "Upload Files", can be set to None to display an empty string instead.
widget_type (str): A str() defining the type of widget to use to display the file uploader, options are "container" or "expander", default is "container".
"""
def __init__(self, save_location, expanded=True, file_extensions=["png", "jpg", "jpeg"], info_msg="Upload new files here.", label="Upload Files", upload_label="Upload Files", widget_type="container"):
def __init__(self, save_location, expanded=True, file_extensions=["png", "jpg", "jpeg"], header="Upload Files", info_msg="Upload new files here.", uid="files", upload_label="Upload Files", widget_type="container"):
self.save_location = save_location
self.expanded = expanded
self.file_extenions = file_extensions
self.file_extensions = file_extensions
self.header = header
self.info_msg = info_msg
self.label = label
self.widget_type = widget_type
self.uid = uid
self.upload_label = upload_label
self.uploader = self.create_layout()
self.widget_type = widget_type
self.uploader = self.create_layout(self.expanded, self.file_extensions, self.header, self.info_msg, self.uid, self.upload_label, self.widget_type)
if self.uploaded_files is not None:
self.save_uploaded_files(self.uploaded_files, self.save_location)
def create_layout(self):
def create_layout(self, expanded, file_extensions, header, info_msg, uid, upload_label, widget_type):
"""Creates the file uploader widget layout.
Creates a file uploader widget using either a container or expander.
Returns:
upload_options (st.container or st.expander): The root widget for the file uploader layout.
"""
if self.widget_type == "expander":
if self.label == None:
self.label = ""
self.upload_options = st.expander(label=self.label, expanded=self.expanded)
else:
self.upload_options = st.expander(label=self.label, expanded=self.expanded)
if widget_type == "expander":
if header == None:
header = ""
self.upload_options = st.expander(label=header, expanded=expanded)
else:
self.upload_options = st.container()
with self.upload_options:
if self.label == None:
if header == None:
pass
else:
self.gallery_label = st.markdown(f"**{self.label}**")
self.gallery_header = st.markdown(f"**{header}**")
with self.upload_options:
self.upload_options_msg = st.info(self.info_msg)
self.uploaded_files = st.file_uploader(label=self.upload_label, accept_multiple_files=True, type=self.file_extenions, help="Upload a new file.")
self.upload_options_msg = st.info(info_msg)
if upload_label == None:
upload_label = ""
self.uploaded_files = st.file_uploader(label=upload_label, key=f"{uid}_upload_widget", accept_multiple_files=True, type=file_extensions, help="Upload a new file.")
return self.upload_options
def save_uploaded_files(self, files_to_upload, destination):
"""Saves the uploaded files.
Saves the files selected using the file uploader to the directory provided.
Saves the file(s) selected using the file uploader to the directory provided.
Args:
files_to_upload (list): A list() of files retuned by the st.file_uploader widget.
files_to_upload (list): A list() of file(s) returned by the st.file_uploader widget.
destination (str): A str() pointing to the directory to save the uploaded files.
"""
for file in files_to_upload:
self.full_path = Path(f"{destination}/{file.name}")
full_path = Path(f"{destination}/{file.name}")
with Image.open(file) as f:
f.save(self.full_path)
f.save(full_path)
st.cache_resource.clear()
'''

Expand Down
46 changes: 24 additions & 22 deletions src/streamlit_uploads_library/uploads.py
Expand Up @@ -25,60 +25,62 @@ class UploadFiles():
expander (bool): A bool() used to set the initial state of the expander, only used when using the "expander" widget_type.
file_extensions (list): A list() containing strings of the file extensions to include in the library, default is (".png", ".jpg", ".jpeg").
info_msg (str): A str() used to set an info message above the uploader, default is "Upload new files here.".
label (str): A str() used to set the label of the "expander" or the header in the "container" type widget, default is "Upload Files", can be set to None to not display it.
upload_label (str): A str() used to set the label of the file uploader widget, default is "Upload Files".
header (str): A str() used to set the header of the "expander" or the header in the "container" type widget, default is "Upload Files", can be set to None to not display it.
uid (str): A str() containing a unique identifier allowing you to create multiple file uploaders on the same page.
upload_label (str): A str() used to set the label of the file uploader widget, default is "Upload Files", can be set to None to display an empty string instead.
widget_type (str): A str() defining the type of widget to use to display the file uploader, options are "container" or "expander", default is "container".
"""
def __init__(self, save_location, expanded=True, file_extensions=["png", "jpg", "jpeg"], info_msg="Upload new files here.", label="Upload Files", upload_label="Upload Files", widget_type="container"):
def __init__(self, save_location, expanded=True, file_extensions=["png", "jpg", "jpeg"], header="Upload Files", info_msg="Upload new files here.", uid="files", upload_label="Upload Files", widget_type="container"):
self.save_location = save_location
self.expanded = expanded
self.file_extenions = file_extensions
self.file_extensions = file_extensions
self.header = header
self.info_msg = info_msg
self.label = label
self.widget_type = widget_type
self.uid = uid
self.upload_label = upload_label
self.uploader = self.create_layout()
self.widget_type = widget_type
self.uploader = self.create_layout(self.expanded, self.file_extensions, self.header, self.info_msg, self.uid, self.upload_label, self.widget_type)

if self.uploaded_files is not None:
self.save_uploaded_files(self.uploaded_files, self.save_location)

def create_layout(self):
def create_layout(self, expanded, file_extensions, header, info_msg, uid, upload_label, widget_type):
"""Creates the file uploader widget layout.
Creates a file uploader widget using either a container or expander.
Returns:
upload_options (st.container or st.expander): The root widget for the file uploader layout.
"""
if self.widget_type == "expander":
if self.label == None:
self.label = ""
self.upload_options = st.expander(label=self.label, expanded=self.expanded)
else:
self.upload_options = st.expander(label=self.label, expanded=self.expanded)
if widget_type == "expander":
if header == None:
header = ""
self.upload_options = st.expander(label=header, expanded=expanded)
else:
self.upload_options = st.container()
with self.upload_options:
if self.label == None:
if header == None:
pass
else:
self.gallery_label = st.markdown(f"**{self.label}**")
self.gallery_header = st.markdown(f"**{header}**")
with self.upload_options:
self.upload_options_msg = st.info(self.info_msg)
self.uploaded_files = st.file_uploader(label=self.upload_label, accept_multiple_files=True, type=self.file_extenions, help="Upload a new file.")
self.upload_options_msg = st.info(info_msg)
if upload_label == None:
upload_label = ""
self.uploaded_files = st.file_uploader(label=upload_label, key=f"{uid}_upload_widget", accept_multiple_files=True, type=file_extensions, help="Upload a new file.")
return self.upload_options

def save_uploaded_files(self, files_to_upload, destination):
"""Saves the uploaded files.
Saves the files selected using the file uploader to the directory provided.
Saves the file(s) selected using the file uploader to the directory provided.
Args:
files_to_upload (list): A list() of files retuned by the st.file_uploader widget.
files_to_upload (list): A list() of file(s) returned by the st.file_uploader widget.
destination (str): A str() pointing to the directory to save the uploaded files.
"""
for file in files_to_upload:
self.full_path = Path(f"{destination}/{file.name}")
full_path = Path(f"{destination}/{file.name}")
with Image.open(file) as f:
f.save(self.full_path)
f.save(full_path)
st.cache_resource.clear()

0 comments on commit 343b1be

Please sign in to comment.