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-htmlVerified import paths — ran on the pinned version, not inferred.
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').
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.
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.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.
Replace `.dict()` with `.model_dump()` and `.json()` with `.model_dump_json()` for Pydantic V2 models.
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.
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'`).