# Streamlit Pydnatic Form
Streamlit form component defined by a Pydantic model.
## Installation
```bash
pip install streamlit-pydantic-form
```
## Usage
### Without `streamlit-pydantic-form`
```python
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.
```python
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.
```python
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`.
```python
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.
```python
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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/shunichironomura/streamlit-pydantic-form",
"name": "streamlit-pydantic-form",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": "streamlit, pydantic, form",
"author": "Shunichiro Nomura",
"author_email": "nomura@space.t.u-tokyo.ac.jp",
"download_url": "https://files.pythonhosted.org/packages/9a/c1/bfd5af451abc110a3b9dae0c2ca80394eafabb9c67633343e06838e87d2b/streamlit_pydantic_form-0.0.6.tar.gz",
"platform": null,
"description": "# Streamlit Pydnatic Form\n\nStreamlit form component defined by a Pydantic model.\n\n## Installation\n\n```bash\npip install streamlit-pydantic-form\n```\n\n## Usage\n\n### Without `streamlit-pydantic-form`\n\n```python\nimport streamlit as st\n\nwith st.form(\"form_0\"):\n slider_val = st.slider(\"Form slider\")\n checkbox_val = st.checkbox(\"Form checkbox\")\n\n submitted = st.form_submit_button(\"Submit\")\n if submitted:\n st.write(\"slider\", slider_val, \"checkbox\", checkbox_val)\n```\n\n### With `streamlit-pydantic-form`\n\nWith `streamlit-pydantic-form` you can define a Pydantic model and use it to automatically generate a form.\n\n```python\nfrom typing import Annotated\n\nimport streamlit as st\nfrom pydantic import BaseModel\n\nfrom streamlit_pydantic_form import st_auto_form, widget\n\nclass SimpleFormModel(BaseModel):\n slider_val: Annotated[int, widget.Slider(\"Form slider\")]\n checkbox_val: Annotated[bool, widget.Checkbox(\"Form checkbox\")]\n\n\nwith st_auto_form(\"form_1\", model=SimpleFormModel) as simple_form:\n val = simple_form.input_widgets()\n submitted = st.form_submit_button(\"Submit\")\n if submitted:\n st.write(\"slider\", val.slider_val, \"checkbox\", val.checkbox_val)\n```\n\n### Nested Model\n\nYou can also define a nested model.\n\n```python\nfrom typing import Annotated\n\nimport streamlit as st\nfrom pydantic import BaseModel\n\nfrom streamlit_pydantic_form import st_auto_form, widget\n\nclass ChildFormModel(BaseModel):\n slider_val: Annotated[int, widget.Slider(\"Child slider\")]\n\nclass ParentFormModel(BaseModel):\n slider_val: Annotated[int, widget.Slider(\"Parent slider\")]\n checkbox_val: Annotated[bool, widget.Checkbox(\"Parent checkbox\")]\n child: ChildFormModel\n\nwith st_auto_form(\"form_2\", model=ParentFormModel) as parent_form:\n val2 = parent_form.input_widgets()\n submitted = st.form_submit_button(\"Submit\")\n if submitted:\n st.write(\n \"parent slider\",\n val2.slider_val,\n \"parent checkbox\",\n val2.checkbox_val,\n \"child slider\",\n val2.child.slider_val,\n )\n```\n\n### Custom Widget\n\nYou can define a custom widget by defining a custom `WidgetBuilder` and pass it to `st_auto_form` as `widget_builder`.\n\n```python\nfrom typing import Annotated\n\nimport streamlit as st\nfrom pydantic import BaseModel\n\nfrom streamlit_pydantic_form import st_auto_form, widget\n\n# Custom widget builder\nclass PointWidget(widget.WidgetBuilder):\n def build(self) -> PointModel:\n x = st.slider(\"X\")\n y = st.slider(\"Y\")\n return PointModel(x=x, y=y)\n\nwith st_auto_form(\"form_3\", model=PointModel, widget_builder=PointWidget()) as point_form:\n val3 = point_form.input_widgets()\n submitted = st.form_submit_button(\"Submit\")\n if submitted:\n st.write(\"x\", val3.x, \"y\", val3.y)\n```\n\nYou can also use the `Annotated` type hint to define a custom widget.\n\n```python\nfrom typing import Annotated\n\nimport streamlit as st\nfrom pydantic import BaseModel\n\nfrom streamlit_pydantic_form import st_auto_form, widget\n\n# External model\nclass PointModel(BaseModel):\n x: int\n y: int\n\n# Custom widget\nclass PointWidget(widget.WidgetBuilder):\n def build(self) -> PointModel:\n x = st.slider(\"X\")\n y = st.slider(\"Y\")\n return PointModel(x=x, y=y)\n\n# Form model\nclass PointFormModel(BaseModel):\n p: Annotated[PointModel, PointWidget()]\n\nwith st_auto_form(\"form_4\", model=PointFormModel) as point_form2:\n val4 = point_form2.input_widgets()\n submitted = st.form_submit_button(\"Submit\")\n if submitted:\n st.write(\"x\", val4.p.x, \"y\", val4.p.y)\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Streamlit form component defined by a Pydantic model",
"version": "0.0.6",
"project_urls": {
"Homepage": "https://github.com/shunichironomura/streamlit-pydantic-form",
"Repository": "https://github.com/shunichironomura/streamlit-pydantic-form"
},
"split_keywords": [
"streamlit",
" pydantic",
" form"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "461da99deaaadbc652d1cb9487062ed434014140fc6494dc0d14ee268e48b64c",
"md5": "7806982ea8a3a32570e9dc8a1bfc6adb",
"sha256": "0d82c1fde8e093c1f644483bf6d5e583502478a7d9029cbb4754786b049dd312"
},
"downloads": -1,
"filename": "streamlit_pydantic_form-0.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7806982ea8a3a32570e9dc8a1bfc6adb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 7844,
"upload_time": "2024-07-29T18:19:14",
"upload_time_iso_8601": "2024-07-29T18:19:14.440892Z",
"url": "https://files.pythonhosted.org/packages/46/1d/a99deaaadbc652d1cb9487062ed434014140fc6494dc0d14ee268e48b64c/streamlit_pydantic_form-0.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9ac1bfd5af451abc110a3b9dae0c2ca80394eafabb9c67633343e06838e87d2b",
"md5": "bd743d55db8eb2d6e2807453c9e1f9d9",
"sha256": "d7152d7c1dd37b6a7a7449db6437c3bc0f2258792c443905ea3f53c300b5c8f5"
},
"downloads": -1,
"filename": "streamlit_pydantic_form-0.0.6.tar.gz",
"has_sig": false,
"md5_digest": "bd743d55db8eb2d6e2807453c9e1f9d9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 7589,
"upload_time": "2024-07-29T18:19:16",
"upload_time_iso_8601": "2024-07-29T18:19:16.315366Z",
"url": "https://files.pythonhosted.org/packages/9a/c1/bfd5af451abc110a3b9dae0c2ca80394eafabb9c67633343e06838e87d2b/streamlit_pydantic_form-0.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-29 18:19:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "shunichironomura",
"github_project": "streamlit-pydantic-form",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "streamlit-pydantic-form"
}