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.

2 Likes

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)
8 Likes

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)}")

4 Likes

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

Mark, this is a great solution! I simplified the implementation by using lambda for the function. The selectbox line becomes:

option = st.selectbox(“Select option”, CHOICES.keys(), format_func=lambda x:CHOICES[ x ])

When I do this for a multiselect box I will get this error:
TypeError: unhashable type: ‘list’

Anyone an idea too overcome this. I’d would be nice if option could be a list of the selected objectnames.

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

Am splitting a text into tokens and for each toke, creating a select box. How would I be able to save the token and the corresponding selected value? Here is my noob attempt.

checkpoint = “bert-base-uncased”

tokenizer = BertTokenizer.from_pretrained(checkpoint)

if “load_state” not in st.session_state:
st.session_state.load_state = False

#txt = “There is a leak on the third floor with the HVAC”
saved = False

txt = st.text_area(“”)
tokenize = st.button(“Tokenize”)
input_cols = st.container()

if tokenize or st.session_state.load_state:
st.session_state.load_state = True
tokens = tokenizer.tokenize(txt)
token_length = len(tokens)
print(token_length)
options = [“L”, “E”]

with input_cols:
    cols = st.columns(token_length)
    for ix, col in enumerate(cols):
        tokenboxes = col.selectbox(tokens[ix], key=ix, options=options)

save = st.button(“Save”)

if save:
st.write(tokenboxes[1]) # the key should exist from above but this index doesnt exist