Insights

10 Streamlit Best Practices

Streamlit is a powerful tool for building data apps, but there are some best practices to follow to get the most out of it. Here are 10 of them.

Streamlit is a powerful open-source library for creating interactive web applications with Python. It is quickly becoming the go-to tool for data scientists and machine learning engineers who want to quickly create interactive web applications with minimal effort.

However, Streamlit is still a relatively new tool and there are a few best practices that should be followed when using it. In this article, we will discuss 10 Streamlit best practices that will help you create better Streamlit applications. We will cover topics such as data loading, caching, and performance optimization.

1. Use the same layout for all pages

Using the same layout for all pages helps to create a consistent user experience. This makes it easier for users to navigate your app and find what they’re looking for quickly. It also ensures that all of your pages look professional and polished, which can help to build trust with your users.

Additionally, using the same layout for all pages will make it easier for you to maintain your Streamlit app over time. You won’t have to worry about making sure each page is formatted correctly or updating multiple layouts when changes need to be made.

2. Keep your code clean and organized

Streamlit is a powerful tool for quickly creating interactive web apps, but it can also be difficult to debug if your code isn’t well-structured.

To keep your Streamlit code clean and organized, use functions to break up large chunks of code into smaller, more manageable pieces. This will make it easier to read and understand what’s going on in the code. Additionally, you should comment your code so that other developers (or even yourself) can easily follow along with what each line does. Finally, use consistent naming conventions throughout your code to ensure everything is easy to find.

3. Make use of st.beta_expander() to hide long text

When you have a lot of text to display, it can be overwhelming for the user. By using st.beta_expander(), you can hide long text and only show it when the user clicks on the expander button. This makes your app more organized and easier to read. It also allows users to quickly scan through the page without having to scroll down too much. Additionally, this feature is great for displaying code snippets or other large blocks of text that don’t need to be seen all at once.

4. Avoid using st.write() in loops

When you use st.write() in a loop, it will create multiple elements on the page for each iteration of the loop. This can cause performance issues and make your app look cluttered. Instead, try to find ways to display all of the data at once using other Streamlit components such as tables or charts.

If you must use st.write() in a loop, consider adding a delay between iterations so that the page doesn’t become overwhelmed with too many elements being created at once.

5. Don’t forget about st.help()

Streamlit has a lot of features and functions that can be used to create powerful applications. However, it can be difficult to remember all the different options available.

That’s where st.help() comes in. This function provides an easy way to access Streamlit’s documentation for any given feature or function. It also allows you to quickly search through the documentation to find what you need. So if you ever get stuck while coding with Streamlit, don’t forget about st.help().

6. Create a custom theme

A custom theme allows you to create a consistent look and feel for your Streamlit app. This helps make it easier for users to navigate the app, as they will be familiar with the layout and design elements.

Creating a custom theme also makes it easier to brand your Streamlit app. You can use colors, fonts, and other design elements that match your company’s branding. This helps give your app a professional look and feel, which is important when presenting data or insights to clients.

Finally, creating a custom theme gives you more control over how your Streamlit app looks and feels. You can customize everything from the font size and color scheme to the background image and logo. This level of customization ensures that your Streamlit app stands out from the crowd.

7. Add a favicon

A favicon is a small icon that appears in the browser tab when someone visits your Streamlit app. It’s an easy way to make your app stand out and give it a professional look.

Adding a favicon is simple: just create a 16×16 pixel image, save it as a .ico file, and upload it to the root directory of your Streamlit project. Then add this line of code to your main.py file:

st.markdown(‘‘, unsafe_allow_html=True)

This will ensure that your favicon displays correctly on all browsers.

8. Use st.cache() to improve performance

Streamlit runs your code every time a user interacts with the app, which can be slow and inefficient. By using st.cache(), you can store the results of expensive computations so that they don’t need to be re-run each time. This will make your Streamlit apps run faster and more efficiently.

Using st.cache() is easy – just wrap any function or computation in it and Streamlit will take care of the rest. For example, if you have an expensive calculation that takes several seconds to complete, you can wrap it in st.cache() like this:

@st.cache
def expensive_calculation():
# Do something expensive here
return result

9. Display progress bars when loading data

Progress bars are a great way to keep users informed about the progress of their data loading. This is especially important when dealing with large datasets, as it can take some time for them to load. Without a progress bar, users may become frustrated and give up on your app before the data has finished loading.

Progress bars also provide an opportunity to add a bit of personality to your Streamlit app. You can customize the look and feel of the progress bar to match the overall design of your app. This helps create a more cohesive user experience and makes your app stand out from the competition.

10. Use st.image() to display images

Using st.image() allows you to easily display images in your Streamlit app, which can be used for a variety of purposes such as displaying product photos or screenshots of data visualizations. This is especially useful if you want to quickly show off the results of your work without having to manually upload and embed an image into your code. Additionally, using st.image() makes it easier to keep track of all the images you’ve used in your app since they are stored within the same file.

Previous

10 AWS EventBridge Best Practices

Back to Insights
Next

10 C# Web API Security Best Practices