Skip to main content

Auto-generate Streamlit UI from Pydantic Models & Dataclasses.

Project description

Streamlit Pydantic

Auto-generate Streamlit UI elements from Pydantic models.

Ruff Rye

Getting StartedDocumentationSupportReport a BugContributionChangelog

Streamlit-pydantic makes it easy to auto-generate UI elements from Pydantic models or dataclasses. Just define your data model and turn it into a full-fledged UI form. It supports data validation, nested models, and field limitations. Streamlit-pydantic can be easily integrated into any Streamlit app.


Try out and explore various examples in our playground here.


Highlights

  • 🪄  Auto-generated UI elements from Pydantic models & Dataclasses.
  • 📇  Out-of-the-box data validation.
  • 📑  Supports nested Pydantic models.
  • 📏  Supports field limits and customizations.
  • 🎈  Easy to integrate into any Streamlit app.

Getting Started

Installation

pip install streamlit-pydantic

Usage

  1. Create a script (my_script.py) with a Pydantic model and render it via pydantic_form:

    import streamlit as st
    import streamlit_pydantic as sp
    from pydantic import BaseModel
    
    
    class ExampleModel(BaseModel):
        some_text: str
        some_number: int
        some_boolean: bool
    
    data = sp.pydantic_form(key="my_sample_form", model=ExampleModel)
    if data:
        st.json(data.model_dump())
    
  2. Run the streamlit server on the python script: streamlit run my_script.py

  3. You can find additional examples in the examples section below.

Examples


👉  Try out and explore these examples in our playground here


The following collection of examples demonstrates how Streamlit Pydantic can be applied in more advanced scenarios. You can find additional - even more advanced - examples in the examples folder or on the playground.

Simple Form

import streamlit as st
import streamlit_pydantic as sp
from pydantic import BaseModel


class ExampleModel(BaseModel):
    some_text: str
    some_number: int
    some_boolean: bool

data = sp.pydantic_form(key="my_sample_form", model=ExampleModel)
if data:
    st.json(data.model_dump())

Date Validation

import streamlit as st
import streamlit_pydantic as sp
from pydantic import BaseModel, Field, HttpUrl
from pydantic_extra_types.color import Color

class ExampleModel(BaseModel):
    url: HttpUrl
    color: Color = Field("blue", format="text")
    email: str = Field(..., max_length=100, regex=r"^\S+@\S+$")

data = sp.pydantic_form(key="my_form", model=ExampleModel)
if data:
    st.json(data.model_dump_json())

Dataclasses Support

import dataclasses
import json

import streamlit as st
from pydantic.json import pydantic_encoder

import streamlit_pydantic as sp


@dataclasses.dataclass
class ExampleModel:
    some_number: int
    some_boolean: bool
    some_text: str = "default input"


data = sp.pydantic_form(key="my_dataclass_form", model=ExampleModel)
if data:
    st.json(dataclasses.asdict(data))

Complex Nested Model

from enum import Enum
from typing import Set

import streamlit as st
from pydantic import BaseModel, Field

import streamlit_pydantic as sp


class OtherData(BaseModel):
    text: str
    integer: int


class SelectionValue(str, Enum):
    FOO = "foo"
    BAR = "bar"


class ExampleModel(BaseModel):
    long_text: str = Field(
        ..., format="multi-line", description="Unlimited text property"
    )
    integer_in_range: int = Field(
        20,
        ge=10,
        le=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."
    )
    read_only_text: str = Field(
        "Lorem ipsum dolor sit amet",
        description="This is a ready only text.",
        readOnly=True,
    )
    single_object: OtherData = Field(
        ...,
        description="Another object embedded into this model.",
    )


data = sp.pydantic_form(key="my_form", model=ExampleModel)
if data:
    st.json(data.model_dump_json())

Render Input

from pydantic import BaseModel

import streamlit_pydantic as sp


class ExampleModel(BaseModel):
    some_text: str
    some_number: int = 10  # Optional
    some_boolean: bool = True  # Option


input_data = sp.pydantic_input(
    "model_input", model=ExampleModel, group_optional_fields="sidebar"
)

Render Output

import datetime

from pydantic import BaseModel, Field

import streamlit_pydantic as sp


class ExampleModel(BaseModel):
    text: str = Field(..., description="A text property")
    integer: int = Field(..., description="An integer property.")
    date: datetime.date = Field(..., description="A date.")


instance = ExampleModel(text="Some text", integer=40, date=datetime.date.today())
sp.pydantic_output(instance)

Custom Form

import streamlit as st
from pydantic import BaseModel

import streamlit_pydantic as sp


class ExampleModel(BaseModel):
    some_text: str
    some_number: int = 10
    some_boolean: bool = True


with st.form(key="pydantic_form"):
    data = sp.pydantic_input(key="my_custom_form_model", model=ExampleModel)
    submit_button = st.form_submit_button(label="Submit")
    obj = ExampleModel(data)

if data:
    st.json(obj.model_dump())

Support & Feedback

Type Channel
🚨  Bug Reports
🎁  Feature Requests
👩‍💻  Usage Questions
📢  Announcements

Documentation

The API documentation can be found here. To generate UI elements, you can use the high-level pydantic_form method. Or the more flexible lower-level pydantic_input and pydantic_output methods. See the examples section on how to use those methods.

Contribution

Development

This repo uses Rye for development. To get started, install Rye and sync the project:

rye sync

Run the playground app:

rye run playground

Run linting and type checks:

rye run checks

[!TIP] The linting and formatting is using ruff and type-checking is done with mypy. You can use the ruff and mypy extensions of your IDE to automatically run these checks during development.

Format the code:

rye run format

Run tests:

rye test

Licensed MIT.

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-0.6.1rc3.tar.gz (124.9 kB view details)

Uploaded Source

Built Distribution

streamlit_pydantic-0.6.1rc3-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_pydantic-0.6.1rc3.tar.gz.

File metadata

  • Download URL: streamlit_pydantic-0.6.1rc3.tar.gz
  • Upload date:
  • Size: 124.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for streamlit_pydantic-0.6.1rc3.tar.gz
Algorithm Hash digest
SHA256 466ea21025a25904ac0f8ce84212cc5bc7bf1d874d85960c305b57eafff023c7
MD5 db45b3f22c7ad1031f55905747d35f06
BLAKE2b-256 f8b1ae7b22eb8c8402c58699ef0209a6dedd8ca4125809c7e4d634054241ba9a

See more details on using hashes here.

File details

Details for the file streamlit_pydantic-0.6.1rc3-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_pydantic-0.6.1rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 eee8408eddb37ef420de31bb8f0f64c3de55fce8495535f896ca339d1bf832fe
MD5 7da9578d7ef099d2ea221062083c6697
BLAKE2b-256 c9020d6441523b09feab3d24a82d2a2853ad60832a48850fb048cd2460cf6b13

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page