Registry / serialization / pydantic-to-html

pydantic-to-html

JSON →
library0.2.0pypypi✓ verified 87d ago

A Python library designed to automatically convert Pydantic models into structured HTML. It intelligently infers appropriate HTML elements and structures based on the Pydantic model's field types, offering support for nested models, lists, and automatic form generation. The library, currently at version 0.2.0, provides features for customizable themes and optional HTMX integration for interactive forms.

pip install pydantic-to-html
INSTALL
IMPORT
SIG · PYDANTIC-TO-HTML
P
pydantic-to-html
serializationpythonv0.2.0
Install
2.8s avg
Import
555ms
Disk
25MB
Pass rate
6/ 10
Env Coverage6 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✕ build_error
py 3.11
✓ —
✓ 3.08s
py 3.12
✓ —
✓ 2.55s
py 3.13
✓ —
✓ 2.68s
py 3.9
✕ build_error
✕ build_error
25MB installed
● package 25MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

render_html
from pydantic_to_html import render_html
BaseModel
from pydantic import BaseModel

This quickstart demonstrates how to define a Pydantic model (including nested models), instantiate it with data, and then convert it into an editable HTML form using `render_html`. The `theme` parameter is used to apply basic styling (e.g., 'bootstrap').

from pydantic import BaseModel from pydantic_to_html import render_html class Address(BaseModel): street: str city: str zip_code: str class User(BaseModel): name: str email: str age: int is_active: bool = True address: Address user_data = User( name="Jane Doe", email="jane.doe@example.com", age=29, address=Address(street="123 Main St", city="Anytown", zip_code="12345") ) html_output = render_html(user_data, editable=True, theme="bootstrap") print(html_output)
Debug
Known issues
breakingPydantic V1 to V2 migration introduces significant breaking changes that affect `pydantic-to-html`'s underlying Pydantic models. Key method renames (e.g., `.dict()` to `.model_dump()`, `.json()` to `.model_dump_json()`) and configuration changes (e.g., `Config` class to `model_config` dictionary) are common pitfalls.
fix
Ensure your Pydantic models adhere to Pydantic V2 syntax. Use `model_dump()` for dictionary representation, `model_dump_json()` for JSON, and migrate `Config` classes to `model_config` attributes. Consider using the `bump-pydantic` tool for automated migration assistance.
affects: Pydantic <2.0.0 (when migrating to >=2.0.0)
gotchaPydantic's default behavior for type coercion can sometimes lead to unexpected data conversions (e.g., a string '10' being coerced to an integer 10). While convenient, it can mask underlying data issues if not explicitly handled or if strict validation is desired.
fix
For stricter validation, consider using Pydantic's `strict` mode (`model_config = {'strict': True}`) or specific Pydantic types like `StrictStr`, `StrictInt`. Always validate input data against your model's expectations.
affects: All versions of `pydantic-to-html` (due to Pydantic's behavior)
gotchaDebugging `ValidationError` exceptions from Pydantic models can be challenging due to their detailed, nested structure. Misinterpreting the `loc`, `msg`, and `type` fields in the error details can lead to incorrect fixes.
fix
When catching `ValidationError`, iterate through `exc.errors()` to inspect each individual error. Pay close attention to the `loc` field to pinpoint the exact problematic field in your model. Pydantic's documentation on error handling provides detailed explanations of error types and structures.
affects: All versions of `pydantic-to-html` (due to Pydantic's behavior)
Errors
Common errors & fixes
AttributeError: 'BaseModel' object has no attribute 'dict'
This error occurs when using Pydantic V2 or newer models with methods deprecated in V1, such as `.dict()` or `.json()`.
fix
Replace `.dict()` with `.model_dump()` and `.json()` with `.model_dump_json()` for Pydantic V2 models.
pydantic. ValidationError: 1 validation error for [YourModelName] [field name] value is not a valid integer (type=type_error.integer)
Input data for a field specified as an integer (e.g., `age: int`) contains a non-integer value that Pydantic cannot coerce, such as a string that doesn't represent a number, or a boolean.
fix
Ensure that the data provided for integer fields is either an actual integer or a string that can be successfully converted to an integer by Pydantic. Review the input data for the indicated field and correct its type or value.
pydantic. ValidationError: 1 validation error for [YourModelName] [field name] field required
A required field in your Pydantic model was not provided in the input data, or was explicitly set to `None` without being marked as `Optional` or having a default value.
fix
Provide a value for the required field. If the field should be optional, define it using `Optional[Type]` (e.g., `field: Optional[str]`) or provide a default value (e.g., `field: str = 'default_value'`).
Upgrade
Version history
0.2.0latest on PyPI · released Mar 24, 2025
Audit
Dependencies
pydanticrequiredCore dependency for defining models to be converted to HTML. Requires Pydantic v2 or newer.
Agent activity
9 hits · last 30 days
node
8
Resources
pydantic-to-html — pip install pydantic-to-html · libregistry