Streamlit has revolutionized the way we create web applications in Python. At the heart of this revolution are Streamlit widgets - interactive elements that allow users to interact with your app seamlessly. In this blog post, we'll explore various Streamlit widgets and how to use them effectively.
Let's start with some fundamental widgets that form the building blocks of any Streamlit app.
The st.button()
widget creates a clickable button:
import streamlit as st if st.button('Click me!'): st.write('You clicked the button!')
This simple code creates a button that, when clicked, displays a message.
The st.checkbox()
widget creates a toggle-able checkbox:
show_text = st.checkbox('Show text') if show_text: st.write('This text is visible because you checked the box!')
For mutually exclusive options, use st.radio()
:
option = st.radio('Choose your favorite color:', ('Red', 'Green', 'Blue')) st.write(f'Your favorite color is {option}')
Streamlit offers various widgets for user input. Let's look at a few:
Use st.text_input()
for single-line text input:
name = st.text_input('Enter your name:') st.write(f'Hello, {name}!')
For numerical input, use st.number_input()
:
age = st.number_input('Enter your age:', min_value=0, max_value=120, step=1) st.write(f'You are {age} years old')
Sliders are great for selecting values from a range:
volume = st.slider('Set the volume:', 0, 100, 50) st.write(f'Volume is set to {volume}%')
For choosing from a list of options, Streamlit provides several widgets:
Use st.selectbox()
for dropdown selection:
fruit = st.selectbox('Choose a fruit:', ['Apple', 'Banana', 'Cherry']) st.write(f'You selected: {fruit}')
For multiple selections, use st.multiselect()
:
options = st.multiselect('Choose your favorite colors:', ['Red', 'Green', 'Blue', 'Yellow']) st.write('You selected:', ', '.join(options))
The st.file_uploader()
widget allows users to upload files:
uploaded_file = st.file_uploader('Choose a CSV file', type='csv') if uploaded_file is not None: data = pd.read_csv(uploaded_file) st.write(data)
Streamlit provides widgets for date and time input:
Use st.date_input()
for date selection:
date = st.date_input('Select a date:') st.write(f'Selected date: {date}')
For time selection, use st.time_input()
:
time = st.time_input('Set an alarm for:') st.write(f'Alarm set for: {time}')
You can customize the appearance of widgets using the key
parameter:
st.button('Click me!', key='styled_button', help='This is a custom button')
Create dynamic interfaces by conditionally displaying widgets:
if st.checkbox('Show advanced options'): st.slider('Advanced setting:', 0, 100, 50)
Organize your layout using columns:
col1, col2 = st.columns(2) with col1: st.button('Button 1') with col2: st.button('Button 2')
Streamlit maintains widget states across reruns. Use the key
parameter to uniquely identify widgets:
count = 0 if st.button('Increment', key='increment_button'): count += 1 st.write(f'Count: {count}')
Streamlit widgets are powerful tools for creating interactive web applications. By combining these widgets creatively, you can build complex, user-friendly interfaces with just a few lines of Python code. Experiment with different widgets, customize their appearance, and create dynamic layouts to take your Streamlit apps to the next level.
08/12/2024 | Python
14/11/2024 | Python
26/10/2024 | Python
22/11/2024 | Python
08/11/2024 | Python
15/11/2024 | Python
17/11/2024 | Python
14/11/2024 | Python
25/09/2024 | Python
21/09/2024 | Python
25/09/2024 | Python