st.menu_button
Display a dropdown menu button widget.
A menu button displays a button which, when clicked, opens a dropdown menu with multiple options. Selecting an option triggers a rerun and returns the selected option value. If you open and close the dropdown menu without selecting an option, the app doesn't rerun.
st.menu_button behaves like st.button except that it returns one of multiple options when triggered instead of only True. Unlike st.selectbox, the button label remains unchanged after a selection, and the return value reverts to None on the next rerun, until the menu button is triggered again.
| Function signature[source] | |
|---|---|
st.menu_button(label, options, *, key=None, help=None, on_click=None, args=None, kwargs=None, type="secondary", icon=None, disabled=False, width="content", format_func=special_internal_function) | |
| Parameters | |
label (str) | A short label explaining to the user what this button is for. The label can optionally contain GitHub-flavored Markdown of the following types: Bold, Italics, Strikethroughs, Inline Code, Links, and Images. Images display like icons, with a max height equal to the font height. Unsupported Markdown elements are unwrapped so only their children (text contents) render. Display unsupported elements as literal characters by backslash-escaping them. E.g., "1\. Not an ordered list". See the body parameter of st.markdown for additional, supported Markdown directives. |
options (Iterable) | Labels for the menu options in an Iterable. This can be a list, set, or anything supported by st.dataframe. If options is dataframe-like, the first column will be used. Each label will be cast to str internally by default. Labels can include markdown as described in the label parameter. |
key (str, int, or None) | An optional string or integer to use as the unique key for the widget. If this is None (default), a key will be generated for the widget based on the values of the other parameters. No two widgets may have the same key. Assigning a key stabilizes the widget's identity and preserves its state across reruns even when other parameters change. A key lets you access the widget's value via st.session_state[key] (read-only). For more details, see Widget behavior. Additionally, if key is provided, it will be used as a CSS class name prefixed with st-key-. |
help (str or None) | A tooltip that gets displayed when the button is hovered over. If this is None (default), no tooltip is displayed. The tooltip can optionally contain GitHub-flavored Markdown, including the Markdown directives described in the body parameter of st.markdown. |
on_click (callable) | An optional callback invoked when an option is selected. |
args (list or tuple) | An optional list or tuple of args to pass to the callback. |
kwargs (dict) | An optional dict of kwargs to pass to the callback. |
type ("primary", "secondary", or "tertiary") | An optional string that specifies the button type. This can be one of the following:
|
icon (str or None) | An optional emoji or icon to display next to the button label. If icon is None (default), no icon is displayed. If icon is a string, the following options are valid:
|
disabled (bool) | An optional boolean that disables the button if set to True. The default is False. |
width ("content", "stretch", or int) | The width of the menu button. This can be one of the following:
|
format_func (function) | Function to modify the display of the menu options. It receives the raw option as an argument and should output the label to be shown for that option. This has no impact on the return value of the menu button. |
| Returns | |
(any or None) | The selected option value when an option is clicked, or None if no option was clicked. This is a copy of the selected option, not the original. |
Examples
Still have questions?
Our forums are full of helpful information and Streamlit experts.
