5 Tips for Building Useful Streamlit Dashboards in Minutes

0
7


5 Tips for Building Useful Streamlit Dashboards in Minutes
Image by Editor | ChatGPT

 

Introduction

 
Streamlit is a Python framework for creating user-friendly web applications with minimal code. It is mainly aimed at data professionals and developers and is frequently used for data exploration, building dashboards, and prototyping ML applications. The framework provides simple, high-level APIs that are easy to use and includes many built-in features for developing useful dashboards. However, many still only know the basics and do not fully utilize Streamlit.

That’s why this article will explore five different tips to help you build useful Streamlit dashboards in minutes.

 

1. Use Caching

 
Streamlit is a framework that natively runs our scripts from the beginning whenever we have a change in the input. This means the computation will be expensive if we unnecessarily repeat operations such as data or model loading.

By using caching, we will decrease the operational time and computational memory significantly, as we can reuse the same object. For example, the code implementation is as below:

@st.cache_data
def load_data():
    return pd.read_csv("large_dataset.csv")

@st.cache_resource
def load_model():
    from sklearn.ensemble import RandomForestClassifier
    return RandomForestClassifier()

 

The st.cache_data is best for data operations such as CSV loads, while st.cache_resource is best for persistent resources such as ML models or DB connections.

By using caching effectively, we can make our dashboard process faster, even with large datasets or models.

 

2. Batch Inputs

 
As mentioned previously, Streamlit processes the script from the beginning whenever we have a change in our input. There are many cases where we will have many input workflows that will become cumbersome when there are changes to every single input.

By using st.form, we can group widgets and the operations so they update only when the user clicks a submit button. Example code implementation is shown below:

with st.form("filters"):
    min_value = st.slider("Minimum Value", 0, 100, 10)
    max_value = st.slider("Maximum Value", 0, 100, 90)
    submitted = st.form_submit_button("Apply")

if submitted:
    st.write(f"Filtering data between {min_value} and {max_value}")

 

The code above essentially avoids unnecessary recomputation and gives users control over when the dashboard refreshes, especially with the capability for input batching.

 

3. Persist State

 
There are times as well that we want to keep the state when there are input changes, such as the counter, filter variables, or authentication flags. It might be too much to use the caching, which is why we can use the persist state method with st.session_state.

We can persist variables across reruns and even update them interactively with the st.session_state, which makes the whole workflow smoother and more efficient memory-wise. The example code implementation is shown below:

if "counter" not in st.session_state:
    st.session_state.counter = 0

if st.button("Increment"):
    st.session_state.counter += 1

st.write(f"Counter: {st.session_state.counter}")

 

The code above initiates a variable inside the st.session_state so it persists across reruns. It basically helps us have a much better workflow.

 

4. Highlight Key Metrics

 
Whenever we are using a dashboard, the important thing is to show what matters the most. For many business users and non-technical users they want to see the important numbers upfront, and supporting details only when they choose to explore further.

By using the st.metric, we could display key performance indicators (KPIs) as clean cards. The function makes the dashboard useful for showing KPIs such as user growth, revenue, error rates, and many more. The code implementation is shown below:

st.metric("Active Users", 1234, "+134")

 

The code above will show a KPI card for “Active Users” with a positive delta of +134. In this way, we can make our dashboard useful with a single line of code.

 

5. Leverage Community Components

 
Streamlit’s core library covers most use cases, but sometimes you need more advanced interactivity than what’s available. That’s where community components come in. These are third-party extensions built by the Streamlit community that can be installed in your Python environment

There are many useful features, such as editable tables (streamlit-aggrid), interactive maps (streamlit-folium), chat UIs (streamlit-chat), and many others. This means you can upgrade your dashboard’s functionality easily.

Visit and explore the community components that are useful for your work.

 

Wrapping Up

 
Streamlit is a Python framework that is useful for building web applications, especially for developers and data professionals. It’s easy to use, but there are still many ways to improve our experience using them to build better dashboards.

In this article, we have explored five different ways to build a useful Streamlit dashboard in minutes, from using caching, to persisting state, to leveraging community components.

I hope this has helped!
 
 

Cornellius Yudha Wijaya is a data science assistant manager and data writer. While working full-time at Allianz Indonesia, he loves to share Python and data tips via social media and writing media. Cornellius writes on a variety of AI and machine learning topics.