Skip to main content

UI components for Python using Pydantic and Jinja2 templates

Project description

PyJinHx

Build reusable, type-safe UI components for template-based web apps in Python. PyJinHx combines Pydantic models with Jinja2 templates to give you template discovery, component composition, and JavaScript bundling.

  • Automatic Template Discovery: Place templates next to component files—no manual paths
  • JavaScript Bundling: Automatically collects and bundles .js files from component directories
  • Composability: Nest components easily—works with single components, lists, and dictionaries
  • Flexible: Use Python classes for reusable components, HTML syntax for quick page composition
  • Type Safety: Pydantic models provide validation and IDE support

Installation

pip install pyjinhx

Example

This single example shows the full setup (Python classes + templates) and both ways to render:

  • Python-side: instantiate a component class and call .render().
  • Template-side: render an HTML-like string with custom tags via Renderer.

Step 1: Define component classes

# components/ui/button.py
from pyjinhx import BaseComponent


class Button(BaseComponent):
    id: str
    text: str
    variant: str = "default"
# components/ui/card.py
from pyjinhx import BaseComponent
from components.ui.button import Button


class Card(BaseComponent):
    id: str
    title: str
    action_button: Button
# components/ui/page.py
from pyjinhx import BaseComponent
from components.ui.card import Card


class Page(BaseComponent):
    id: str
    title: str
    main_card: Card

Step 2: Create templates (auto-discovered next to the class files)

<!-- components/ui/button.html -->
<button id="{{ id }}" class="btn btn-{{ variant }}">{{ text }}</button>
<!-- components/ui/card.html -->
<div id="{{ id }}" class="card">
  <h2>{{ title }}</h2>
  <div class="action">{{ action_button }}</div>
</div>
<!-- components/ui/page.html -->
<main id="{{ id }}">
  <h1>{{ title }}</h1>
  {{ main_card }}
</main>

Step 3A: Python-side rendering (BaseComponent.render())

from components.ui.button import Button
from components.ui.card import Card
from components.ui.page import Page

page = Page(
    id="home",
    title="Welcome",
    main_card=Card(
        id="hero",
        title="Get Started",
        action_button=Button(id="cta", text="Sign up", variant="primary"),
    ),
)

html = page.render()

Step 3B: Template-side rendering (Renderer(...).render(source))

from pyjinhx import Renderer

renderer = Renderer("./components", auto_id=True)

html = renderer.render(
    """
    <Page title="Welcome">
      <Card title="Get Started">
        <Button text="Sign up" variant="primary"/>
      </Card>
    </Page>
    """
)

Template-side rendering supports:

  • Type safety for registered classes: if Button(BaseComponent) exists, its fields are validated when <Button .../> is instantiated.
  • Generic tags: if there is no registered class, a generic BaseComponent is used as long as the template file can be found.

JavaScript & extra assets

  • Component-local JS: if a component class MyWidget has a sibling file my-widget.js, it is auto-collected and injected once at the root render level.
  • Extra JS: pass js=[...] with file paths; missing files are ignored.
  • Extra HTML files: pass html=[...] with file paths; they are rendered and exposed in the template context by filename stem (e.g. extra_content.htmlextra_content.html wrapper). Missing files raise FileNotFoundError.

FastAPI + HTMX example

Component class

# components/ui/button.py
from pyjinhx import BaseComponent


class Button(BaseComponent):
    id: str
    text: str

Component template (with HTMX)

<!-- components/ui/button.html -->
<button
  id="{{ id }}"
  hx-post="/clicked"
  hx-vals='{"button_id": "{{ id }}"}'
  hx-target="#click-result"
  hx-swap="innerHTML"
>
  {{ text }}
</button>

FastAPI app (two routes)

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

from components.ui.button import Button

app = FastAPI()


@app.get("/button", response_class=HTMLResponse)
def button() -> str:
    return (
        Button(id="save-btn", text="Click me").render()
        + '<div id="click-result"></div>'
    )


@app.post("/clicked", response_class=HTMLResponse)
def clicked(button_id: str = "unknown") -> str:
    return f"Clicked: {button_id}"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyjinhx-0.2.6.tar.gz (63.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyjinhx-0.2.6-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file pyjinhx-0.2.6.tar.gz.

File metadata

  • Download URL: pyjinhx-0.2.6.tar.gz
  • Upload date:
  • Size: 63.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pyjinhx-0.2.6.tar.gz
Algorithm Hash digest
SHA256 da7f778985a9aab9f6d9e2cd332b8fa39c29fce3a2cb7927db18c621a61659e0
MD5 c52e8ebcddaaaef80d26a88f6c48f0be
BLAKE2b-256 2c6adc2df211654b5454f8a047cf8133518b5e1cb7affe395f30dff6e9e8bb56

See more details on using hashes here.

File details

Details for the file pyjinhx-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: pyjinhx-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pyjinhx-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 78cc281a4e82d19c7d4252007d3b10948efe5d8d68a60b79a0020fba407a0a36
MD5 98f4b88fc8dd8da622cf68efb34d7ce2
BLAKE2b-256 c7430585fa573ba28ebc42a6af6c85909155c029d77f6df3d0ca765d64a97734

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page