Skip to content
Topics
Streamlit
Beyond the Basics: Complete Guide for Streamlit Buttons

Beyond the Basics: Complete Guide for Streamlit Buttons

Streamlit has revolutionized the way we build data applications. With its low-code approach, it has made it possible for data scientists and developers to create interactive, user-friendly applications with ease. One of the key elements in Streamlit that contributes to this interactivity is the humble button. In this guide, we'll dive deep into the world of Streamlit's st.button, exploring its usage, styling options, and event-driven capabilities. We'll also provide practical examples for beginners and seasoned developers alike.

Have you heard of this awesome Data Analysis & Data Visualisation tool, that turns your Streamlit App into Tableau?

PyGWalker (opens in a new tab) is a Python Library that helps you easily embed a Tableau-like UI into your own Streamlit app effortlessly. Check out this amazing video produced by Sven from Coding is Fun (opens in a new tab) demonstrating the detailed steps for empowering your Streamlit app with this powerful Data Visualization Python Library!


Special Thanks to Sven and his great contribution (opens in a new tab) to PyGWalker community!

Additionally, you can also check out PyGWalker GitHub Page (opens in a new tab) for more PyGWalker examples.

Understanding st.button in Streamlit

What is st.button in Streamlit?

In Streamlit, st.button is a function that creates a button widget. It's a simple yet powerful tool that allows you to add interactivity to your Streamlit applications. When a user clicks a button, it triggers an event, which can be used to perform a specific action or update the application's state.

if st.button('Say Hello'):
   st.write('Hello, Streamlit!')

In the example above, when the 'Say Hello' button is clicked, the text 'Hello, Streamlit!' is displayed on the screen.

How to Use st.button in Streamlit

Using st.button in Streamlit is straightforward. The function takes one required argument - the label of the button, which is a string. It also accepts several optional arguments that allow you to customize the button's behavior and appearance. Let's take a look at some of these arguments:

  • key: A unique identifier for the button. This is useful when you have multiple buttons and you want to distinguish between them.
  • help: A string that will be displayed as a tooltip when the user hovers over the button.
if st.button('Click Me', key='my_button', help='Click this button to perform an action'):
   st.write('You clicked the button!')

Exploring Arguments and Styling Options for Streamlit Button

Streamlit's st.button function provides a number of arguments that allow you to customize the appearance and behavior of your buttons. However, as of the time of writing, Streamlit does not natively support changing the color and size of a button directly through the st.button function.

Styling Streamlit Buttons with CSS

While Streamlit doesn't provide built-in options for button styling, you can use CSS to customize your buttons. You can inject custom CSS into your Streamlit app using the st.markdown function with the unsafe_allow_html=True argument. Here's an example:

st.markdown("""
<style>
.custom-button {
   background-color: #4CAF50;
   color: white;
   padding: 14px 20px;
   margin: 8px 0;
   border: none;
   cursor: pointer;
   width: 100%;
}
.custom-button:hover {
   opacity: 0.8;
}
</style>
<button class="custom-button">Custom Button</button>
""", unsafe_allow_html=True)

In this example, we've created a custom button with a green background, white text, and some padding. We've also added a hover effect that reduces the button's opacity when the mouse is over it.

The on_clickButton Event in Streamlit

In Streamlit, the on_click event is triggered when a button is clicked. This event can be used to perform a specific action or update the application's state. For example, you could use the on_click event to refresh data, show or hide content, or perform a calculation.

Here's an example of how you might use the on_click event in Streamlit:

def say_hello():
   st.write('Hello, Streamlit!')
 
button = st.button('Say Hello')
button.on_click(say_hello)

In this example, when the 'Say Hello' button is clicked, the say_hello function is called, which writes 'Hello, Streamlit!' to the screen.

Practical Examples of Streamlit Button

Streamlit's st.button is not just a theoretical concept, it's a practical tool that you can use to add interactivity to your applications. Let's look at some examples that illustrate how to use it effectively.

Streamlit Button Examples for Beginners

If you're new to Streamlit, here's a simple example to get you started. This code creates a button that, when clicked, displays a message:

if st.button('Click me'):
    st.write('You clicked the button!')

In this example, st.button('Click me') creates a button with the label 'Click me'. When this button is clicked, st.write('You clicked the button!') is executed, and the message 'You clicked the button!' is displayed on the screen.

Advanced Streamlit Button Usage

For more advanced users, you can use buttons to control the flow of your application. For example, you could create a button that loads data when clicked:

if st.button('Load data'):
    data = load_data()
    st.write(data)

In this example, clicking the 'Load data' button calls the load_data function and displays the returned data on the screen.

Conclusion

Streamlit's st.button is a powerful tool for creating interactive, user-friendly applications. Whether you're a beginner just starting out with Streamlit or a seasoned developer looking to add more interactivity to your applications, understanding how to use and style buttons is essential. With the ability to customize button behavior and appearance, as well as handle click events, you can create a wide range of interactive features in your Streamlit applications. So go ahead, start experimenting with st.button, and see what you can create!

Have you heard of this awesome Data Analysis & Data Visualisation tool, that turns your Streamlit App into Tableau?

PyGWalker (opens in a new tab) is a Python Library that helps you easily embed a Tableau-like UI into your own Streamlit app effortlessly.

PyGWalker for Data visualization in Streamlit (opens in a new tab)

Frequently Asked Questions

How do you make a button in Streamlit?

In Streamlit, you can create a button using the st.button function. The function takes one required argument, which is the label of the button.

How do you position a button in Streamlit?

As of the time of writing, Streamlit does not provide built-in support for positioning elements, including buttons. The layout of a Streamlit app is mostly determined by the order in which elements are added in the script. However, you can use markdown and HTML in combination with CSS to create custom layouts.

How do you make a radio button on Streamlit?

In Streamlit, you can create a group of radio buttons using the st.radio function. The function takes two arguments: a label for the group of radio buttons, and a list of options.

What are the disadvantages of Streamlit?

While Streamlit is a powerful tool for creating data applications, it does have some limitations. For example, as of the time of writing, it lacks built-in support for creating complex layouts and styling elements. Additionally, because Streamlit apps are designed to be stateless, managing state (such as user inputs or variable values) can be challenging. However, Streamlit is actively developed and maintained, and new features and improvements are regularly added.