Skip to main content

General utilities to make streamlit app creation easier in an object-oriented manner.

Project description

streamlit-utils

GitHub Python PyPI License: MIT CI build pre-commit Checked with mypy Code style: black

General utilities to make streamlit app creation easier in an object-oriented manner. This package started with the idea for the session_state decorator to simplify the interaction with the st.session_state object. It has since grown to include other utilities as well.


General utilities

This section describes the general utilities provided by this package in a non-exhaustive manner. For more information, please refer to the docstrings of the individual functions and classes.

The session_state decorator

Traditionally, storing state in a Streamlit app is done by saving a value to a key in the st.session_state object. This object is a SessionState instance that is created only on the first run of the app and will be persisted between reruns of the app. As the apps are procedural, this means that we have to check for the existence of the key before we can use it.

A typical usage of the st.session_state object looks like this:

import streamlit as st

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}")

However, this approach creates two major inconveniences:

  • The code gets cluttered with if statements to check for key existences.
  • We lose the ability to use type hints with our stateful variables.

And we can also observe that the code gets less 'pythonic', with lots of if statements in a 'look-before-you-leap' fashion. This is where the session_state decorator comes in. The code above can be rewritten as:

import streamlit as st

from streamlit_utils.state import session_state

@session_state
class MyState:
    counter: int = 0

if st.button("increment"):
    MyState.counter += 1

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

What the above code does is to create a MyState instance that is stored in the st.session_state object. The MyState instance is created only on the first run of the app and will be retrieved from the st.session_state object on subsequent runs. This means that we can use the MyState instance as if it was a normal singleton, without having to worry about the key existence.

The example uses only one state variable, but the MyState class can have as many state variables as needed. The state variables can also be of any type that is supported by the st.session_state object.

Another possible usage, when using a session_state decorated singleton to store state for Streamlit buttons and fields is:

@session_state
class MyState:
    counter: int = 0
    text: str = ""

st.text_input("Enter a text:", key="MyState.text")
st.write(f"Text entered: {MyState.text}")

Note that the key argument to store and persist the text_input value is specified with the name of the singleton class and the key on a string. This feature is provided by the session_state decorator and allow seamless usage with any Streamlit widget that accepts a key argument.

Streamlit widgets that accept a key argument does not accept that this key gets modified by other agents (e.g. other widgets or directly set). For this reason, changing the MyState.text previous to the text_input widget line will do nothing to the value of the text_input widget.

Contributing

Install the package to your local Python virtual environment with one of the following commands:

make install

# OR directly with pip
pip install -e .[dev]

Before contributing, please always check your code health by running:

  • Code formatter, linter and type-checker:
# The install command can be suppressed if the package was installed using the
# `make install` convenience command.
pre-commit install
pre-commit run --all-files
  • Tests:
# Can be run with `make test` as well.
pytest --cov

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit-utils-0.1.1.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

streamlit_utils-0.1.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file streamlit-utils-0.1.1.tar.gz.

File metadata

  • Download URL: streamlit-utils-0.1.1.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for streamlit-utils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8ecb612c38aecf4994456be67d2a793de6bd9f18ce513a9739aa217ec39a7218
MD5 6df94516ad8075779e6f074d16c5bb78
BLAKE2b-256 5880648008f15f7ddfa4d2ca5d90c61c8827d06e19537e765ab6f67f5e3986c7

See more details on using hashes here.

File details

Details for the file streamlit_utils-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_utils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b21a3c5d3d58ce522120a0fe407ed1ce7047e384c8ac5e32f5f94eb370555779
MD5 14e5f277dcff49ff9c3c6207704b9a21
BLAKE2b-256 41514b90e0daee09f1f22d8d2bb90686666fd64ae55da5b609c062177ae005f8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page