Issue with selectbox

So, my issue is pretty simple.
Let’s say you have a calendar, you have a selectbox in which elements are
0 - Monday,
1 - Tuesday,

30 - Someday
Now, you want to be able to search for a specific day (st.selectbox does it natively with the keyboard), and also you want to have 2 buttons, previous and next that help you navigate in between the calendar. I want the buttons to change the label of the selectbox.

My solution is below, but I have some issues. In particular, if I do a combination of filtering the list with something like “Sund”, pressing a button, and changing the page, everything doesn’t work anymore, the indices go to 0 or exceed the maximum of the list length. Do you have any workaround?

import streamlit as st

calendar_clmns = st.columns([1, 5, 1])

if "curr_day" not in st.session_state:
    st.session_state["curr_day"] = 0
    
with calendar_clmns[0]:
    # Prev
    if st.button("Precedente", use_container_width=True):
        st.session_state["curr_day"] = max(0, st.session_state["curr_day"] - 1)

with calendar_clmns[2]:
    # Succ
    if st.button("Successivo", use_container_width=True):
        st.session_state["curr_day"] = min(30, st.session_state["curr_day"] + 1)

with calendar_clmns[1]:
    selected_option = st.selectbox("Seleziona il possesso da visualizzare:", 
                                    index=st.session_state["curr_day"], 
                                    options=[str(i) + " - " + day[i]  for i in range(31)], 
                                    label_visibility="collapsed")

selected_day = int(selected_option.split(" - ")[0])
if selected_day != st.session_state["curr_day"]:
    st.session_state["curr_day"] = selected_day

@Nicola_Lombardi, I was not able to reproduce the problem. My guess is that the logic here is not failing but in how the list day is defined in your code - however that is not shown in the snippet you provided.

selectbox with buttons

basically if I seach for monday multiple times and selecting always the same entry, I get random results

Thank you so much, you put me in the right direction.
Basically, my strings were values from a dictionary, and I was getting them using .values(), extracting the portion before the " - " and using that as an index. Unfortunately, indexes were not corresponding, so I fixed that by converting to a list and using the .index() method.

1 Like