Pydantic to HTML Converter
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.
Common errors
-
AttributeError: 'BaseModel' object has no attribute 'dict'
cause This error occurs when using Pydantic V2 or newer models with methods deprecated in V1, such as `.dict()` or `.json()`.fixReplace `.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)
cause 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.fixEnsure 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
cause 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.fixProvide 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'`).
Warnings
- breaking Pydantic 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.
- gotcha Pydantic'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.
- gotcha Debugging `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.
Install
-
pip install pydantic-to-html
Imports
- render_html
from pydantic_to_html import render_html
- BaseModel
from pydantic import BaseModel
Quickstart
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)