
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
Easy state management in Streamlit with Pydantic.
Statelit is an easy way to manage the state of your Streamlit applications using Pydantic. Statelit allows you to import and export dashboard states with just a few extra lines of code.
Reasons to use Statelit:
Via pip:
pip install statelit
✨ See the below example in action here! ✨
# Requires `pip install matplotlib numpy`
import matplotlib.pyplot as plt
import numpy as np
import streamlit as st
from pydantic import BaseModel
from statelit import StateManager
class AppState(BaseModel):
seed: int = 372193
size: int = 10_000
mu: float = 5.0
sigma: float = 5.0
bins: int = 100
log: bool = False
state_manager = StateManager(AppState)
with st.sidebar:
state = state_manager.form()
with st.expander("State"):
state_manager.text_area()
np.random.seed(state.seed)
arr = np.random.normal(state.mu, state.sigma, size=state.size)
if state.log:
arr = np.log(arr)
fig, ax = plt.subplots()
ax.hist(arr, bins=state.bins)
st.pyplot(fig)
See example.py for a more complicated example and a demo of all of Statelit's features.
StatelitModel Attributes and MethodsThe following attributes and methods are part of the public API and are considered stable.
StatelitModel().pydantic_objPydantic object being used by the StatelitModel().
StatelitModel().widget()Render a single widget for a single field.
Parameters:
field_name: str (required) - String name of the field to render a widget for.key_suffix: Optional[str] = None - Suffix to apply to the state key.validate_output: bool = True - If true, run the "from_streamlit callback" for this field before returning a value.**kwargs - Additional keyword arguments that will be passed to the Streamlit callback.Returns: (Any) The value output by the Streamlit callback (i.e. the value of the widget), after running the from_streamlit callback if validate_output is True.
StatelitModel().form()Renders all widgets for the entire Pydantic BaseModel. Widgets are rendered in the order they're defined in the model.
Parameters:
key_suffix: Optional[str] = None - Suffix to apply to the state key.exclude: Optional[List[str]] = None - Which field names to exclude from rendering.**kwargs - Additional keyword arguments that will be passed to the Streamlit callback.Returns: (pydantic.BaseModel) The Pydantic model object, pydantic_obj.
StatelitModel().code()Renders Markdown syntax highlighted version of the JSON state.
Returns: (str) JSON representation of the state.
StatelitModel().text_area()Renders the JSON state as a text field. The JSON can be modified, and changing its value will update all other fields to match.
Parameters:
key_suffix: Optional[str] = None - Suffix to apply to the state key.exclude: Optional[List[str]] = None - Which field names to exclude from rendering.validate_output: bool = True - If true, run the "from_streamlit callback" before returning a value. In this case, validating the output is equivalent to converting the JSON str into the Pydantic object.**kwargs - Additional keyword arguments that will be passed to the Streamlit callback.Returns: (pydantic.BaseModel | str) Either outputs the Pydantic object (equivalent to StatelitModel().pydantic_obj) if validate_output is True, or alternatively returns a str JSON representation of the state if validate_output is False.
StatelitModel().lazy_text_area()NOTE: This feature is buggy. Use at your own peril. Known bugs are that it does not work with Optional[T] and statelit.types.DateRange.
Renders the JSON state as a "lazy" text field. The JSON can be modified, but changes won't be saved until the "Apply" button is pressed.
Parameters:
key_suffix: Optional[str] = None - Suffix to apply to the state key.exclude: Optional[List[str]] = None - Which field names to exclude from rendering.validate_output: bool = True - If true, run the "from_streamlit callback" before returning a value. In this case, validating the output is equivalent to converting the JSON str into the Pydantic object.**kwargs - Additional keyword arguments that will be passed to the Streamlit callback.Returns: (pydantic.BaseModel | str) Either outputs the Pydantic object (equivalent to StatelitModel().pydantic_obj) if validate_output is True, or alternatively returns a str JSON representation of the state if validate_output is False.
The following implementations are considered stable:
| Type | Widget | Notes |
|---|---|---|
float | st.number_input | |
int | st.number_input | |
decimal.Decimal | st.number_input | |
str | st.text_input or st.text_area | st.text_area is used if the default value contains a \n; otherwise, st.text_input is used. |
enum.Enum | st.selectbox | st.radio is also a good choice; set the streamlit_widget kwarg in the Field() to use that. |
datetime.date | st.date_input | |
datetime.time | st.time_input | |
pydantic.ConstrainedInt | st.slider | Used when both ge/gt and le/lt are set; otherwise, use st.number_input |
pydantic.ConstrainedFloat | st.slider | Used when both ge/gt and le/lt are set; otherwise, use st.number_input |
pydantic.color.Color | st.color_picker | Colors are always converted to hex values. |
dict[any, bool] | st.multiselect | True if selected, False if not selected. |
Set[enum.Enum] | st.multiselect | All Enum members are valid selections; set consists of selected members. |
The following implementations are considered experimental and are potentially subject to some future changes:
| Type | Widget | Notes |
|---|---|---|
datetime.datetime | st.date_input | Time component is always cast to 00:00:00. For true datetimes, at the moment, it is suggested you use separate datetime.date and datetime.times and manually combine them. |
statelit.types.DateRange | st.date_input | Custom type. Currently does not work with lazy state. |
tuple[datetime.date, datetime.date] | st.date_input | Same as DateRange. |
tuple[int, int] | st.slider | In the future, defaults (when default is None) and some kwargs will be set more intelligently. |
tuple[float, float] | st.slider | In the future, defaults (when default is None) and some kwargs will be set more intelligently. |
tuple[decimal.Decimal, decimal.Decimal] | st.slider | In the future, defaults (when default is None) and some kwargs will be set more intelligently. |
list | st.text_area | Unclear if list will always be a text area in future versions. |
dict | st.text_area | Unclear if dict will always be a text area in future versions. |
set | st.text_area | Unclear if set will always be a text area in future versions. |
Most users do not need this.
Note that Streamlit is a new project, and some of these implementations may be considered unstable until the 0.1.0 release.
The StatefulObjectBase class is a Generic class that consists of shared internals for both model and field instances.
There are three types of state: base, replicated, and lazy:
base state is the source of truth for all state. Each StatefulObjectBase has only one base state.replicated state is always kept in sync with base state. If replicated state changes, then base state is updated.lazy state is updated when base state updates, but changes to lazy state will not update the base state by itself (for example, updating a lazy-state text field does not update base state, but a button may trigger a callable that updates the base state from the lazy state).State is represented by keys associated with each object. Note that keys are by default assigned automatically. If you want to support more dynamic rendering (that makes when widgets are rendered non-deterministic), please set a key_suffix=.
StatefulObjectBases also have methods that allow for conversion between Statelit and Pydantic called to_streamlit and from_streamlit.
Converting a Pydantic model field to a Statelit field mostly consists of checking pydantic.fields.ModelField.type_. However, there are additional complications that allow for greater control.
A callback is a callable that comes in one of three types: it is either a widget, to_streamlit, or from_streamlit
A converter is a callable that takes in (value: Any, field: pydantic.fields.ModelField, model: pydantic.BaseModel), and returns a callback.
A FieldFactory is a callable that takes in (value: Any, field: pydantic.fields.ModelField, model: pydantic.BaseModel), and returns a StatefulObjectBase
ValidationErrors are not handled right now and crash everything. They need to be handled gracefully.DateRange type, tuple[T, T] types, and Optional[T] do not currently work with lazy_text_area(). Sorry about that.flatten=True for StateManager.form() does not sync with text areas and lazy text areas. (Flatten mode is still in development, and should be avoided if you are using text areas.)Streamlit is a trademark of Streamlit Inc.
This package is unaffiliated with Streamlit Inc. and Pydantic.
FAQs
Easy state management in Streamlit with Pydantic.
We found that statelit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.