Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 38 ยท Layout and Navigation

The Sidebar

Put filters and settings in the Streamlit sidebar with st.sidebar. Learn the with syntax, st.logo, starting collapsed, forms for filters and how to drive a dashboard from sidebar widgets.

Most dashboards have the same shape: a narrow panel on the left with controls, and the main area with the results. Streamlit builds that panel for you. Everything you place in st.sidebar appears in a collapsible column on the left, which keeps the controls available without cluttering the page. On a phone the sidebar becomes a slide-in drawer.

The Streamlit snippets are real and read-only, with a description of the page. Runnable blocks build the options and apply the filters, which is the logic that turns sidebar widgets into a view.

Putting widgets in the sidebar

There are two equivalent styles:

Python
import streamlit as st

# Style 1: attribute notation
region = st.sidebar.selectbox("Region", ["All", "North", "South"])

# Style 2: a with block (nice for many widgets)
with st.sidebar:
    st.header("Filters")
    min_price = st.slider("Minimum price", 0, 100, 10)
    only_stock = st.checkbox("In stock only")

st.title("Product explorer")
st.write(f"Showing {region} products over ${min_price}")

The page shows a title in the main area and, on the left, a panel with a "Region" drop-down, then a "Filters" header with a slider and a checkbox. The text in the main area follows the controls: "Showing All products over $10". The sidebar can hold any element that works in the main area: widgets, text, charts, metrics, and even columns. Keep it to controls and short notes. Charts and tables belong in the main area, where there is space for them.

Order inside the script does not decide the position on screen: sidebar elements always appear in the sidebar, whenever in the script you write them. This means you can place the sidebar code at the top of the script (as is conventional, since the main area depends on those values).

Page-level settings for the sidebar

Setting Where Effect
initial_sidebar_state="collapsed" st.set_page_config Start with the sidebar closed
initial_sidebar_state="expanded" st.set_page_config Start open (default on wide screens)
st.logo("logo.png", icon_image="icon.png") anywhere Show a logo above the sidebar and in the collapsed header
st.sidebar.title("...") script A title inside the sidebar

st.logo takes an image path, URL or Streamlit-supported image; icon_image is a small version shown when the sidebar is collapsed, and link= makes the logo clickable. Together with st.set_page_config (from Magic and Page Configuration), these give an app a branded frame in two lines.

Use the sidebar as a control panel

The standard pattern has three parts: build the options from the data, create the widgets, filter. The first part is worth a closer look, because options should come from the data and not be typed by hand.

Python Build sidebar options from the data

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.

In the app those values become st.sidebar.multiselect("Region", regions, default=regions), st.sidebar.selectbox("Category", categories), st.sidebar.slider("Price", price_min, price_max, (price_min, price_max)) and st.sidebar.date_input("Dates", (date_min, date_max)). Defaults that select everything mean the page starts by showing all the data.

One function applies all the filters

Keep filtering in one function. It takes the DataFrame and the widget values and returns the view, so it can be tested and reused.

Python Apply every sidebar filter in one function

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.

The function mirrors the structure of a real app: sidebar widgets gather the arguments, view = apply_filters(...), then the main area shows view.

Every change in a sidebar widget reruns the script. On a heavy page, group the filters in a form so the dashboard updates only when the user presses "Apply":

Python
with st.sidebar.form("filters"):
    regions = st.multiselect("Region", ["North", "South", "East", "West"],
                             default=["North"])
    price = st.slider("Price", 0, 100, (10, 60))
    st.form_submit_button("Apply")

The sidebar shows the controls and an "Apply" button; the main area changes only after a click. See Forms and Callbacks for the rules.

  • Keep it short. More than about eight controls is a sign you need tabs or a second page.
  • Group with headers and dividers. st.sidebar.divider() and st.sidebar.subheader() help.
  • Show state, not only controls. A small st.sidebar.caption("Data updated 5 min ago") tells users how fresh the data is.
  • Use key= consistently. Sidebar widgets that you later read from st.session_state need keys.
  • Mobile. The sidebar collapses behind a button on narrow screens; do not hide the only way to operate the app there without testing on a small screen.
  • Navigation vs. filters. In a multipage app (next lesson) the page menu appears at the top of the sidebar; your own widgets appear below it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which two forms are equivalent for adding a widget to the sidebar?

    Attribute notation and a with block both place elements in the sidebar.
  2. 2. Which setting makes the sidebar start closed?

    initial_sidebar_state is a set_page_config option.
  3. 3. Where should filtering logic live?

    A plain function is easy to test and reuse; the widgets only gather its arguments.
  4. 4. How do you update the dashboard only when the user presses Apply?

    Widgets inside a form do not rerun the app until the form is submitted.

Summary

  • st.sidebar (attribute or with) puts controls in a collapsible left panel; keep results in the main area.
  • Build widget options and defaults from the data, and apply all filters in one function.
  • Use initial_sidebar_state and st.logo for the frame, and a form for "apply" behavior.
  • On small screens the sidebar becomes a drawer, so test the narrow layout.

Next: Multipage Apps