Estimate project
← All posts

Streamlit for simple demo apps: advantages and drawbacks

Ever stumbled upon a machine learning model demo with a polished graphical interface? Chances are it was built with Streamlit. Keep reading to find out more about this framework.

9 min read
Machine LearningStreamlitPythonData VisualizationPrototyping

What is Streamlit?

Ever stumbled upon a machine learning model demo with a polished graphical interface? Quite likely, it was built with the Streamlit framework. Streamlit is an open-source Python framework for quick prototyping and for building visualizations and dashboards, and it does not require any knowledge of front-end technologies like JS or HTML. A handful of lines of code is enough to build dynamic, good-looking apps in which users can enter data and see the results in real-time. When a Streamlit app runs, the Python code executes and produces the HTML, CSS, and JavaScript needed to render the app in a web browser.

What is it used for?

Its main purpose is building simple web applications that showcase how machine learning models or algorithms work, or that present data. It ships with a good number of ready-made widgets for entering parameters and for displaying different kinds of data - from text, through images, to audio. These elements are implemented in JS but expose a simple Python interface. Because it is so easy to use, it lends itself to fast prototyping and building demo applications.

Most of the topics covered in this article have been put into practice in a demo application.

Pros

Here is what we appreciate about the framework.

Session state

Since the framework is reactive by design, the application is rerun whenever its state changes and the scene needs refreshing. Session state exists to keep the application’s state across these reruns. An application built this way revolves around reading values from and writing values to the session state, which makes it a central part of every app written in this framework. Session state is thread-safe: each session gets its own, so several users will not disturb one another’s application state. It is a package-level global variable with the same interface as an ordinary Python dictionary.

Reactive widget states

Reading and updating the state of widgets such as a checkbox is simple. You only have to define a key in the session state dictionary under which the widget’s state will be kept – nothing more. From there, we can move on to writing the application logic.

view.py

import streamlit as st

from demo_streamlit.callbacks import sample_index_callback
from demo_streamlit.load_data import load_dataset
from demo_streamlit.settings import Settings

def sidebar_view() -> None:
    """Draw side bar."""
    samples = load_dataset()
    st.sidebar.selectbox(
        "Select sample index:",
        options=list(range(0, len(samples))),
        key=Settings.selected_sample_index,
        on_change=sample_index_callback,
    )

The widget’s value is retrieved using its key:

app_state.py

from dataclasses import dataclass

import streamlit as st

from demo_streamlit.settings import Settings

@dataclass
class AppState:
    """An app state model."""

    selected_sample_index: int
    """Selected sample index."""

    message: str
    """UI message."""

def get_app_state() -> AppState:
    """Get AppState dataclass from session state."""
    return AppState(
        selected_sample_index=int(
            st.session_state.get(Settings.selected_sample_index, 0)
        ),
        message=st.session_state.get(Settings.message, ""),
    )

Thread safe singletons and cache

The cache is a commonly used feature that lets you optimize time-consuming operations when the application view is refreshed. The caching mechanism built into the framework is very easy to use. It relies on the @st.cache decorator, where you can set, for example, the time to live parameter and hash functions for custom objects. The cache takes into account the arguments, the body of the function along with every function it calls, and any external variables the function uses. Because the framework is reactive and redraws the UI on every state change, caching is indispensable for keeping the application reasonably fast and responsive.

The code below defines a function that returns the objects stored in the database that match a query. Since it takes a while to run, it was optimized with the caching mechanism, and the TTL parameter was set to 10 minutes. For objects the framework cannot hash on its own, custom hash functions were supplied. For complex objects, they return None, which leaves those objects out of the hash computation.

database.py

@st.cache(
    hash_funcs={
        Collection: lambda _: None,
        database.Database: lambda _: None,
        MongoClient: lambda _: None,
    },
    ttl=600,  # TTL value in seconds
)
def list_items(
    self,
    query: dict[str, Any] | None = None,
) -> list[dict[str, Any]]:
        if query is None:
            query = {}

        items: list[dict[str, Any]] = [item for item in self._collection.find(query, sort=[("timestamp_created", -1)])]

        return items

Flexible widget callbacks

The framework is very flexible when it comes to defining callbacks for widget events. Only a few events exist, but they cover most needs: on_click and on_change. When constructing a widget, we can pass a function to on_click or on_change that will be invoked when the event fires. We can also hand over arbitrary parameters as a tuple via the args argument.

callback.py

import logging
from typing import Any

import streamlit as st

logger = logging.getLogger(__name__)

def button_callback(index: str, headers: dict[str, Any] | None) -> None:
    """Button on_click callback."""
    logger.info(  # pylint: disable = (logging-fstring-interpolation)
        f"Passed arguments: ({index}, {headers})."
    )

view.py

import streamlit as st
from streamlit.web.server.websocket_headers import _get_websocket_headers

from demo_streamlit.callbacks import button_callback
from demo_streamlit.settings import Settings

def sidebar_view() -> None:
    """Draw side bar."""
    headers: dict[str, str] | None = _get_websocket_headers()
    if headers:
        with st.sidebar.expander("HTTP header"):
            st.json(headers)

    st.sidebar.button(
        label="Log parameters to terminal",
        on_click=button_callback,
        args=(
            st.session_state.get(Settings.selected_sample_index, 0),
            headers,
        ),
    )

