Component example: Simple button
This is an interactive button that sends events to Python. It demonstrates basic frontend-to-backend communication.
Key concepts demonstrated
This component demonstrates the following concepts:
- Component registration with HTML, CSS, and JavaScript
- One-time trigger values sent from JavaScript with
setTriggerValue() - Callback functions using the
on_<trigger>_changenaming pattern - Accessing trigger values from the component's return object
Complete code
How it works
Trigger values
When the button is clicked, setTriggerValue("action", "button_clicked") sends a one-time event to Python. The makes the component's "action" attribute return the string "button_clicked" in Python.
Callback registration
The on_action_change parameter registers a callback that runs when the trigger fires. The callback name follows the pattern on_<trigger_name>_change. Always register a callback for trigger values, even an empty one like lambda: None. This ensures the result object always has an attribute for the trigger. In this example, if a callback isn't defined, "action" won't exist as a result attribute until the trigger fires. This can cause an AttributeError if you try to access it before the trigger fires.
In this example, the callback is used to increment the value of st.session_state.click_count. This is a simple way to track the number of times the button has been clicked. Alternatively, you could use a state value in your component to track the number of clicks. For an example of a component that uses a state value to track the number of clicks, see the interactive counter example.
Accessing the trigger
The trigger value is accessible via result.action. Trigger values are transient, just like st.button() values. However, you can configure triggers to return different data types and values for different events. In this example, the "action" attribute returns the string "button_clicked" when the button is clicked and None otherwise. You can create complex components that return different values to indicate different user actions.
Still have questions?
Our forums are full of helpful information and Streamlit experts.