Skip to main content

UI components for Python using Pydantic and Jinja2 templates

Project description

PyJinHx

Pydantic Jinja HTMX

Type-safe UI components for Python web apps. A component is a Pydantic model plus a Jinja template sitting next to it — nest them with PascalCase tags, and co-located JS/CSS is collected automatically at render.

pip install pyjinhx

Example

A Card that renders a Button — the tag's attributes become validated Pydantic fields:

# components/button.py
from pyjinhx import BaseComponent

class Button(BaseComponent):
    id: str
    text: str
    variant: str = "default"
<!-- components/button.html -->
<button id="{{ id }}" class="btn btn-{{ variant }}">{{ text }}</button>
<!-- components/card.html -->
<div id="{{ id }}" class="card">
  <h2>{{ title }}</h2>
  <Button id="cta" text="{{ button_text }}" variant="primary"/>
</div>
from pyjinhx import BaseComponent, Renderer

class Card(BaseComponent):
    id: str
    title: str
    button_text: str = "Sign up"

Renderer.set_default_environment("./components")
html = Card(id="hero", title="Get Started").render()

Drop a button.css or card.js next to the component and it's included once, automatically.

Reactivity (HTMX)

Components declare what state they depend on. Return one component from a mutation route — every other mounted region that reacts to the same keys updates via out-of-band swaps, no manual wiring:

from typing import ClassVar
from pyjinhx import ReactiveComponent, MutationKey, setup

class Keys(MutationKey):
    TODOS = "todos"

class Counter(ReactiveComponent):
    remaining: int
    reacts_to: ClassVar[set[str]] = {Keys.TODOS}

    @classmethod
    def load(cls) -> "Counter":
        return cls(remaining=db.remaining())

setup(app)  # FastAPI: lifespan + middleware, done

@app.post("/todos/toggle")
def toggle():
    db.toggle_all()
    return Counter.render()  # other regions reacting to TODOS update too

Learn more

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.8.0.tar.gz (263.5 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.8.0-py3-none-any.whl (125.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyjinhx-0.8.0.tar.gz
Algorithm Hash digest
SHA256 35492d2625029bb9b6b5e7b412415b7e4c4a053c2e49c3b4897e89bc22937fe7
MD5 140d27112e37ad19411e523953a8663a
BLAKE2b-256 f625d0e989d5f8ed1542902364b156d3128bcdc1d03b01e09a2d226bc34e19d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyjinhx-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 125.7 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.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c00e7b5ea257a20d99c11477f410765884b60ceb117dbedf2345110525b3587
MD5 79d8e0d6ba041fe6fd9e467805b0ee8a
BLAKE2b-256 d2505ec13e14e7bd7eb2c02d4e6b641c9c58b0c345bc18a320c8b3d39f7c5ece

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