LEARN

Using Selectbox in Streamlit

A guide to implementing selectbox dropdowns in Streamlit for user input, data filtering, and interactive applications.

Archie Sarre Wood
Archie Sarre Wood

Adding a Selectbox in Streamlit

Selectboxes in Streamlit provide dropdown menus for user selection, enabling users to choose from predefined options. The st.selectbox() function creates an interactive dropdown that can filter data, control application flow, or gather user input.

Basic Example

A simple selectbox that displays the selected option.

import streamlit as st

option = st.selectbox(
    'Choose your favorite color',
    ['Red', 'Blue', 'Green', 'Yellow']
)
st.write('Your selected color:', option)

Using Selectbox with DataFrames

A selectbox that filters DataFrame data based on selection.

import streamlit as st
import pandas as pd

# Create sample data
df = pd.DataFrame({
    'department': ['Sales', 'Marketing', 'Engineering', 'Sales', 'Marketing'],
    'revenue': [100, 200, 300, 150, 250]
})

# Create selectbox with unique departments
department = st.selectbox(
    'Select Department',
    options=df['department'].unique()
)

# Filter and display data
filtered_df = df[df['department'] == department]
st.dataframe(filtered_df)

Setting a Default Value

Using the index parameter to set a default value.

import streamlit as st

selected = st.selectbox(
    "Select a category",
    options=['electronics', 'clothing', 'food'],
    index=1  # Set default to 'clothing'
)
st.write(f'You selected: {selected}')

Filtering Data with the Evidence Dropdown

Evidence provides the Dropdown component as an alternative to Streamlit’s selectbox. It enables interactive filtering directly in dashboards and reports.

Basic Example

A simple dropdown that displays the selected option.

```sql categories
select distinct category_name, sales
from sales_data
```

<Dropdown 
    data={categories} 
    name=category_filter 
    value=category_name 
/>

Using a Dropdown to Filter a Data Table

Filtering a data table using a dropdown.

```sql categories
select distinct category_name, sales
from sales_data
```

<Dropdown 
    data={categories} 
    name=category_filter 
    value=category_name
/>

```sql filtered_sales
select *
from sales_data
where category_name = '${inputs.category_filter.value}'
```

<DataTable data={filtered_sales} />

Setting a Default Value

```sql categories
select 'Cursed Sporting Goods' as category_name union all
select 'Sinister Toys' as category_name union all
select 'Mysterious Apparel' as category_name union all
select 'Odd Equipment' as category_name
```

<Dropdown 
    data={categories} 
    name=category_filter 
    value=category_name
    defaultValue="Sinister Toys"
/>

This approach allows for data filtering within reports using SQL for interactivity.

Streamlit Selectbox vs. Evidence Dropdown

FeatureStreamlit SelectboxEvidence Dropdown
SQL Integration(Manual)✔️
Multiple Selection✖️✔️
Default Value Support✔️✔️
Custom Formatting✔️✔️
Query-based Options(Manual)✔️

While Streamlit’s selectbox is great for python-based data apps, Evidence’s Dropdown component is optimized for data reporting and SQL-driven interactivity.

For more input options, explore the Evidence component library.

Get Started with Evidence

Build beautiful data apps using Evidence

  • Ship reports in hours
  • Terse, expressive syntax
  • All in version control

Start Free Trial →