Skip to content

Commit

Permalink
Create basic docstrings and update demo page with changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hreikin committed Mar 12, 2023
1 parent aa04095 commit 1e23857
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
42 changes: 40 additions & 2 deletions pages/Uploads.py
Expand Up @@ -17,7 +17,7 @@
},
)

source_code = """
source_code = '''
import streamlit as st
import logging
from pathlib import Path
Expand All @@ -26,6 +26,29 @@
logger = logging.getLogger(__name__)
class UploadFiles():
"""A file uploader with save functionality.
The file uploader comes with multiple options able to be configured including 2 different view
types. It is not required to use this and you can easily replace it with your own, it is provided
as a convenience so you dont need to create the code yourself or replicate it across multiple
projects.
Example usage:
import streamlit as st
from streamlit_uploads_library.uploads import UploadFiles
st.set_page_config(page_title="Streamlit Uploads Library")
default_uploader = UploadFiles(save_location="assets")
Args:
save_location (str): A str() of the path to the folder you wish to save images to, for example, "assets".
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".
"""
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"):
self.save_location = save_location
self.expanded = expanded
Expand All @@ -40,6 +63,13 @@ def __init__(self, save_location, expanded=True, file_extensions=["png", "jpg",
self.save_uploaded_files(self.uploaded_files, self.save_location)
def create_layout(self):
"""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 = ""
Expand All @@ -59,12 +89,20 @@ def create_layout(self):
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.
Args:
files_to_upload (list): A list() of files retuned 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}")
with Image.open(file) as f:
f.save(self.full_path)
st.cache_resource.clear()
"""
'''

with st.sidebar:
st.info("Welcome to the `streamlit-uploads-library` example app.")
Expand Down
38 changes: 38 additions & 0 deletions src/streamlit_uploads_library/uploads.py
Expand Up @@ -6,6 +6,29 @@
logger = logging.getLogger(__name__)

class UploadFiles():
"""A file uploader with save functionality.
The file uploader comes with multiple options able to be configured including 2 different view
types. It is not required to use this and you can easily replace it with your own, it is provided
as a convenience so you dont need to create the code yourself or replicate it across multiple
projects.
Example usage:
import streamlit as st
from streamlit_uploads_library.uploads import UploadFiles
st.set_page_config(page_title="Streamlit Uploads Library")
default_uploader = UploadFiles(save_location="assets")
Args:
save_location (str): A str() of the path to the folder you wish to save images to, for example, "assets".
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".
"""
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"):
self.save_location = save_location
self.expanded = expanded
Expand All @@ -20,6 +43,13 @@ def __init__(self, save_location, expanded=True, file_extensions=["png", "jpg",
self.save_uploaded_files(self.uploaded_files, self.save_location)

def create_layout(self):
"""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 = ""
Expand All @@ -39,6 +69,14 @@ def create_layout(self):
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.
Args:
files_to_upload (list): A list() of files retuned 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}")
with Image.open(file) as f:
Expand Down

0 comments on commit 1e23857

Please sign in to comment.