Streamlit form component defined by a Pydantic model
Project description
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)
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
File details
Details for the file streamlit_pydantic_form-0.0.5.tar.gz
.
File metadata
- Download URL: streamlit_pydantic_form-0.0.5.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.1 Linux/5.15.153.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44f51175f5b9c9bcc7334d2bd83ef73d526f47c9ebb13d35a1db60aa15b670a0 |
|
MD5 | 9fcc82e2800290eec54b7ca2caf97959 |
|
BLAKE2b-256 | b56b5127bb7556a959ef9f4ef292c1bbc0a7b06ce7e8af24742afcafd796c7a2 |
File details
Details for the file streamlit_pydantic_form-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: streamlit_pydantic_form-0.0.5-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.1 Linux/5.15.153.1-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d267aea7693ceed106d259120028f88d61cdaae691a3a55f0227461606042755 |
|
MD5 | f1efa6b79f198bc40034ee8ca214bb29 |
|
BLAKE2b-256 | 26fc2a7100920fc0d40c2b13516e769f15d4a3d0ede5dddb3803dedaacce829a |