Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Layout and Navigation

Tabs, Expanders, Popovers and Dialogs

Organize a Streamlit page with tabs, expanders and popovers, and ask for confirmation with st.dialog. Learn why every tab's code runs on each rerun and how to avoid slow tabs.

A page that shows everything at once gets long and noisy. Streamlit gives you four ways to tuck content away until it is needed: tabs (switch between views), expanders (open and close a section), popovers (a small floating panel) and dialogs (a modal window that asks for attention). Each hides content but they behave differently on a rerun, and that difference matters for speed.

The Streamlit code is real and read-only; the page is described in words. Runnable stand-ins count function calls to prove how the reruns behave.

st.tabs

Python
import streamlit as st

overview, details, settings = st.tabs(["Overview", "Details", "Settings"])

with overview:
    st.write("Summary numbers go here.")
with details:
    st.dataframe({"a": [1, 2], "b": [3, 4]})
settings.checkbox("Show raw data")

The page shows a tab bar with three labels. "Overview" is selected, showing its sentence; click "Details" to see the table; click "Settings" to see the checkbox. Switching tabs happens in the browser and does not rerun your script, which makes it instantaneous. Add icons or emoji in the labels ("๐Ÿ“ˆ Charts"), and st.tabs has an optional default= argument to open another tab first in newer versions.

Important: every tab's code runs

There is a catch many beginners hit. The code for all tabs runs on every rerun, even for tabs the user is not looking at. Streamlit hides the other tabs in the browser but your script computed them. If the "Details" tab loads a huge file or trains a model, that work happens every time any widget changes, whether or not the tab is visible.

The standard cure is to cache the slow step (see the State and Caching chapter), or to use a widget (a segmented_control or radio) instead of tabs so that only the chosen branch runs. The block below is not Streamlit; it counts how often a slow function is called in each design.

Python Stand-in: all tabs run on every rerun

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With tabs, details ran five times though nobody looked at it; with the choice, it ran zero times. For small content, tabs are best (instant switching). For heavy content, a choice widget or a cache is better.

st.expander

An expander is a labelled section that opens and closes:

Python
with st.expander("How is this calculated?"):
    st.write("Revenue minus returns, converted to USD at the daily rate.")
    st.latex(r"\text{net} = \text{revenue} - \text{returns}")

with st.expander("Advanced options", expanded=True, icon=":material/tune:"):
    threshold = st.slider("Threshold", 0, 100, 50)

The first section shows only a clickable line "How is this calculated?"; clicking it reveals the explanation. The second starts open because of expanded=True. Expanders are ideal for help text, raw data and optional settings. Like tabs, the code inside runs every time even when the section is closed (widgets inside still work and their values are available). An expander cannot be nested inside another expander in most versions; use columns or containers for deeper structure.

st.popover

A popover is a button that opens a small floating panel, perfect for filter menus and settings that should not take permanent space:

Python
with st.popover("Filters"):
    region = st.selectbox("Region", ["All", "North", "South"])
    only_open = st.checkbox("Only open orders")

st.write("Region:", region, "| open only:", only_open)

The page shows a "Filters" button. Click it and a small panel pops up with a selectbox and a checkbox. Changing a widget inside reruns the script as usual and the panel stays open, and the text line below updates. Clicking outside closes the panel; the widget values remain set. Pass use_container_width=True (or the newer width option) to widen the button.

st.dialog: a modal window

A dialog darkens the page and shows a window the user must deal with. In Streamlit you define the dialog's content as a function with the @st.dialog decorator and open it by calling that function, normally from a button:

Python
import streamlit as st

@st.dialog("Delete all rows?")
def confirm_delete():
    st.write("This cannot be undone.")
    reason = st.text_input("Reason (optional)")
    col1, col2 = st.columns(2)
    if col1.button("Cancel"):
        st.rerun()                       # closes the dialog
    if col2.button("Delete", type="primary"):
        st.session_state.deleted = reason or "no reason"
        st.rerun()

if st.button("Delete everything"):
    confirm_delete()

if "deleted" in st.session_state:
    st.success(f"Deleted ({st.session_state.deleted}).")

The page shows a "Delete everything" button. Click it: the page dims and a window titled "Delete all rows?" appears with the warning, a text box and two buttons. "Cancel" closes it. "Delete" saves the reason in session state, closes the dialog and the green message appears behind it. Details:

  • Only one dialog can be open at a time, and a dialog cannot open another dialog.
  • Widgets inside a dialog rerun only the dialog function (it behaves like a fragment), not the whole script. That keeps it fast, but means the rest of the page does not see the values until you call st.rerun() or write to session state.
  • The dialog closes when the user clicks the X, presses Escape or clicks outside. Calling st.rerun() from inside closes it from your code.
  • width="small" (default), "medium" or "large" sets the size.

The confirmation flow (stand-in)

Modal confirmations are "state machines": closed, open, then back to closed with a result. The stand-in below is not Streamlit; it models the flow in a few lines of plain Python.

Python Stand-in: a confirm dialog as a state machine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing the right container

Need Use Reruns on open? Notes
Switch between parallel views st.tabs No All tabs' code still runs
Hide optional or help content st.expander No Code runs even when closed
Compact menu of settings st.popover No Widgets inside rerun the script
Demand a decision st.dialog Dialog only One at a time

Hide it, but carefully

Tabs Views side by side
Expander Optional detail
Popover Small floating menu
Dialog A modal decision
1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A user opens the 'Overview' tab. Does the code of the 'Details' tab run?

    The browser hides inactive tabs but your script still executes all of them, so cache or avoid slow work.
  2. 2. Which call opens an @st.dialog function?

    A dialog opens when you call the decorated function.
  3. 3. How do you close a dialog from your code?

    Calling st.rerun() inside the dialog function closes it and reruns the whole script.
  4. 4. Which element is a compact floating panel opened by a button?

    st.popover shows a button that opens a floating panel with your widgets.

Summary

  • st.tabs switches views in the browser without a rerun, but every tab's code runs each time.
  • st.expander hides optional content; st.popover is a floating menu of widgets.
  • @st.dialog creates a modal window opened by calling the function; st.rerun() closes it.
  • Cache slow work or use a choice widget when hidden content is expensive.

Next: The Sidebar