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.

1 Like

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

1 Like

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),
    )
1 Like

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:

1 Like