A library to convert Pydantic models to HTML
Project description
pydantic-to-html
A Python library that automatically converts Pydantic models into structured HTML using safe and semantic defaults.
Overview
pydantic-to-html infers an appropriate HTML structure based on the types of fields in a Pydantic model, including support for nested models, lists, and forms for user input.
Installation
pip install pydantic-to-html
Usage
from pydantic import BaseModel
from pydantic_to_html import render_html
class User(BaseModel):
name: str
email: str
age: int
user = User(name="John Doe", email="john@example.com", age=30)
html = render_html(user)
print(html)
Features
- ✅ Automatic HTML Conversion – Converts any Pydantic model into a structured HTML representation.
- ✅ Recursive Nesting – Handles nested models and lists of models gracefully.
- ✅ Table-Based Rendering – Uses elements for structured data representation.
- ✅ List Support – Renders lists as
- or
- ✅ Automatic Form Generation – Can generate editable forms with elements.
- ✅ Type-Safe Defaults – Uses appropriate form fields based on the Pydantic field type (
str, int, bool, datetime, etc.). - ✅ Customizable Themes – Allows passing custom CSS classes or styles.
- ✅ HTMX Support – Optionally integrates HTMX for live updates.
- ✅ Validation & Constraints – Uses Pydantic field constraints to enforce validation on form inputs.
- , depending on the data type.
API
def render_html( model: BaseModel, editable: bool = False, theme: str | None = None, htmx: bool = False, htmx_mode: str = "full", # "full" | "inline" | "none" max_depth: int | None = None, ) -> str: """Converts a Pydantic model into HTML (table or form)."""
Type Mapping
- Lists of Primitives (
List[str]) →- with
- elements.
- Lists of Models (
List[Model]) → Rendered as nested tables. - Dictionaries (
Dict[str, Any]) → Rendered as a key-value table if structured, or- if unstructured.
- Nested Pydantic Models → Recursively rendered as nested tables, with a configurable max_depth to limit depth.
Validation & Constraints Mapping
- Min/Max Length (
Field(min_length=3, max_length=50)) → minlength="3" maxlength="50" - Greater Than (
Field(gt=0)) → min="0" - Regex Constraints (
Field(regex="^[a-z]+$") → pattern="^[a-z]+$"
Error Handling for Edge Cases
- Unsupported Types (
bytes,UUID,Callable)- UUID → Rendered as str(uuid)
- bytes → Rendered as "[binary data]"
- Callable → Rendered as "[function]"
- Default Factories (
Field(default_factory=...)) → Rendered appropriately with their defaults.
Roadmap
This is our current development roadmap. Contributions are welcome!
Phase 1: Basic Rendering ✅
- Convert flat Pydantic models into tables
- Support nested models using recursion
- Support lists as
- elements
Phase 2: Editable Forms ✅
- Render models as forms with fields
- Infer input types from field types
- Handle lists as multiple input fields
Phase 3: Advanced Features ✅
- Add HTMX integration for interactive forms
- Implement theme support
- Add validation based on Pydantic constraints
- Support for complex types (Enum, Literal, etc.)
- Build and publish package to PyPI
Phase 4: Future Enhancements
- Add custom rendering hooks
- Performance improvements for large models
- Add support for custom widgets
- Add accessibility features
- Create comprehensive documentation site
Installation
The package is available on PyPI:
pip install pydantic-to-html
License
MIT
| Pydantic Type | Default HTML Element | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| str | |||||||||||||||
| int | |||||||||||||||
| float | |||||||||||||||
| bool | |||||||||||||||
| datetime | |||||||||||||||
| List[str] | Multiple inside |
||||||||||||||
| List[Model] | Nested
HTMX Modes
Handling Nested Models & Lists |
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_to_html-0.1.1.tar.gz.
File metadata
- Download URL: pydantic_to_html-0.1.1.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e2444b63084954ce235d3bed559175c124ac4a17ccde7d1c4318c4153c20e41
|
|
| MD5 |
8dff9f52481dac0023b9b782c8fec934
|
|
| BLAKE2b-256 |
c39a665429bf2629fdf084e33294a566631aa4f328f3860c14097910f1974ade
|
File details
Details for the file pydantic_to_html-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pydantic_to_html-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5bc37371d7109438e6030c75594c896cbbf824ee4561b88a39dab636a518af0
|
|
| MD5 |
7a519b3644adb1e2669bfc104f906848
|
|
| BLAKE2b-256 |
56964052dcbe298bef0d5fb684c7cb7de014b2017431a6b238d9d3070a4f9a14
|