How to disable the file within the Sidebox widget not getting processed by default?

Hi,

This is my first post in this Streamlit forum. Streamlit has been great so far and I was able to quickly create a webapp in comparison to Flask which took a long time. I ran into a problem where I have created a Sidebox widget with a file browser that has the ability to display all the files in the current working directory where the app is running. However when I start the app, the first file (in this case the image) is being processed by default. Is there a way I can modify the below code such that the image gets processed only when I click on the sidebox and then select a particular image/file.

def file_selector(folder_path):
	filenames = os.listdir(folder_path)
	selected_filename = st.sidebar.selectbox('Or select an example image', filenames)
	return os.path.join(folder_path, selected_filename)

image_selected = file_selector(folder_path = "./example_images")

if image_selected is not None:
	image = np.array(Image.open(image_selected))
	st.image(image)
	processed_image = predict.preprocess_image(image_selected)
	prediction = predict.model_predict(processed_image, model)
	st.write("### Predictions:")
	res = '%s : %s' % (prediction[0][0], prediction[0][1])
	st.write(res)
	st.write("### Description:")
	descr = predict.description(prediction)
	st.write(descr[0][1])

Welcome to the forums @upendrak !

For now, you could hack it by inserting an empty value at the beginning of your filenames array, and not run the script for that value. The empty string won’t be very visible in the selectbox :

def file_selector(folder_path):
    filenames = os.listdir(folder_path)
    filenames.insert(0, "") # <-- default empty
    selected_filename = st.sidebar.selectbox("Or select an example image", filenames)
    return os.path.join(folder_path, selected_filename)

image_selected = file_selector(folder_path="./images")

if image_selected is not None and image_selected != './images\\': # <-- check for empty
    image = np.array(Image.open(image_selected))
    st.image(image)