Display image based on user selection

Hello, I want to display the image based on the item the user selects. Right now, I’m only able to do it manually by using x amount of else if statements. Ideally, I do not want to do that if I have like hundreds of items.

I tried to use loops but I didn’t manage to get it to work. Please assist, thank you.

sample dataset:

Below are the codes:

import streamlit as st
import pandas as pd

df = pd.read_csv(‘phones.csv’)
image_list = df.iloc[:, 0].unique().tolist()
item_list = df.iloc[:, 1].unique().tolist()

user_option = st.selectbox(‘Choose an item’, [‘–Select–’] + item_list)
if user_option == ‘–Select–’:
st.info(‘You can view the image here’)
elif user_option == item_list[0]:
st.image(image_list[0])
elif user_option == item_list[1]:
st.image(image_list[1])
elif user_option == item_list[2]:
st.image(image_list[2])
elif user_option == item_list[3]:
st.image(image_list[3])
elif user_option == item_list[4]:
st.image(image_list[4])

You can make a dict from the lists:

images = dict(zip(item_list, image_list))

user_option = st.selectbox(‘Choose an item’, [’–Select–’] + item_list)

st.image(images[user_option])

Best,
Randy

@randyzwitch Thank you!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.