Transform Pydantic models into Streamlit forms.
Project description
Streamlit Pydantic
Transform Pydantic Models into Streamlit UI components
Getting Started • Features & Screenshots • Documentation • Support • Report a Bug • Changelog
Streamlit-pydantic makes it easy to deal with Pydantic objects within Streamlit. It allows to directly generate Input as well as Output UI components based on a Pydantic models.
Getting Started
Installation
Requirements: Python 3.6+.
pip install streamlit-pydantic
Usage
Streamlit-pydantic provides the pydantic_input
and pydantic_output
methods to render input/output UI components from Pydantic models. Those methods can be easily embedded into any streamlit script. For example:
-
Create a script (
my_script.py
) with a Pydantic model and render it viapydantic_input
:from pydantic import BaseModel import streamlit_pydantic as sp class ExampleModel(BaseModel): text: str integer: int test: bool sp.pydantic_input(ExampleModel, "input_data")
-
Run the streamlit server on the python script:
streamlit run my_script.py
-
You can find additional examples in the examples section below.
Examples
The following collection of examples demonstrate how Streamlit Pydantic can be applied in more advanced scenarios. You can find additional - even more advanced - examples in the examples folder.
Simple Form
import datetime
import streamlit as st
from pydantic import BaseModel, Field
import streamlit_pydantic as sp
class ExampleModel(BaseModel):
short_text: str = Field(..., max_length=60, description="Short text property")
positive_integer: int = Field(
..., ge=0, multiple_of=10, description="Positive integer with step count of 10."
)
date: datetime.date = Field(
datetime.date.today(),
description="Date property.",
)
with st.form(key="pydantic_form"):
# Render input model -> input data is accesible via st.session_state["input_data"]
sp.pydantic_input(ExampleModel, "input_data")
submit_button = st.form_submit_button(label="Submit")
Input-output Form
from enum import Enum
from typing import Set
import streamlit as st
from pydantic import BaseModel, Field, ValidationError, parse_obj_as
import streamlit_pydantic as sp
class SelectionValue(str, Enum):
FOO = "foo"
BAR = "bar"
class ExampleModel(BaseModel):
long_text: str = Field(..., description="Unlimited text property")
integer_in_range: int = Field(
20,
ge=10,
lt=30,
multiple_of=2,
description="Number property with a limited range.",
)
single_selection: SelectionValue = Field(
..., description="Only select a single item from a set."
)
multi_selection: Set[SelectionValue] = Field(
..., description="Allows multiple items from a set."
)
with st.form(key="pydantic_form"):
# Render input model
sp.pydantic_input(ExampleModel, "input_data")
submit_button = st.form_submit_button(label="Submit")
if submit_button:
try:
# Get input data from session
input_data_obj = parse_obj_as(ExampleModel, st.session_state["input_data"])
# Show the input data
sp.pydantic_output(input_data_obj)
except ValidationError as ex:
st.error(ex)
Support & Feedback
Type | Channel |
---|---|
🚨 Bug Reports | |
🎁 Feature Requests | |
👩💻 Usage Questions | tbd |
📢 Announcements | tbd |
Features
TODO
Documentation
TODO
Contribution
- Pull requests are encouraged and always welcome. Read our contribution guidelines and check out help-wanted issues.
- Submit Github issues for any feature request and enhancement, bugs, or documentation problems.
- By participating in this project, you agree to abide by its Code of Conduct.
- The development section below contains information on how to build and test the project after you have implemented some changes.
Development
pip install universal-build
python build.py --make --check
Refer to our contribution guides for more detailed information on our build scripts and development process.
Licensed MIT. Created and maintained with ❤️ by developers from Berlin.
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
Built Distribution
Hashes for streamlit_pydantic-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75ea71177cf9ad82eba9039ff7b628ab4cc246d030e18f573d75167d05415749 |
|
MD5 | 65d296b7508481fc7a559af0becef390 |
|
BLAKE2b-256 | bce2b88fc9531f1f2af54002f938a54d366ac808ceaf6576f954a3e4e3f34ac1 |