streamlit / streamlit

Streamlit — A faster way to build and share data apps.
https://streamlit.io
Apache License 2.0
35.98k stars 3.11k forks source link

Streamlit 1.37.0 and 1.37.1 have a bug format_func corruption used dict in st.selectbox #9237

Closed bsnresearcher closed 3 months ago

bsnresearcher commented 3 months ago

Checklist

Summary

Streamlit 1.37.0 and 1.37.1 have a bug format_func corruption used dict in st.selectbox

Reproducible Code Example

Open in Streamlit Cloud

import streamlit as st

# Display title
st.title("Streamlit Selectbox with Dict Example")

# Options dictionary
# Below causes the problem that menu items were "1-0","2-0","3-0". "100-Value 1",...expected.
options = {
    "100": "Value 1",
    "200": "Value 2",
    "300": "Value 3"
}

# Below causes "IndexError: string index out of range"
options = {
    "1": "Value 1",
    "2": "Value 2",
    "3": "Value 3"
}

# TypeError: 'int' object is not subscriptable
options = {
    1: "Value 1",
    2: "Value 2",
    3: "Value 3"
}

# TypeError: 'int' object is not subscriptable
options = {
    100: "Value 1",
    200: "Value 2",
    300: "Value 3"
}

# Create selectbox
selected_key = st.selectbox("Choose an option:", options=options.items(), format_func=lambda x: f"{x[0]} - {x[1]}", key="test") # Error occurred above.

Steps To Reproduce

Run the above code in streamlit 1.37.0 and 1.37.1.

Expected Behavior

Item labels are generated following the format f"{x[0]} - {x[1]}". That is "Key Num-Vale".

Current Behavior

TypeError: 'int' object is not subscriptable

Is this a regression?

Debug info

Additional Information

No response

github-actions[bot] commented 3 months ago

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

raethlein commented 3 months ago

Hey @bsnresearcher, thanks for raising the issue! I have been able to reproduce it. It looks like it was broken by this commit: 45a732c4bfe700fc0d2741a97568f515e16b88e0 It seems like ensure_indexable was preserving the dict structure whereas now something else seems to happen with the dict_items

bsnresearcher commented 3 months ago

Streamlit helps my work. Thank you for its modification and provision of Streamlit package!