Streamlit Pydnatic Form
Streamlit form component defined by a Pydantic model.
Installation
pip install streamlit-pydantic-form
Usage
Without streamlit-pydantic-form
import streamlit as st
with st.form("form_0"):
slider_val = st.slider("Form slider")
checkbox_val = st.checkbox("Form checkbox")
submitted = st.form_submit_button("Submit")
if submitted:
st.write("slider", slider_val, "checkbox", checkbox_val)
With streamlit-pydantic-form
With streamlit-pydantic-form you can define a Pydantic model and use it to automatically generate a form.
from typing import Annotated
import streamlit as st
from pydantic import BaseModel
from streamlit_pydantic_form import st_auto_form, widget
class SimpleFormModel(BaseModel):
slider_val: Annotated[int, widget.Slider("Form slider")]
checkbox_val: Annotated[bool, widget.Checkbox("Form checkbox")]
with st_auto_form("form_1", model=SimpleFormModel) as simple_form:
val = simple_form.input_widgets()
submitted = st.form_submit_button("Submit")
if submitted:
st.write("slider", val.slider_val, "checkbox", val.checkbox_val)
Nested Model
You can also define a nested model.
from typing import Annotated
import streamlit as st
from pydantic import BaseModel
from streamlit_pydantic_form import st_auto_form, widget
class ChildFormModel(BaseModel):
slider_val: Annotated[int, widget.Slider("Child slider")]
class ParentFormModel(BaseModel):
slider_val: Annotated[int, widget.Slider("Parent slider")]
checkbox_val: Annotated[bool, widget.Checkbox("Parent checkbox")]
child: ChildFormModel
with st_auto_form("form_2", model=ParentFormModel) as parent_form:
val2 = parent_form.input_widgets()
submitted = st.form_submit_button("Submit")
if submitted:
st.write(
"parent slider",
val2.slider_val,
"parent checkbox",
val2.checkbox_val,
"child slider",
val2.child.slider_val,
)
Custom Widget
You can define a custom widget by defining a custom WidgetBuilder and pass it to st_auto_form as widget_builder.
from typing import Annotated
import streamlit as st
from pydantic import BaseModel
from streamlit_pydantic_form import st_auto_form, widget
# Custom widget builder
class PointWidget(widget.WidgetBuilder):
def build(self) -> PointModel:
x = st.slider("X")
y = st.slider("Y")
return PointModel(x=x, y=y)
with st_auto_form("form_3", model=PointModel, widget_builder=PointWidget()) as point_form:
val3 = point_form.input_widgets()
submitted = st.form_submit_button("Submit")
if submitted:
st.write("x", val3.x, "y", val3.y)
You can also use the Annotated type hint to define a custom widget.
from typing import Annotated
import streamlit as st
from pydantic import BaseModel
from streamlit_pydantic_form import st_auto_form, widget
# External model
class PointModel(BaseModel):
x: int
y: int
# Custom widget
class PointWidget(widget.WidgetBuilder):
def build(self) -> PointModel:
x = st.slider("X")
y = st.slider("Y")
return PointModel(x=x, y=y)
# Form model
class PointFormModel(BaseModel):
p: Annotated[PointModel, PointWidget()]
with st_auto_form("form_4", model=PointFormModel) as point_form2:
val4 = point_form2.input_widgets()
submitted = st.form_submit_button("Submit")
if submitted:
st.write("x", val4.p.x, "y", val4.p.y)
Metadata
Release files for streamlit-pydantic-form 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| streamlit_pydantic_form-0.0.6.tar.gz | 7.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| streamlit_pydantic_form-0.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.4 kB
Release files / streamlit_pydantic_form-0.0.6.tar.gz
| Download URL | streamlit_pydantic_form-0.0.6.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d7152d7c1dd37b6a7a7449db6437c3bc0f2258792c443905ea3f53c300b5c8f5
|
|
BLAKE2b-256 checksum How to use checksums |
9ac1bfd5af451abc110a3b9dae0c2ca80394eafabb9c67633343e06838e87d2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.1 Linux/5.15.153.1-microsoft-standard-WSL2
|
Release files / streamlit_pydantic_form-0.0.6-py3-none-any.whl
| Download URL | streamlit_pydantic_form-0.0.6-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0d82c1fde8e093c1f644483bf6d5e583502478a7d9029cbb4754786b049dd312
|
|
BLAKE2b-256 checksum How to use checksums |
461da99deaaadbc652d1cb9487062ed434014140fc6494dc0d14ee268e48b64c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.11.1 Linux/5.15.153.1-microsoft-standard-WSL2
|
Security for streamlit-pydantic-form 0.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.