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.

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)


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.