Problem with st.selectbox, can't get the new value

Hello community! It’s me again!

I have a problem, I’m trying to retrieve the label that the user would like to modify. I use a selectbox to get the list of possible labels.
But when the new label is selected, it retrieves the old one, and not the new one.

Here my code :

def select_correct_glyph(rowImgDet, idx: int):
    all_mzl = all_mzl_info('list')
    default_index = 0

    if st.session_state.zip_detect[idx][1][0]:
        default_index = st.session_state.zip_detect[idx][1][0] - 1
    
    print(st.session_state.zip_detect[idx][1][0])
    print(default_index)
    print(all_mzl[default_index])
    new_label = rowImgDet.selectbox("Correct Label",
                                    options=all_mzl, 
                                    key=f"label_{idx}", 
                                    index=default_index,
                                    label_visibility='hidden')

    return new_label


clear_session_state('preview_imgs')
tmp_img.empty()

# all_mzl = all_mzl_info('list')
# default_index = 0

## - Correct the label of the detected glyphs
for i, result in enumerate(st.session_state.zip_detect):
    space()
    rowImgDet = row([0.3, 0.7], gap='small')
  
    rowImgDet.image(result[0])
    test = select_correct_glyph(rowImgDet, i)
    print(test)

If you have any ideas about the problem, I’d love to hear from you! Thanks in advance! :smiling_face:

Could you post a minimal reproducible code?

The dataflow in the basic concept has to be understood.

Thanks @ferdy, I’ll try to give you that :sweat_smile:

from streamlit_extras.row import row

def update_zip_detect(index, new_prediction):
    updated_zip_detect = st.session_state.zip_detect[:]
    updated_zip_detect[index] = (updated_zip_detect[index][0], new_prediction)
    st.session_state.zip_detect = updated_zip_detect


def select_correct_glyph(rowImgDet, idx: int):
    all_mzl = ["1 - 𒁹 DIS", "2 - 𒌋 U"]
    default_index = None

    if st.session_state.zip_detect[idx][1][0]:
        default_index = st.session_state.zip_detect[idx][1][0] - 1

    print("0", st.session_state.zip_detect[idx][1][0])

    new_label = rowImgDet.selectbox("Correct Label",
                            all_mzl, 
                            key=f"label_{idx}", 
                            index=default_index,
                            label_visibility='hidden')
    print("1", new_label)

    new_label = int(new_label.split(" ")[0])
    print("2", new_label)

    if new_label != st.session_state.zip_detect[idx][1][0]:
        mzl_information = mzl_info(new_label['mzl_number'])
        pred_result = [
            mzl_information['mzl_number'],
            mzl_information['glyph'],
            mzl_information['glyph_name'],
        ]

        update_zip_detect(idx, pred_result)
        print("3", st.session_state.zip_detect[idx][1][0])


pred_res = ["1", glyph, '𒁹', [830,1700, 865, 1800])
st.session_state.zip_detect.append((["IMG"], pred_res))

for i, result in enumerate(st.session_state.zip_detect):
   rowImgDet = row([0.3, 0.7], gap='small')

   rowImgDet.image(result[0])
   select_correct_glyph(rowImgDet, i)

That's more or less the basic logic. A prediction is made, I put everything in the zip_detect (one line = one prediction), and then if the person presses a button, the predictions go into the selectbox to change the result (in this case the prediction becomes a label), but when the selection of the new glyph is made it doesn't retrieve it, because it retrieves the one present in the index.

I don’t know if I’ve clarified the problem. :woozy_face:

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