Dynamic choice in st.sidebar.selectbox()

I have the following code in my app

company = df_query['Company_Name'].unique() 
company_choice = st.sidebar.selectbox('Select Company Name:', company)

report = 'Report Type','List','Summary' 
report_choice = st.sidebar.selectbox('Type of Report:', report)

year = df_query['Year'].unique().astype(int)
year_choice = st.sidebar.selectbox('Year', year)

Then in the if statment I have

if company_choice == company_choice[0]:

but it is not recognized also, I had it with ’ ’ same result.

The query produce the following result

company_choice = df_query['Company_Name'].unique()
company_choice
array(['All', 'Company 1', 'Company 2'], dtype=object)

then printing
company_choice[0] shows :‘All’

How can I use company_choice [ ] in my if statment

Regards

I’m not clear on what you’re trying to do here. This doesn’t make sense to me:

if company_choice == company_choice[0]:

Are you trying to check if company_choice == company[0]?

Hi,

I want yo use the query result d perform activities inside the if statment. if company_choice == the fist element in the array perform this if the second element in the array to that.

this is the full code

st.markdown(f"<h1 style='{style_heading}'>Ridesharing Income</h1>", unsafe_allow_html=True)

company = df_query['Company_Name'].unique() 
company_choice = st.sidebar.selectbox('Select Company Name:', company)


report = 'Report Type','List','Summary' 
report_choice = st.sidebar.selectbox('Type of Report:', report)

year = df_query['Year'].unique().astype(int)
year_choice = st.sidebar.selectbox('Year', year)

if company_choice == company_choice[0]:
    st.markdown(f"<h2 style='{style_heading}'>All Companies Income Reports</h2>", unsafe_allow_html=True)

if company_choice == company_choice[1]:
    Do this ....

if company_choice == company_choice[2]:
   Do that ......

if company_choice == company_choice[2]:
    Do something else
.
.
.

1 Like

have you try st.pills ?

Nope, I am looking into now

I still don’t understand:

if company_choice == company_choice[i]:

company_choice is a string. company is the list of choices. I think you mean

if company_choice == company[i]:
1 Like

Which array?

Yeah I agree with @mathcatsand here, it sounds like that’s what you are trying to do

Btw, you can also put your if statements into a function that you assign to on_change if you wanted to have those statements execute before the entire page re-runs. If so, you would also need to assign a key to selectbox and can then access the result through the session state.

company_choice = st.sidebar.selectbox('Select Company Name:', company, key='company_choice', on_change=if_function, args=(company, ))
def if_function(company: list):
    if st.session_state.company_choice == company[0]:
        st.markdown(f"<h2 style='{style_heading}'>All Companies Income Reports</h2>", unsafe_allow_html=True)
...

I am trying to avoid hardcoding. Trying to use elements in a list or array to populate the choices