Skip to content

Commit

Permalink
Fix caching with class inheritance.
Browse files Browse the repository at this point in the history
  • Loading branch information
hreikin committed Mar 31, 2023
1 parent 8abe8a7 commit d8576e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 50 deletions.
43 changes: 5 additions & 38 deletions streamlit_uploads_library/gallery.py
Expand Up @@ -4,9 +4,10 @@
class Gallery(Library):
"""Create a simple gallery out of streamlit widgets.
Using the gallery is simple, import `streamlit_uploads_library` and then instantiate the class with the
required `directory` variable. Other options can be configured by passing in different variables
when instantiating the class.
A wrapper around the `Library` class to create a gallery. Using the gallery is simple, import
`streamlit_uploads_library` and then instantiate the class with the required `directory`
variable. Other options can be configured by passing in different variables when instantiating
the class.
Example Usage:
python
Expand All @@ -31,38 +32,4 @@ def __init__(self, directory, file_extensions=(".png", ".jpg", ".jpeg"), image_a
self.number_of_columns = number_of_columns
self.show_details = show_details
self.uid = uid
super(Gallery, self).__init__(self.directory, self.file_extensions, self.image_alignment, self.number_of_columns, self.show_details, self.uid)

def fetch_files(self, directory, file_extensions):
"""Returns a list of all files.
Returns a list of files to be used by create_gallery().
Args:
directory (str): A str() of the path to the folder containing the gallery images, for example, "assets".
file_extensions (tuple): A tuple() containing strings of the file extensions to include in the library, default is (".png", ".jpg", ".jpeg").
Returns:
all_files (list): A list of files.
all_filenames (list): A list of filenames.
"""
return super().fetch_files(directory, file_extensions)

@st.cache_resource(experimental_allow_widgets=True, show_spinner="Refreshing gallery...")
def create_gallery(_self, directory, file_extensions, image_alignment, number_of_columns, show_details, uid):
"""Creates a simple gallery with columns.
Creates a gallery using columns out of streamlit widgets.
Args:
directory (str): A str() of the path to the folder containing the gallery images, for example, "assets".
file_extensions (tuple): A tuple() containing strings of the file extensions to include in the gallery, default is (".png", ".jpg", ".jpeg").
image_alignment (str): A str() with the CSS keyword used to align the images and details columns.
number_of_columns (int): An int() indicating the number of columns to create.
show_details (bool): A bool() that when set to True allows the creation of libraries, default is False to create a gallery.
uid (str): A str() containing a unique identifier allowing you to create multiple libraries on the same page containing the same images.
Returns:
library_gallery_container (st.container): A streamlit widget containing the gallery.
"""
return super().create_library(directory, file_extensions, image_alignment, number_of_columns, show_details, uid)
super(Gallery, self).__init__(self.directory, self.file_extensions, self.image_alignment, self.number_of_columns, self.show_details, self.uid)
24 changes: 12 additions & 12 deletions streamlit_uploads_library/library.py
Expand Up @@ -36,7 +36,7 @@ def __init__(self, directory, file_extensions=(".png", ".jpg", ".jpeg"), image_a
self.number_of_columns = number_of_columns
self.show_details = show_details
self.uid = uid
self.library = self.create_library(self.directory, self.file_extensions, self.image_alignment, self.number_of_columns, self.show_details, self.uid)
self.root_container = self.create(directory=self.directory, file_extensions=self.file_extensions, image_alignment=self.image_alignment, number_of_columns=self.number_of_columns, show_details=self.show_details, uid=self.uid)

def fetch_files(self, directory, file_extensions):
"""Returns a list of all files.
Expand Down Expand Up @@ -80,7 +80,7 @@ def update_file(self, old_file, new_file, del_check=False):
st.cache_resource.clear()
st.experimental_rerun()

def create_details(_self, img, filename_idx, uid):
def create_details(self, img, filename_idx, uid):
"""Create the details section for each displayed image.
Creates a default details section for each image it is used on, can be overridden to create
Expand All @@ -99,21 +99,21 @@ def create_details(_self, img, filename_idx, uid):
details_col1, details_col2 = st.columns(2)
del_check = st.checkbox(label="Delete ?", key=f"{img_path.stem}_{uid}_del_check_{filename_idx}", help="Permanently delete a file from the library.")
if del_check:
st.button(label="Delete", key=f"{img_path.stem}_{uid}_delete_button_{filename_idx}", type="secondary", use_container_width=True, on_click=_self.update_file, args=(img_path, new_name, del_check))
st.button(label="Delete", key=f"{img_path.stem}_{uid}_delete_button_{filename_idx}", type="secondary", use_container_width=True, on_click=self.update_file, args=(img_path, new_name, del_check))
else:
st.button(label="Update", key=f"{img_path.stem}_{uid}_submit_button_{filename_idx}", type="primary", use_container_width=True, on_click=_self.update_file, args=(img_path, new_name, del_check))
st.button(label="Update", key=f"{img_path.stem}_{uid}_submit_button_{filename_idx}", type="primary", use_container_width=True, on_click=self.update_file, args=(img_path, new_name, del_check))
with details_col1:
st.text_input(label="Width:", key=f"{img_path.stem}_{uid}_width_{filename_idx}", value=f"{img_meta.width}", disabled=True)
with details_col2:
st.text_input(label="Height:", key=f"{img_path.stem}_{uid}_height_{filename_idx}", value=f"{img_meta.height}", disabled=True)
except get_image_size.UnknownImageFormat:
width, height = -1, -1

@st.cache_resource(experimental_allow_widgets=True, show_spinner="Refreshing library...")
def create_library(_self, directory, file_extensions, image_alignment, number_of_columns, show_details, uid):
"""Creates a simple library with columns.
@st.cache_resource(experimental_allow_widgets=True, show_spinner="Loading...")
def create(_self, directory, file_extensions, image_alignment, number_of_columns, show_details, uid):
"""Creates a simple library or gallery with columns.
Creates a library using columns out of streamlit widgets.
Creates a library or gallery using columns out of streamlit widgets.
Args:
directory (str): A str() of the path to the folder containing the library images, for example, "assets".
Expand All @@ -124,10 +124,10 @@ def create_library(_self, directory, file_extensions, image_alignment, number_of
uid (str): A str() containing a unique identifier allowing you to create multiple libraries on the same page containing the same images.
Returns:
library_gallery_container (st.container): A streamlit widget containing the library.
root_container (st.container): A streamlit widget containing the library.
"""
library_gallery_container = st.container()
with library_gallery_container:
root_container = st.container()
with root_container:
# To be able to display the images, details and buttons all in one row and aligned
# correctly so that images of different sizes don't affect the alignment of the details
# and buttons we need do some minor maths and keep track of multiple index values.
Expand Down Expand Up @@ -178,4 +178,4 @@ def create_library(_self, directory, file_extensions, image_alignment, number_of
col_idx = 0
library_rows_idx += 1
filename_idx += 1
return library_gallery_container
return root_container

0 comments on commit d8576e3

Please sign in to comment.