Label and values in in selectbox

hello
is it possible to have lookup values in a selectbox. I want the selectbox to display e.g. “dataset a”, “dataset b”, “dataset c” but associate a numeric value (1,2,3) with each of them so if selected, the selectbox returns a number. in html this would be: dataset a. in case this is not possible, would you want me to add it to the requested features in git? Thanks and happy Xmas everyone.

1 Like

Hi @godot63,

Happy holidays to you as well!

It’s not possible to do so at this time.

There is a format_func attribute that allows you to format the display value separately from the return value, but I’m not seeing a way to use it to achieve exactly what you want.

Feel free to file a feature request on Github issues! Please include your use case as well to illustrate what you’re trying to do and why the current API isn’t suitable.

We had actually initially designed the API to work as you described then switched it to how it is now, based on user feedback.

1 Like

hi Jonathan_Rhone,
Thanks for your fast reply. Good to know that this is not available, at least for a while. It’s not a problem since there are easy workarounds, it just asks a bit more querying the database. I will continue for a while and file a feature request once I have gained more experience with Streamlit, and if at that point, I am still convinced that this would be a useful feature.

Hey @godot63,

Looks like you could actually use format_func in this way to do what you want.

import streamlit as st

display = ("male", "female")

options = list(range(len(display)))

value = st.selectbox("gender", options, format_func=lambda x: display[x])

st.write(value)

amazing, this works great, thank you very much!

1 Like

Ahh. Did not get to the example by @Jonathan_Rhone so I created this one

import streamlit as st

CHOICES = {1: "dataset a", 2: "dataset b", 3: "dataset c"}


def format_func(option):
    return CHOICES[option]


option = st.selectbox("Select option", options=list(CHOICES.keys()), format_func=format_func)
st.write(f"You selected option {option} called {format_func(option)}")

Thanks, Marc, that was my workaround too, but I like Jonathans solution!