Once the button was clicked, the terminal showed the following output:

Query params for storing app state in URL

When several people use the application, they may want to share its configuration with one another. This is what the framework’s query_params functionality is for. The st.experimental_set_query_params function accepts a dictionary, which is converted into GET parameters in the URL. In the same way, the st.experimental_set_query_params function reads a dictionary of parameters from the URL. This makes it possible to send someone a URL containing the saved parameters, so that opening the application shows them exactly the same view. The catch is the supported data format, which must be dict[str, list[str]].

Python decorators for state updates

Decorators are worth considering for operations that keep repeating. Updating query_params, for instance, can be wrapped in a decorator and attached to every callback that reacts to widget state changes.

query_params.py

from functools import wraps

import streamlit as st

def update_query_params() -> None:
    """Updates the URL query params."""
    query_params: dict[str, list[str]] = dict()
    query_params["text_input"] = [st.session_state.get("text_input", "")]
    query_params["checkbox_state"] = [str(st.session_state.get("checkbox_state", False))]
    st.experimental_set_query_params(**query_params)

def refresh_query_params(f):
    """Updates the URL query params after every app state change."""

    @wraps(f)
    def wrapper(*args, **kwargs):
        retval = f(*args, **kwargs)
        update_query_params()
        return retval

    return wrapper

callback.py

from query_params import refresh_query_params

@refresh_query_params
def checbox_callback() -> None:
    ...

 @refresh_query_params
def text_input_callback() -> None:
    ...

Development server mode with monitoring changes in the code

This is just plain handy. There is no need to restart the application after each edit to its source code. By default, streamlit starts applications in developer mode, which watches the source code for modifications. Each change is picked up, and the app adjusts its behavior immediately.

Access HTTP request headers

Starting with version 1.14.0, you can read the raw HTTP header that reached the application. This comes in handy when you need to pull specific information out of the HTTP headers, for example the ID/e-mail of a user authenticated through Google Cloud:

import streamlit as st
from streamlit.web.server.websocket_headers import _get_websocket_headers

headers = _get_websocket_headers()
access_token = headers.get("X-Access-Token")
if access_token is not None:
  # authenticate the user or whatever
  ...

Deployment

Deploying the application is very easy, and the documentation covers it in detail. It explains how to deploy with Docker, kubernetes, or Streamlit Cloud containers. Every guide comes with example code snippets, so the user is walked through the whole process step by step.

To launch an application on Streamlit Cloud, all you need is a link to a GitHub repository. A few clicks later, we get a public URL at which our running application is available.

Cons

Below are the framework’s weaknesses that are worth keeping in mind while designing an application.

The scene is redrawn every time the state changes

As noted before, every change to the UI state makes the application run its code from the very beginning, with the entire application state kept in the session state. When widget states change often, the app may refresh annoyingly frequently, which adds considerable delay. This behavior pushes you to design the application so that as many slow functions as possible are cached, but for certain types of applications that does not suffice.

No nesting of containers

Views cannot be nested, which seriously limits how an application layout can be designed. This becomes a problem in more complex views or when you want to display two complex objects next to each other for comparison. Due to this restriction, we have to find workarounds that may be less convenient.

Session state only with simple key-value pairs

Session state works very well for simple key-value pairs, such as storing widget states. Real-world applications, however, more often rely on complex objects that hold the state of the whole application. That way, a single object represents the entire application, and the types of its class fields can be enforced. Sadly, the framework does not support this at the moment. To get this result, we have to write two functions that load/save a class from/to the session state.

app_state.py

import streamlit as st
from dataclasses import dataclass

@dataclass
class AppState:
    checkbox_state: bool
    text_input: str

def load_app_state() -> AppState:
    return AppState(
        checkbox_state=st.session_state.get("checkbox_state", False),
        text_input=st.session_state.get("text_input", ""),
    )

def save_app_state(state: AppState) -> None:
    st.session_state["checkbox_state"] = state.checkbox_state
    st.session_state["text_input"] = state.text_input

Duplication of elements in the code when reusing widgets/containers

Comparing two objects requires keeping a set of variables for both of them in the session state. Since the session state holds key-value pairs, we must either keep a list of elements under one key or define separate keys for each object, e.g., text_input_1 and text_input_2. This is not elegant and makes mistakes more likely.

Necessity to project elements from the session state

Objects stored in the session state have no declared type, so we cannot be certain what kind of object we will get back. To be on the safe side, we can apply the cast method from the typing package.

cast_example.py

import streamlit as st
from typing import cast, Any

value: list[str]
value = cast(list[str], st.session_state["key"])

Conclusion

Streamlit is an excellent framework for building simple demo applications. When the application state changes often, redrawing the scene from scratch after every change can lead to performance and latency problems. Another restriction is that containers such as a column cannot be nested, which narrows down the page layout options.

Features offered by the framework, including session state, cache, and widget callbacks, make it possible to build complex application flows quickly.

In more complex applications, You may run into limitations stemming from how the framework is designed. It is a good idea to weigh the requirements of the application You are building and decide whether Streamlit is the right fit. There are many comparable frameworks that differ in complexity and capabilities; you can find a comparison of them here.