# Format\_func (function) - Examples Please

**URL:** <https://discuss.streamlit.io/t/format-func-function-examples-please/11295>\
**Category:** Using Streamlit\
**Tags:** selectbox, multiselect, radio-button\
**Created:** [March 28, 2021, 6:48am UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295 "2021-03-28T06:48:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![madras](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@madras](https://discuss.streamlit.io/u/madras)\
**Post date:** [March 28, 2021, 6:48am UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/1 "2021-03-28T06:48:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [April 1, 2021, 1:28pm UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/2 "2021-04-01T13:28:46Z")

</div>

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:

```python
 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:

 ![image](https://global.discourse-cdn.com/streamlit/original/2X/9/901917dae2f1cc3e7cfc08d607a15b5e88dd3318.png)

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

---

<div class="post-metadata">

**Author:** ![madras](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@madras](https://discuss.streamlit.io/u/madras)\
**Post date:** [May 4, 2021, 10:15pm UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/3 "2021-05-04T22:15:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hiankun](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/hiankun/32/8535_2.png) [@hiankun](https://discuss.streamlit.io/u/hiankun)\
**Post date:** [June 14, 2021, 7:32am UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/4 "2021-06-14T07:32:59Z")

</div>

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:

```auto
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),
    )

```

---

<div class="post-metadata">

**Author:** ![hiankun](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/hiankun/32/8535_2.png) [@hiankun](https://discuss.streamlit.io/u/hiankun)\
**Post date:** [June 14, 2021, 7:40am UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/5 "2021-06-14T07:40:06Z")

</div>

Here is a minimal working code to test the options.

```auto
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:

 ![2021-06-14-153854_810x181_scrot](https://global.discourse-cdn.com/streamlit/original/2X/e/eca61043d2628fdb62ada8aa03c2ee070f428009.png)

---

<div class="post-metadata">

**Author:** ![Charly\_Wargnier](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/charly_wargnier/32/1504_2.png) [@Charly\_Wargnier](https://discuss.streamlit.io/u/Charly_Wargnier)\
**Post date:** [March 21, 2022, 7:46pm UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/6 "2022-03-21T19:46:21Z")

</div>

I didn’t know you could do this, thanks Randy! 🙏

---

<div class="post-metadata">

**Author:** ![delreino](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@delreino](https://discuss.streamlit.io/u/delreino)\
**Post date:** [September 2, 2022, 4:14pm UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/7 "2022-09-02T16:14:11Z")

</div>

> [@madras](#):
>
> `2 Portland Oregon`  
> `3 Newark NewJersey`  
> `4 Phoenix Arizona`  
> `5 Chandler Arizona`

```
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)
    )

```

---

<div class="post-metadata">

**Author:** ![hao7Chen](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/hao7chen/32/35245_2.png) [@hao7Chen](https://discuss.streamlit.io/u/hao7Chen)\
**Post date:** [December 1, 2023, 10:09am UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/8 "2023-12-01T10:09:22Z")

</div>

## The param is very important for me.

Example:

```py
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}')

```

 ![image](https://global.discourse-cdn.com/streamlit/original/3X/8/3/83e284b82968cfa15668b866e1c857dd9c50885b.png)  
 ![image](https://global.discourse-cdn.com/streamlit/original/3X/9/4/94d1aada902282b152e533adcab4d36d2b854fc2.png)

---

<div class="post-metadata">

**Author:** ![Nick1](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/nick1/32/49507_2.png) [@Nick1](https://discuss.streamlit.io/u/Nick1)\
**Post date:** [October 2, 2024, 5:01am UTC](https://discuss.streamlit.io/t/format-func-function-examples-please/11295/9 "2024-10-02T05:01:45Z")

</div>

Just sending a nudge – mention of ‘needs to be updated’ (in docs) in 2021.  
It still hasn’t been. A good explainer already here, just need to migrate to docs
