3 8 Buttons Explained
Key Concepts
- st.button: A widget that creates a clickable button.
- Parameters: Understanding the parameters like
label
andkey
. - Interactive Use: How to use buttons to trigger actions in your Streamlit app.
st.button
st.button
is a Streamlit widget that creates a clickable button. When the button is clicked, it returns True
; otherwise, it returns False
. This widget is essential for creating interactive applications where user actions trigger specific events or computations.
Parameters
The st.button
function accepts several parameters to customize its behavior:
label
: A string that provides a label for the button.key
: A unique key that identifies the button, useful when multiple buttons are present.
Interactive Use
Using st.button
, you can create interactive applications where users can trigger actions by clicking a button. For example, you can create a button that performs a calculation, updates a plot, or displays a message.
Examples
Here are some examples to illustrate the use of st.button
:
import streamlit as st st.title("Button Example") # Basic button if st.button("Click me"): st.write("Button was clicked!") # Button with a unique key if st.button("Click me again", key="unique_key"): st.write("Another button was clicked!") # Interactive button to trigger a calculation number = st.number_input("Enter a number", min_value=0, max_value=100, step=1) if st.button("Calculate Square"): result = number ** 2 st.write(f"The square of {number} is {result}")
In the first example, a basic button is created. When clicked, it displays a message. The second example demonstrates using a unique key for a button. The third example shows how to create an interactive button that triggers a calculation based on user input.