UI components for Python using Pydantic and Jinja2 templates
Project description
PyJinHx
Declare reusable, type-safe UI components for template-based web apps in Python. PyJinHx combines Pydantic models with Jinja2 templates to give you automatic template discovery, nested composition, and JavaScript automatic collection—all without manual wiring.
Installation
pip install pyjinhx
Core Capabilities
- Automatic Template Discovery: Place an HTML template next to your component class with a matching name (e.g.,
button.htmlforButton) - Global Component Registry: All components register by
idand are accessible in any template via{{ component_id }} - Nested Components: Pass components as fields—works with single components, lists, and dictionaries
- Property Access: Access nested component properties via
.props(e.g.,{{ nested.props.text }}) - JavaScript Automatic Collection: Automatically collects
.jsfiles next to templates and bundles them into a single<script>tag - Extra HTML Templates: Include additional HTML files via the
htmlfield
Example
# components/ui/button.py
from pyjinhx import BaseComponent
class Button(BaseComponent):
id: str
text: str
<!-- components/ui/button.html -->
<button id="{{ id }}">{{ text }}</button>
# components/ui/card.py
from pyjinhx import BaseComponent
from components.ui.button import Button
class Card(BaseComponent):
id: str
title: str
action_button: Button
menu_items: list[Button]
<!-- components/ui/card.html -->
<div id="{{ id }}" class="card">
<h2>{{ title }}</h2>
<div class="action">
<p>Action: {{ action_button.props.text }}</p>
{{ action_button.html }}
</div>
<ul class="menu">
{% for item in menu_items %}
<li>
<span>Item: {{ item.props.text }}</span>
{{ item.html }}
</li>
{% endfor %}
</ul>
</div>
# Usage
from components.ui.card import Card
from components.ui.button import Button
action_btn = Button(id="submit-btn", text="Submit")
menu_buttons = [
Button(id="menu-1", text="Home"),
Button(id="menu-2", text="About"),
Button(id="menu-3", text="Contact")
]
card = Card(
id="form-card",
title="User Form",
action_button=action_btn,
menu_items=menu_buttons
)
html = card.render()
<!-- Rendered output -->
<div id="form-card" class="card">
<h2>User Form</h2>
<div class="action">
<p>Action: Submit</p>
<button id="submit-btn">Submit</button>
</div>
<ul class="menu">
<li>
<span>Item: Home</span>
<button id="menu-1">Home</button>
</li>
<li>
<span>Item: About</span>
<button id="menu-2">About</button>
</li>
<li>
<span>Item: Contact</span>
<button id="menu-3">Contact</button>
</li>
</ul>
</div>
TODO
- Add tests for generic components, i.e. BaseComponent declarations with extra properties
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 pyjinhx-0.1.6.tar.gz.
File metadata
- Download URL: pyjinhx-0.1.6.tar.gz
- Upload date:
- Size: 24.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04f8ba2412f0fd6ff3aba0c25eeae106ecc025ce4c929fbaa7ade05a3f4c8095
|
|
| MD5 |
bbe2345a8f376168c96eda1b9b306f8c
|
|
| BLAKE2b-256 |
84d945bc03cd1b4e14484553cf1ce05d92f6bc0d7f283815e8462d2759902f39
|
File details
Details for the file pyjinhx-0.1.6-py3-none-any.whl.
File metadata
- Download URL: pyjinhx-0.1.6-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e84e2e90cfb45829267d69aa6a93e0e96ac97849bd084c747a12c604ffabfdbd
|
|
| MD5 |
509a944ba5cef2880033eb6c3df30c8b
|
|
| BLAKE2b-256 |
ee1199d77251931ed88bdf87f59db0850efb133e076388d5db14207a810fad87
|