Format_func (function) - Examples Please

Can someone please help with an example for format_func ( function ) that is referenced in the API document for selectbox, radiobutton, multiselect etc and how to use it?

Would be nice if the API doc included an example for all the parameters for the methods.

Thank you.

4 Likes

Hi @madras -

Youโ€™re right, we do need to improve that part of the docs (I think there might even be an open GitHub issue for that).

format_func allows you to modify the display of the value within the widget, while returning the original value. See this example:

 import streamlit as st


btn = st.multiselect(
    "Some text", [2, 4, 6, 8, 10], format_func=lambda x: "option " + str(x)
)

btn

The app will show the following:

In the Python code, I defined a lambda function that takes the input and adds the string โ€œoptionโ€ to the front of it. When the word selections are shown in the widget, it still returns the original value to the Python variable btn.

This can be useful in situations where you have keys that mean something, but are stored as integers in a process (common in databases). So you could pass a dict to the format_func that you define and do a key/value lookup, you could do a simple text transformation like I did, or anything else that is valid Python.

Best,
Randy

4 Likes

Thank you @randyzwitch

Would a format_func() be of any use in the scenario below? If not, how to do this?

DataFrame:

StoreID City State
1 Seattle Washington
2 Portland Oregon
3 Newark NewJersey
4 Phoenix Arizona
5 Chandler Arizona

I want the selectbox to display the options in the following format (Sorted by State then by City):
Arizona - Chandler - 5
Arizona - Phoenix - 4
New Jersey - Newark -3
Oregon - Portland - 2
Seattle - Washington - 1

while the value for each option should be their corresponding StoreID.

While I get the displaying of options correctly, I canโ€™t seem to associate the StoreID to the options. The value of the option I select is always the actual string being displayed like Seattle - Washington - 1 instead of just 1.

How do to this? Is Format_func() useful here?

Thank you.

As @randyzwitch has mentioned, using a dict might be the solution. I have just applied one in my app and it worked pretty good.

If I didnโ€™t misunderstand your case, the following code should do what you want:

city_options = {
    5: "Arizona - Chandler - 5",
    4: "Arizona - Phoenix - 4",
    3: "New Jersey - Newark -3",
    2: "Oregon - Portland - 2",
    1: "Seattle - Washington - 1",
    }

city_mode = st.sidebar.radio(
    label="Choose a city option:",
    options= (5, 4, 3, 2, 1),
    format_func=lambda x: city_options.get(x),
    )
2 Likes

Here is a minimal working code to test the options.

import streamlit as st

city_options = { 
    5: "Arizona - Chandler - 5",
    4: "Arizona - Phoenix - 4",
    3: "New Jersey - Newark -3",
    2: "Oregon - Portland - 2",
    1: "Seattle - Washington - 1",
    }   

city_mode = st.sidebar.radio(
    label="Choose a city option:",
    options= (5, 4, 3, 2, 1), 
    format_func=lambda x: city_options.get(x),
    )   

st.write(f'You have chosen {city_options.get(city_mode)}'
        f' with the value {city_mode}.')

Screenshot:

3 Likes

I didnโ€™t know you could do this, thanks Randy! :pray:

1 Like
key_values = {
    1: "one",
    2: "two",
    3: "three"
}
st.radio("Pick a number",
        options=key_values.keys(),
        format_func=lambda x: "{}: {}".format(key_values.get(x),x)
    )

The param is very important for me.

Example:

import streamlit as st
from dataclasses import dataclass


@dataclass
class Animal:
    id: str
    name: str
    date: str


if 'animals' not in st.session_state:
    st.session_state.animals = (
        Animal('1', 'cat', '20231201'), Animal('2', 'dog', '20231201'), Animal('3', 'fish', '20231201'))

animals = st.session_state.animals

args = (info.name for info in animals)

animals = st.selectbox('Select one', options=animals, format_func=lambda animal: animal.name)
st.header(f'Selected: {animals}')