- Contents
- st.expander
- ExpanderContainer
st.expander
Insert a multi-element container that can be expanded/collapsed.
Inserts a container into your app that can be used to hold multiple elements and can be expanded or collapsed by the user. When collapsed, all that is visible is the provided label.
To add elements to the returned container, you can use the with notation (preferred) or just call methods directly on the returned object. See examples below.
By default, all content within the expander is computed and sent to the frontend, even if the expander is closed. To enable lazy execution where content only runs when the expander is open, use on_change="rerun" or pass a callable to on_change. The .open property indicates whether the expander is currently open, letting you conditionally render expensive content.
Note
To follow best design practices and maintain a good appearance on all screen sizes, don't nest expanders.
| Function signature[source] | |
|---|---|
st.expander(label, expanded=False, *, key=None, icon=None, width="stretch", on_change="ignore", args=None, kwargs=None) | |
| Parameters | |
label (str) | A string to use as the header for the expander. 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. Common block-level Markdown (headings, lists, blockquotes) is automatically escaped and displays as literal text in labels. See the body parameter of st.markdown for additional, supported Markdown directives. |
expanded (bool) | If True, initializes the expander in "expanded" state. Defaults to False (collapsed). |
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. When on_change is set to "rerun" or a callable, setting a key lets you read or update the expanded state via st.session_state[key]. For more details, see Widget behavior. |
icon (str, None) | An optional emoji or icon to display next to the expander label. If icon is None (default), no icon is displayed. If icon is a string, the following options are valid:
|
width ("stretch" or int) | The width of the expander container. This can be one of the following:
|
on_change ("ignore", "rerun", or callable) | How the expander should respond when the user expands or collapses it. This controls whether the expander tracks state and triggers reruns. on_change can be one of the following:
When the expander tracks state, it can't be used inside Streamlit cache-decorated functions. |
args (list or tuple or None) | An optional list or tuple of args to pass to the on_change callback. |
kwargs (dict or None) | An optional dict of kwargs to pass to the on_change callback. |
| Returns | |
(ExpanderContainer) | An ExpanderContainer object with an .open property to return the current state of the expander if the expander tracks state. |
Examples
Example 1: Use context management You can use the with notation to insert any element into an expander
Example 2: Call methods directly
You can call methods directly on the returned object:
Example 3: Programmatically control the expander state
You can use a key to programmatically control the expander state or access the state in callbacks. You must set the on_change parameter for the expander to track state.
ExpanderContainer
A container returned by st.expander.
ExpanderContainer is a Streamlit container with an .open property for lazy execution. Use with notation or call methods directly on the container to add elements to the expander.
| Attributes | |
open (bool or None) | Whether the expander is open. This is True if the expander is open and False if it's collapsed, or None if state tracking isn't enabled. |
Examples
Example 1: Lazy loading content
Example 2: Conditionally render content outside of the expander
Still have questions?
Our forums are full of helpful information and Streamlit experts.
