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:
Image_Url | Item_Name |
---|---|
https://m.media-amazon.com/images/I/41sHexVm4NL._AC_US60_SCLZZZZZZZ__.jpg | Google Pixel 5A |
https://m.media-amazon.com/images/I/41YNaNTqJoL._AC_US60_SCLZZZZZZZ__.jpg | Google Pixel 6 |
https://m.media-amazon.com/images/I/41t1HM8UN8L._AC_US60_SCLZZZZZZZ__.jpg | Moto G Power |
https://m.media-amazon.com/images/I/41UuyU7HsPL._AC_US60_SCLZZZZZZZ__.jpg | Samsung Galaxy S20 |
https://m.media-amazon.com/images/I/31XThr46I6L._AC_US60_SCLZZZZZZZ__.jpg | SAMSUNG SM-A125FZKHXSP Galaxy A12 |
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])