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>
# components/card.py
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 pyjinhx import ReactiveComponent, MutationKey, mutates, setup

class Keys(MutationKey):
    TODOS = "todos"

class Counter(ReactiveComponent, react={Keys.TODOS}):
    remaining: int

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

@mutates(Keys.TODOS)
def toggle_all():
    db.toggle_all()

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

@app.post("/todos/toggle")
def toggle():
    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.21.1.tar.gz (343.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.21.1-py3-none-any.whl (167.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyjinhx-0.21.1.tar.gz
  • Upload date:
  • Size: 343.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.21.1.tar.gz
Algorithm Hash digest
SHA256 2811e150615127de848f056ad02c69c8eb95f8927ad416deccc4834f1347303f
MD5 c44cf9730cead289c200220b7766184f
BLAKE2b-256 b486fb3c8c91d00f88553dbcff319db32cce2a948eee936da7e076873bab3937

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyjinhx-0.21.1-py3-none-any.whl
  • Upload date:
  • Size: 167.3 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.21.1-py3-none-any.whl
Algorithm Hash digest
SHA256 199821250653d6a53fad3c7121ca1ef940ca1ecfc5c7d9b31532661621f50be7
MD5 6b954a6de19ae37f36c5b891e8f63d74
BLAKE2b-256 ba3173ec068a50340d1775ad97d941a80694ae377146135dc7d5ab288d0034d2

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