Skip to main content

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)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_pydantic_form-0.0.6.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_pydantic_form-0.0.6-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_pydantic_form-0.0.6.tar.gz.

File metadata

  • Download URL: streamlit_pydantic_form-0.0.6.tar.gz
  • Upload date:
  • Size: 7.6 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

Hashes for streamlit_pydantic_form-0.0.6.tar.gz
Algorithm Hash digest
SHA256 d7152d7c1dd37b6a7a7449db6437c3bc0f2258792c443905ea3f53c300b5c8f5
MD5 bd743d55db8eb2d6e2807453c9e1f9d9
BLAKE2b-256 9ac1bfd5af451abc110a3b9dae0c2ca80394eafabb9c67633343e06838e87d2b

See more details on using hashes here.

File details

Details for the file streamlit_pydantic_form-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: streamlit_pydantic_form-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 7.8 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

Hashes for streamlit_pydantic_form-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0d82c1fde8e093c1f644483bf6d5e583502478a7d9029cbb4754786b049dd312
MD5 7806982ea8a3a32570e9dc8a1bfc6adb
BLAKE2b-256 461da99deaaadbc652d1cb9487062ed434014140fc6494dc0d14ee268e48b64c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page