pyjinhx 0.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,205 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyjinhx
3
+ Version: 0.2.2
4
+ Summary: UI components for Python using Pydantic and Jinja2 templates
5
+ Project-URL: Homepage, https://github.com/paulomtts/pyjinhx
6
+ Author-email: Paulo Mattos <paulomtts@outlook.com>
7
+ License: MIT
8
+ License-File: LICENSE.txt
9
+ Keywords: ,components,jinja2,pydantic,templates,ui
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Requires-Python: >=3.13
16
+ Requires-Dist: jinja2>=3.1.6
17
+ Requires-Dist: markupsafe>=3.0.3
18
+ Requires-Dist: pydantic>=2.12.5
19
+ Requires-Dist: pytest>=9.0.1
20
+ Description-Content-Type: text/markdown
21
+
22
+ # PyJinHx
23
+
24
+ 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.
25
+
26
+ - **Automatic Template Discovery**: Place templates next to component files—no manual paths
27
+ - **JavaScript Bundling**: Automatically collects and bundles `.js` files from component directories
28
+ - **Composability**: Nest components easily—works with single components, lists, and dictionaries
29
+ - **Flexible**: Use Python classes for reusable components, HTML syntax for quick page composition
30
+ - **Type Safety**: Pydantic models provide validation and IDE support
31
+
32
+ ## Installation
33
+
34
+ ```bash
35
+ pip install pyjinhx
36
+ ```
37
+
38
+ ## Example
39
+
40
+ This single example shows the full setup (Python classes + templates) and both ways to render:
41
+
42
+ - Python-side: instantiate a component class and call `.render()`.
43
+ - Template-side: render an HTML-like string with custom tags via `Renderer`.
44
+
45
+ ### Step 1: Define component classes
46
+
47
+ ```python
48
+ # components/ui/button.py
49
+ from pyjinhx import BaseComponent
50
+
51
+
52
+ class Button(BaseComponent):
53
+ id: str
54
+ text: str
55
+ variant: str = "default"
56
+ ```
57
+
58
+ ```python
59
+ # components/ui/card.py
60
+ from pyjinhx import BaseComponent
61
+ from components.ui.button import Button
62
+
63
+
64
+ class Card(BaseComponent):
65
+ id: str
66
+ title: str
67
+ action_button: Button
68
+ ```
69
+
70
+ ```python
71
+ # components/ui/page.py
72
+ from pyjinhx import BaseComponent
73
+ from components.ui.card import Card
74
+
75
+
76
+ class Page(BaseComponent):
77
+ id: str
78
+ title: str
79
+ main_card: Card
80
+ ```
81
+
82
+ ### Step 2: Create templates (auto-discovered next to the class files)
83
+
84
+ ```html
85
+ <!-- components/ui/button.html -->
86
+ <button id="{{ id }}" class="btn btn-{{ variant }}">{{ text }}</button>
87
+ ```
88
+
89
+ ```html
90
+ <!-- components/ui/card.html -->
91
+ <div id="{{ id }}" class="card">
92
+ <h2>{{ title }}</h2>
93
+ <div class="action">{{ action_button }}</div>
94
+ </div>
95
+ ```
96
+
97
+ ```html
98
+ <!-- components/ui/page.html -->
99
+ <main id="{{ id }}">
100
+ <h1>{{ title }}</h1>
101
+ {{ main_card }}
102
+ </main>
103
+ ```
104
+
105
+ ### Step 3A: Python-side rendering (`BaseComponent.render()`)
106
+
107
+ ```python
108
+ from components.ui.button import Button
109
+ from components.ui.card import Card
110
+ from components.ui.page import Page
111
+
112
+ page = Page(
113
+ id="home",
114
+ title="Welcome",
115
+ main_card=Card(
116
+ id="hero",
117
+ title="Get Started",
118
+ action_button=Button(id="cta", text="Sign up", variant="primary"),
119
+ ),
120
+ )
121
+
122
+ html = page.render()
123
+ ```
124
+
125
+ ### Step 3B: Template-side rendering (`Renderer(...).render(source)`)
126
+
127
+ ```python
128
+ from pyjinhx import Renderer
129
+
130
+ renderer = Renderer("./components", auto_id=True)
131
+
132
+ html = renderer.render(
133
+ """
134
+ <Page title="Welcome">
135
+ <Card title="Get Started">
136
+ <Button text="Sign up" variant="primary"/>
137
+ </Card>
138
+ </Page>
139
+ """
140
+ )
141
+ ```
142
+
143
+ Template-side rendering supports:
144
+
145
+ - Type safety for registered classes: if `Button(BaseComponent)` exists, its fields are validated when `<Button .../>` is instantiated.
146
+ - Generic tags: if there is no registered class, a generic `BaseComponent` is used as long as the template file can be found.
147
+
148
+ ## JavaScript & extra assets
149
+
150
+ - 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.
151
+ - Extra JS: pass `js=[...]` with file paths; missing files are ignored.
152
+ - Extra HTML files: pass `html=[...]` with file paths; they are rendered and exposed in the template context by filename stem (e.g. `extra_content.html` → `extra_content.html` wrapper). Missing files raise `FileNotFoundError`.
153
+
154
+ ## FastAPI + HTMX example
155
+
156
+ ### Component class
157
+
158
+ ```python
159
+ # components/ui/button.py
160
+ from pyjinhx import BaseComponent
161
+
162
+
163
+ class Button(BaseComponent):
164
+ id: str
165
+ text: str
166
+ ```
167
+
168
+ ### Component template (with HTMX)
169
+
170
+ ```html
171
+ <!-- components/ui/button.html -->
172
+ <button
173
+ id="{{ id }}"
174
+ hx-post="/clicked"
175
+ hx-vals='{"button_id": "{{ id }}"}'
176
+ hx-target="#click-result"
177
+ hx-swap="innerHTML"
178
+ >
179
+ {{ text }}
180
+ </button>
181
+ ```
182
+
183
+ ### FastAPI app (two routes)
184
+
185
+ ```python
186
+ from fastapi import FastAPI
187
+ from fastapi.responses import HTMLResponse
188
+
189
+ from components.ui.button import Button
190
+
191
+ app = FastAPI()
192
+
193
+
194
+ @app.get("/button", response_class=HTMLResponse)
195
+ def button() -> str:
196
+ return (
197
+ Button(id="save-btn", text="Click me").render()
198
+ + '<div id="click-result"></div>'
199
+ )
200
+
201
+
202
+ @app.post("/clicked", response_class=HTMLResponse)
203
+ def clicked(button_id: str = "unknown") -> str:
204
+ return f"Clicked: {button_id}"
205
+ ```
@@ -0,0 +1,12 @@
1
+ pyjinhx/__init__.py,sha256=A5rqiFbZmPs1Jn4839YAn4dQvCA0Sqp2xNX1uMRpTbg,284
2
+ pyjinhx/base.py,sha256=izBK3nDs0Sz2KNzMEJ1687TxftUduqSIcrRoC08p80s,6205
3
+ pyjinhx/dataclasses.py,sha256=0I-CxvF0uw3x8mGBQXyUNtBy8NEBlgAAYSfjNzW_Mn8,598
4
+ pyjinhx/finder.py,sha256=q6jBwO6MXmL__lNV9h4Ok6JROkAIjfTLwDvxGddaOo4,8370
5
+ pyjinhx/parser.py,sha256=doXdz9Nmru93sUVbT4zGOg_LdxL9SLUiz7Oxgyfl_Jo,2522
6
+ pyjinhx/registry.py,sha256=hSaf5DL0vcHBMj2ptyxh4-CizoFaFzWQXBPIho76ARs,2936
7
+ pyjinhx/renderer.py,sha256=bH2C5uVM0tyAaQJUgaHiBuobVqcNNcSYRopxtNxOO6Y,14790
8
+ pyjinhx/utils.py,sha256=PjMRM6tN7mq4LqegPaLKldqmdSPGXuzVl99hPb5up4Q,3136
9
+ pyjinhx-0.2.2.dist-info/METADATA,sha256=Xz172df41cPQi7s6U52BPE9TpZZUzhp-eU6HIHWbcto,5250
10
+ pyjinhx-0.2.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
11
+ pyjinhx-0.2.2.dist-info/licenses/LICENSE.txt,sha256=D-n1hyHpOIRpR8OPEIOPNW_PrynmPjkNVilwGo2J104,1068
12
+ pyjinhx-0.2.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Paulo Mattos
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.