Saaransh Menon
Saaransh Menon

Reputation: 416

Execute line of code when length of list changes Python

I am trying to show a certain piece of text when the list length is greater than one.

Initially when the streamlit app is run there are no files in the uploaded images list so I dont want to show the Uploaded Images text but when a person uploads images I want to show the "Uploaded Images :" text.

How do you listen to the length of a list and execute code according to its change.

My Code:

uploaded_files = st.file_uploader(
    "Choose samples to upload...", accept_multiple_files=True)

image_list = []
image_captions = []

#ignore this function
def add_border(input_image, border, color=0):
    img = Image.open(input_image)
    if isinstance(border, int) or isinstance(border, tuple):
        bimg = ImageOps.expand(img, border=border, fill=color)
        return bimg
    else:
        raise RuntimeError('Border is not an integer or tuple!')


for uploaded_file in uploaded_files:
    image = add_border(uploaded_file, border=10, color='white')
    image_list.append(image)
    image_captions.append(uploaded_file.name)

if len(image_list) > 1:
    st.write("Uploaded Images :")


st.image(image_list, width=125)

Upvotes: 1

Views: 90

Answers (1)

Andrew Giannoulis
Andrew Giannoulis

Reputation: 106

Here is my idea: since the image_list will contain as many images as the ones in uploaded_files you can simply do this:

count = 0 
for n, uploaded_file in enumerate(uploaded_files): 
   count += 1 
   # rest of ur code 

print("Uploaded Images:") 

Upvotes: 1

Related Questions