Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lukasmasuch/streamlit-pydantic
🪄 Auto-generate Streamlit UI from Pydantic Models and Dataclasses.
https://github.com/lukasmasuch/streamlit-pydantic
dataclasses forms json-schema pydantic python streamlit streamlit-component streamlit-library type-hints typing
Last synced: 5 days ago
JSON representation
🪄 Auto-generate Streamlit UI from Pydantic Models and Dataclasses.
- Host: GitHub
- URL: https://github.com/lukasmasuch/streamlit-pydantic
- Owner: lukasmasuch
- License: mit
- Created: 2021-07-09T23:03:31.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-18T18:31:02.000Z (5 months ago)
- Last Synced: 2025-01-09T19:37:32.389Z (12 days ago)
- Topics: dataclasses, forms, json-schema, pydantic, python, streamlit, streamlit-component, streamlit-library, type-hints, typing
- Language: Python
- Homepage: https://st-pydantic.streamlit.app
- Size: 469 KB
- Stars: 480
- Watchers: 8
- Forks: 65
- Open Issues: 34
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Support: .github/SUPPORT.md
Awesome Lists containing this project
README
Streamlit Pydantic
Auto-generate Streamlit UI elements from Pydantic models.
Getting Started •
Documentation •
Support •
Report a Bug •
Contribution •
ChangelogStreamlit-pydantic makes it easy to auto-generate UI elements from [Pydantic](https://github.com/samuelcolvin/pydantic/) models or [dataclasses](https://docs.python.org/3/library/dataclasses.html). 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
```bash
pip install streamlit-pydantic
```### Usage
1. Create a script (`my_script.py`) with a Pydantic model and render it via `pydantic_form`:
```python
import streamlit as st
import streamlit_pydantic as sp
from pydantic import BaseModelclass ExampleModel(BaseModel):
some_text: str
some_number: int
some_boolean: booldata = 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](#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](./examples) or on the [playground](https://st-pydantic.streamlit.app/).
### Simple Form
```python
import streamlit as st
import streamlit_pydantic as sp
from pydantic import BaseModelclass ExampleModel(BaseModel):
some_text: str
some_number: int
some_boolean: booldata = sp.pydantic_form(key="my_sample_form", model=ExampleModel)
if data:
st.json(data.model_dump())
```### Date Validation
```python
import streamlit as st
import streamlit_pydantic as sp
from pydantic import BaseModel, Field, HttpUrl
from pydantic_extra_types.color import Colorclass 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
```python
import dataclasses
import jsonimport streamlit as st
from pydantic.json import pydantic_encoderimport 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
```python
from enum import Enum
from typing import Setimport streamlit as st
from pydantic import BaseModel, Fieldimport streamlit_pydantic as sp
class OtherData(BaseModel):
text: str
integer: intclass 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
```python
from pydantic import BaseModelimport streamlit_pydantic as sp
class ExampleModel(BaseModel):
some_text: str
some_number: int = 10 # Optional
some_boolean: bool = True # Optioninput_data = sp.pydantic_input(
"model_input", model=ExampleModel, group_optional_fields="sidebar"
)
```### Render Output
```python
import datetimefrom 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
```python
import streamlit as st
from pydantic import BaseModelimport streamlit_pydantic as sp
class ExampleModel(BaseModel):
some_text: str
some_number: int = 10
some_boolean: bool = Truewith 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](./docs). To generate UI elements, you can use the high-level [`pydantic_form`](./docs/streamlit_pydantic.ui_renderer.md#function-pydantic_form) method. Or the more flexible lower-level [`pydantic_input`](./docs/streamlit_pydantic.ui_renderer.md#function-pydantic_input) and [`pydantic_output`](./docs/streamlit_pydantic.ui_renderer.md#function-pydantic_output) methods. See the [examples](#examples) section on how to use those methods.
## Contribution
- Pull requests are encouraged and always welcome. Read our [contribution guidelines](https://github.com/lukasmasuch/streamlit-pydantic/tree/main/CONTRIBUTING.md) and check out [help-wanted](https://github.com/lukasmasuch/streamlit-pydantic/issues?utf8=%E2%9C%93&q=is%3Aopen+is%3Aissue+label%3A"help+wanted"+sort%3Areactions-%2B1-desc+) issues.
- Submit Github issues for any [feature request and enhancement](https://github.com/lukasmasuch/streamlit-pydantic/issues/new?assignees=&labels=type%3Aenhancement%2Cstatus%3Aneeds-triage&projects=&template=02_feature-request.yml), [bugs](https://github.com/lukasmasuch/streamlit-pydantic/issues/new?assignees=&labels=type%3Abug%2Cstatus%3Aneeds-triage&projects=&template=01_bug-report.yml), or [documentation](https://github.com/lukasmasuch/streamlit-pydantic/issues/new?assignees=&labels=type%3Adocs%2Cstatus%3Aneeds-triage&projects=&template=03_documentation.yml) problems.
- By participating in this project, you agree to abide by its [Code of Conduct](https://github.com/lukasmasuch/streamlit-pydantic/blob/main/.github/CODE_OF_CONDUCT.md).
- The [development section](#development) below contains information on how to build and test the project after you have implemented some changes.## Development
This repo uses [Rye](https://rye.astral.sh/) for development. To get started, [install Rye](https://rye.astral.sh/) and sync the project:
```bash
rye sync
```Run the playground app:
```bash
rye run playground
```Run linting and type checks:
```bash
rye run checks
```> [!TIP]
> The linting and formatting is using [ruff](https://github.com/astral-sh/ruff) and
> type-checking is done with [mypy](https://github.com/python/mypy). You can use
> the ruff and mypy extensions of your IDE to automatically run these checks
> during development.Format the code:
```bash
rye run format
```Run tests:
```bash
rye test
```---
Licensed **MIT**.