djhtmx 1.0.0__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,879 @@
1
+ Metadata-Version: 2.4
2
+ Name: djhtmx
3
+ Version: 1.0.0
4
+ Summary: Interactive UI Components for Django using HTMX
5
+ Project-URL: Homepage, https://github.com/edelvalle/djhtmx
6
+ Project-URL: Documentation, https://github.com/edelvalle/djhtmx#readme
7
+ Project-URL: Repository, https://github.com/edelvalle/djhtmx.git
8
+ Project-URL: Issues, https://github.com/edelvalle/djhtmx/issues
9
+ Project-URL: Changelog, https://github.com/edelvalle/djhtmx/blob/master/CHANGELOG.md
10
+ Author-email: Eddy Ernesto del Valle Pino <eddy@edelvalle.me>
11
+ License-Expression: MIT
12
+ License-File: LICENSE
13
+ Keywords: django,htmx,liveview,reactive,real-time,spa,websockets
14
+ Classifier: Development Status :: 5 - Production/Stable
15
+ Classifier: Environment :: Web Environment
16
+ Classifier: Framework :: Django
17
+ Classifier: Framework :: Django :: 4.1
18
+ Classifier: Framework :: Django :: 4.2
19
+ Classifier: Framework :: Django :: 5.0
20
+ Classifier: Framework :: Django :: 5.1
21
+ Classifier: Intended Audience :: Developers
22
+ Classifier: License :: OSI Approved :: MIT License
23
+ Classifier: Operating System :: OS Independent
24
+ Classifier: Programming Language :: Python
25
+ Classifier: Programming Language :: Python :: 3
26
+ Classifier: Programming Language :: Python :: 3.11
27
+ Classifier: Programming Language :: Python :: 3.12
28
+ Classifier: Programming Language :: Python :: 3.13
29
+ Classifier: Topic :: Internet :: WWW/HTTP
30
+ Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
31
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
32
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
+ Requires-Python: >=3.11
34
+ Requires-Dist: channels>=4.1.0
35
+ Requires-Dist: django>=4.1
36
+ Requires-Dist: mmh3>=5.1.0
37
+ Requires-Dist: orjson>=3.10.7
38
+ Requires-Dist: pydantic<3,>=2
39
+ Requires-Dist: redis[hiredis]>=5.0.8
40
+ Requires-Dist: uuid6>=2024.7.10
41
+ Requires-Dist: xotl-tools>=3.1.1
42
+ Provides-Extra: logfire
43
+ Requires-Dist: logfire[django]>=3.8.0; extra == 'logfire'
44
+ Provides-Extra: sentry
45
+ Requires-Dist: sentry-sdk>=2.19; extra == 'sentry'
46
+ Description-Content-Type: text/markdown
47
+
48
+ # djhtmx
49
+
50
+ [![CI](https://github.com/edelvalle/djhtmx/actions/workflows/ci.yml/badge.svg)](https://github.com/edelvalle/djhtmx/actions/workflows/ci.yml)
51
+ [![codecov](https://codecov.io/gh/edelvalle/djhtmx/branch/master/graph/badge.svg)](https://codecov.io/gh/edelvalle/djhtmx)
52
+
53
+ Interactive UI Components for Django using [htmx](https://htmx.org)
54
+
55
+ ## Install
56
+
57
+ Add `djhtmx` to your `INSTALLED_APPS` and install the Middleware as the last one
58
+ of the list:
59
+
60
+ ```python
61
+ INSTALLED_APPS = [
62
+ ...
63
+ "djhtmx",
64
+ ...
65
+ ]
66
+
67
+ MIDDLEWARE = [
68
+ ...,
69
+ "djhtmx.middleware",
70
+ ]
71
+
72
+ ```
73
+
74
+ Expose the HTTP endpoint in your `urls.py` as you wish, you can use any path you want.
75
+
76
+ ```python
77
+ from django.urls import path, include
78
+
79
+ urlpatterns = [
80
+ # ...
81
+ path("_htmx/", include("djhtmx.urls")),
82
+ # ...
83
+ ]
84
+ ```
85
+
86
+ In your base template you need to load the necessary scripts to make this work
87
+
88
+ ```html
89
+ {% load htmx %}
90
+ <!doctype html>
91
+ <html>
92
+ <head>
93
+ {% htmx-headers %}
94
+ </head>
95
+ </html>
96
+ ```
97
+
98
+ ## Getting started
99
+
100
+ This app will look for `htmx.py` files in your app and registers all components found there, but if you load any module where you have components manually when Django boots up, that also works.
101
+
102
+ ```python
103
+ from djhtmx.component import HtmxComponent
104
+
105
+
106
+ class Counter(HtmxComponent):
107
+ _template_name = "Counter.html"
108
+ counter: int = 0
109
+
110
+ def inc(self, amount: int = 1):
111
+ self.counter += amount
112
+ ```
113
+
114
+ The `inc` event handler is ready to be called from the front-end to respond to an event.
115
+
116
+ The `counter.html` would be:
117
+
118
+ ```html
119
+ {% load htmx %}
120
+ <div {% hx-tag %}>
121
+ {{ counter }}
122
+ <button {% on "inc" %}>+</button>
123
+ <button {% on "inc" amount=2 %}>+2</button>
124
+ </div>
125
+ ```
126
+
127
+ When the event is dispatched to the back-end the component state is reconstructed, the event handler called and then the full component is rendered back to the front-end.
128
+
129
+ Now use the component in any of your html templates, by passing attributes that are part of the component state:
130
+
131
+ ```html
132
+ {% load htmx %}
133
+
134
+ Counters: <br />
135
+ {% htmx "Counter" %} Counter with init value 3:<br />
136
+ {% htmx "Counter" counter=3 %}
137
+ ```
138
+
139
+ ## Doing more complicated stuff
140
+
141
+ ### Authentication
142
+
143
+ All components have a `self.user` representing the current logged in user or `None` in case the user is anonymous. If you wanna make sure your user is properly validated and enforced. You need to create a base component and annotate the right user:
144
+
145
+ ```python
146
+ from typing import Annotated
147
+ from pydantic import Field
148
+ from djhtmx.component import HtmxComponent
149
+
150
+ class BaseComponent(HtmxComponent, public=False):
151
+ user: Annotated[User, Field(exclude=True)]
152
+
153
+
154
+ class Counter(BaseComponent):
155
+ _template_name = "Counter.html"
156
+ counter: int = 0
157
+
158
+ def inc(self, amount: int = 1):
159
+ self.counter += amount
160
+ ```
161
+
162
+ ### Non-public components
163
+
164
+ These are components that can't be instantiated using `{% htmx "ComponentName" %}` because they are used to create some abstraction and reuse code.
165
+
166
+ Pass `public=False` in their declaration
167
+
168
+ ```python
169
+ class BaseComponent(HtmxComponent, public=False):
170
+ ...
171
+ ```
172
+
173
+ ## Component nesting
174
+
175
+ Components can contain components inside to decompose the behavior in more granular and specialized parts, for this you don't have to do anything but to a component inside the template of other component....
176
+
177
+ ```python
178
+ class Items(HtmxComponent):
179
+ _template_name = "Items.html"
180
+
181
+ def items(self):
182
+ return Item.objects.all()
183
+
184
+ class ItemEntry(HtmxComponent):
185
+ ...
186
+ item: Item
187
+ is_open: bool = False
188
+ ...
189
+ ```
190
+
191
+ `Items.html`:
192
+
193
+ ```html
194
+ {% load htmx %}
195
+
196
+ <ul {% hx-tag %}>
197
+ {% for item in items %}
198
+ {% htmx "ItemEntry" item=item %}
199
+ {% endfor %}
200
+ </ul>
201
+ ```
202
+
203
+ In this case every time there is a render of the parent component all children components will also be re-rendered.
204
+
205
+ How can you preserve the state in the child components if there were some of them that were already had `is_open = True`? The state that is not passed directly during instantiation to the component is retrieved from the session, but the component needs to have consistent id. To do this you have to pass an `id` to the component.
206
+
207
+ `Items.html`:
208
+
209
+ ```html
210
+ {% load htmx %}
211
+
212
+ <ul {% hx-tag %}>
213
+ {% for item in items %}
214
+ {% htmx "ItemEntry" id="item-"|add:item.id item=item %}
215
+ {% endfor %}
216
+ </ul>
217
+ ```
218
+
219
+ ## Lazy lading
220
+
221
+ If you want some component to load lazily, you pass `lazy=True` where it is being instantiated.
222
+
223
+
224
+ `Items.html`:
225
+
226
+ ```html
227
+ {% load htmx %}
228
+
229
+ <ul {% hx-tag %}>
230
+ {% for item in items %}
231
+ {% htmx "ItemEntry" id="item-"|add:item.id item=item lazy=True %}
232
+ {% endfor %}
233
+ </ul>
234
+ ```
235
+
236
+ This makes the component to be initialized, but instead of rendering the template in `_template_name` the template defined in `_template_name_lazy` will be rendered (you can override this). When the component arrives to the front-end it will trigger an event to render it self.
237
+
238
+
239
+ ## Implicit parameters
240
+
241
+ When sending an event to the back-end sometimes you can pass the parameters explicitly to the event handler, and sometimes these are inputs the user is typing stuff on. The value of those inputs are passed implicitly if they nave a `name="..."` attribute.
242
+
243
+ ```python
244
+ class Component(HtmxComponent):
245
+ ...
246
+
247
+ def create(self, name: str, is_active: bool = False):
248
+ Item.objects.create(name=name, is_active=is_active)
249
+
250
+ ```
251
+
252
+ ```html
253
+ {% load htmx %}
254
+
255
+ <form {% hx-tag %} {% on "submit" "create" %}>
256
+ <input type="text" name="name">
257
+ <input type="checkbox" name="is_active">
258
+ <button type="submit">Create!</button>
259
+ </form>
260
+ ```
261
+
262
+ The parameters of any event handler are always converted by pydantic to the annotated types. It's suggested to properly annotate the event handler parameter with the more restrictive types you can.
263
+
264
+ ### Data structures in implicit parameters
265
+
266
+ Suppose that you have a multiple choice list and you want to select multiple options, you can do this by suffixing the name with `[]` as in `choices[]`:
267
+
268
+ ```python
269
+ class DeleteSelection(HtmxComponent):
270
+
271
+ @property
272
+ def items(self):
273
+ return self.filter(owner=self.user)
274
+
275
+ def delete(self, selected: list[UUID] | None = None):
276
+ if selected:
277
+ self.items.filter(id__in=selected).delete()
278
+ ```
279
+
280
+ ```html
281
+ {% load htmx %}
282
+
283
+ <form {% hx-tag %} {% on "submit" "delete" %}>
284
+ <h1>Select items to be deleted</h1>
285
+ {% for item in items %}
286
+ <p>
287
+ <input
288
+ type="checkbox"
289
+ name="selected[]"
290
+ value="{{ item.id }}"
291
+ id="checkbox-{{ item.id }}"
292
+ />
293
+ <label for="checkbox-{{ item.id }}">{{ item.name}}</label>
294
+
295
+ </p>
296
+ {% endfor %}
297
+ <p><button type="submit">Delete selected</button></p>
298
+ </form>
299
+
300
+ ```
301
+
302
+
303
+ ## Commands
304
+
305
+ Each event handler in a component can yield commands for the library to execute. These are useful for skipping the default component render, redirecting the user, remove the component from the front-end, updating other components, and rendering components with custom context.
306
+
307
+ ### Redirects
308
+
309
+ Wanna redirect the user to some object url:
310
+ - If you have the url directly you can `yield Redirect(url)`.
311
+
312
+ - If you want Django to resolve the url automatically use: `yield Redirect.to(obj, *args, **kwargs)` as you would use `django.shortcuts.resolve_url`.
313
+
314
+ ```python
315
+ from djhtmx.component import HtmxComponent, Redirect
316
+
317
+
318
+ class Component(HtmxComponent):
319
+ ...
320
+
321
+ def create(self, name: str):
322
+ item = Item.objects.create(name=name)
323
+ yield Redirect.to(item)
324
+ ```
325
+
326
+ If you want to open the url in a new url use the `yield Open...` command with similar syntax to `Redirect`.
327
+
328
+ ### Remove the current component from the interface
329
+
330
+ Sometimes you want to remove the component when it responds to an event, for that you need to `yield Destroy(component_id: str)`. You can also use this to remove any other component if you know their id.
331
+
332
+ ```python
333
+ from djhtmx.component import HtmxComponent, Destroy
334
+
335
+
336
+ class Notification(HtmxComponent):
337
+ ...
338
+
339
+ def close(self):
340
+ yield Destroy(self.id)
341
+ ```
342
+
343
+ ### Skip renders
344
+
345
+ Sometimes when reacting to a front-end event is handy to skip the default render of the current component, to achieve this do:
346
+
347
+ ```python
348
+ from djhtmx.component import HtmxComponent, Redirect
349
+
350
+
351
+ class Component(HtmxComponent):
352
+ ...
353
+
354
+ def do_something(self):
355
+ ...
356
+ yield SkipRender(self)
357
+ ```
358
+
359
+ ### Partial Rendering
360
+
361
+ Sometimes you don't want to do a full component render, but a partial one. Specially if the user if typing somewhere to filter items and you don't wanna interfere with the user typing or focus. Here is the technique to do that:
362
+
363
+ ```python
364
+ from djhtmx.component import HtmxComponent, Render
365
+
366
+ class SmartFilter(HtmxComponent):
367
+ _template_name = "SmartFilter.html"
368
+ query: str = ""
369
+
370
+ @property
371
+ def items(self):
372
+ items = Item.objects.all()
373
+ if self.query:
374
+ items = items.filter(name__icontains=self.query)
375
+ return items
376
+
377
+ def filter(self, query: str):
378
+ self.query = query.trim()
379
+ yield Render(self, template="SmartFilter_list.html")
380
+ ```
381
+
382
+ `SmartFilter.html`:
383
+
384
+ ```html
385
+ {% load htmx %}
386
+
387
+ <div {% hx-tag %}>
388
+ <input type="text" name="query" value="{{ query }}">
389
+ {% include "SmartFilter_list.html" %}
390
+ </div>
391
+ ```
392
+
393
+ `SmartFilter_list.html`:
394
+
395
+ ```html
396
+ <ul {% oob "list" %}>
397
+ {% for item in items %}
398
+ <li><a href="{{ item.get_absolute_url }}">{{ item }}</a></li>
399
+ {% empty %}
400
+ <li>Nothing found!</li>
401
+ {% endfor %}
402
+ </ul>
403
+ ```
404
+
405
+ - Split the component in multiple templates, the main one and the partial ones.
406
+ - For readability prefix the name of the partials with the name of the parent.
407
+ - The partials need a single root HTML Element with an id and the `{% oob %}` tag next to it.
408
+ - When you wanna do the partial render you have to `yield Render(self, template=...)` with the name of the partial template, this will automatically skip the default full render and render the component with that partial template.
409
+
410
+ ### Rendering with Custom Context
411
+
412
+ Sometimes you need to render a component with custom context data that differs from the component's state. The `Render` command supports an optional `context` parameter that allows you to override the component's context:
413
+
414
+ ```python
415
+ from djhtmx.component import HtmxComponent, Render
416
+
417
+ class DataVisualization(HtmxComponent):
418
+ _template_name = "DataVisualization.html"
419
+
420
+ def show_filtered_data(self, filter_type: str):
421
+ # Get some custom data that's not part of component state
422
+ custom_data = self.get_filtered_data(filter_type)
423
+
424
+ # Render with custom context
425
+ yield Render(
426
+ self,
427
+ template="DataVisualization_filtered.html",
428
+ context={
429
+ "filtered_data": custom_data,
430
+ "filter_applied": filter_type,
431
+ "timestamp": datetime.now()
432
+ }
433
+ )
434
+ ```
435
+
436
+ When using custom context:
437
+ - The provided context overrides the component's default context
438
+ - Essential HTMX variables (`htmx_repo`, `hx_oob`, `this`) are preserved
439
+ - The component's state remains unchanged - only the rendering context is modified
440
+ - This is particularly useful for displaying computed data, temporary states, or external data that shouldn't be part of the component's persistent state
441
+
442
+ ## Query Parameters & State
443
+
444
+ Coming back to the previous example let's say that we want to persist the state of the `query` in the URL, so in case the user refreshes the page or shares the link the state of the component is partially restored. For do the following:
445
+
446
+ ```python
447
+ from typing import Annotated
448
+ from djhtmx.component import HtmxComponent
449
+ from djhtmx.query import Query
450
+
451
+
452
+ class SmartFilter(HtmxComponent):
453
+ ...
454
+ query: Annotated[str, Query("query")] = ""
455
+ ...
456
+ ```
457
+
458
+ Annotating with Query causes that if the state of the query is not explicitly passed to the component during instantiation it is taken from the query string of the current URL.
459
+
460
+ There can be multiple components subscribed to the same query parameter or to individual ones.
461
+
462
+ If you want now you can split this component in two, each with their own template:
463
+
464
+
465
+ ```python
466
+ from typing import Annotated
467
+ from djhtmx.component import HtmxComponent, SkipRender
468
+ from djhtmx.query import Query
469
+
470
+ class SmartFilter(HtmxComponent):
471
+ _template_name = "SmartFilter.html"
472
+ query: Annotated[str, Query("query")] = ""
473
+
474
+ def filter(self, query: str):
475
+ self.query = query.trim()
476
+ yield SkipRender(self)
477
+
478
+ class SmartList(HtmxComponent):
479
+ _template_name = "SmartList.html"
480
+ query: Annotated[str, Query("query")] = ""
481
+
482
+ @property
483
+ def items(self):
484
+ items = Item.objects.all()
485
+ if self.query:
486
+ items = items.filter(name__icontains=self.query)
487
+ return items
488
+ ```
489
+
490
+ Instantiate next to each other:
491
+
492
+
493
+ ```html
494
+ <div>
495
+ ...
496
+ {% htmx "SmartFilter" %}
497
+ {% htmx "SmartList" %}
498
+ ...
499
+ </div>
500
+ ```
501
+
502
+ When the filter mutates the `query`, the URL is updated and the `SmartList` is awaken because the both point to the same query parameter, and will be re-rendered.
503
+
504
+ ## Signals
505
+
506
+ Sometimes you modify a model and you want not just the current component to react to this, but also trigger re-renders of other components that are not directly related to the current one. For this signals are very convenient. These are strings that represent topics you can subscribe a component to and make sure it is rendered in case any of the topics it subscribed to is triggered.
507
+
508
+ Signal formats:
509
+ - `app_label.modelname`: Some mutation happened to a model instance of this kind
510
+ - `app_label.modelname.instance_pk`: Some mutation happened to this precise instance of model
511
+ - `app_label.modelname.instance_pk.created`: This instance was created
512
+ - `app_label.modelname.instance_pk.updated`: This instance was updated
513
+ - `app_label.modelname.instance_pk.deleted`: This instance was deleted
514
+
515
+ When an instance is modified the mode specific and not so specific signals are triggered.
516
+ Together with them some other signals to related models are triggered.
517
+
518
+ Example: if we have a Todo list app with the models:
519
+
520
+
521
+ ```python
522
+ class TodoList(Model):
523
+ ...
524
+
525
+ class Item(Model):
526
+ todo_list = ForeignKey(TodoList, related_name="items")
527
+ ```
528
+
529
+ And from the list with id `932` you take a item with id `123` and update it all this signals will be triggered:
530
+
531
+ - `todoapp.item`
532
+ - `todoapp.item.123`
533
+ - `todoapp.item.123.updated`
534
+ - `todoapp.todolist.932.items`
535
+ - `todoapp.todolist.932.items.updated`
536
+
537
+
538
+ ### How to subscribe to signals
539
+
540
+ Let's say you wanna count how many items there are in certain Todo list, but your component does not receive an update when the list is updated because it is out of it. You can do this.
541
+
542
+ ```python
543
+ from djhtmx.component import HtmxComponent
544
+
545
+ class ItemCounter(HtmxComponent):
546
+ todo_list: TodoList
547
+
548
+ def subscriptions(self):
549
+ return {
550
+ f"todoapp.todolist.{self.todo_list.id}.items.deleted",
551
+ f"todoapp.todolist.{self.todo_list.id}.items.created",
552
+ }
553
+
554
+ def count(self):
555
+ return self.todo_list.items.count()
556
+ ```
557
+
558
+ This will make this component re-render every time an item is added or removed from the list `todo_list`.
559
+
560
+ ## Dispatching Events between components
561
+
562
+ Sometimes is handy to notify components in the same session that something changed and they need to perform the corresponding update and `Query()` nor Signals are very convenient for this. In this case you can `Emit` events and listen to them.
563
+
564
+ Find here an implementation of `SmartFilter` and `SmartItem` using this mechanism:
565
+
566
+ ```python
567
+ from dataclasses import dataclass
568
+ from djhtmx.component import HtmxComponent, SkipRender, Emit
569
+
570
+
571
+ @dataclass(slots=True)
572
+ class QueryChanged:
573
+ query: str
574
+
575
+
576
+ class SmartFilter(HtmxComponent):
577
+ _template_name = "SmartFilter.html"
578
+ query: str = ""
579
+
580
+ def filter(self, query: str):
581
+ self.query = query.trim()
582
+ yield Emit(QueryChanged(query))
583
+ yield SkipRender(self)
584
+
585
+ class SmartList(HtmxComponent):
586
+ _template_name = "SmartList.html"
587
+ query: str = ""
588
+
589
+ def _handle_event(self, event: QueryChanged):
590
+ self.query = event.query
591
+
592
+ @property
593
+ def items(self):
594
+ items = Item.objects.all()
595
+ if self.query:
596
+ items = items.filter(name__icontains=self.query)
597
+ return items
598
+ ```
599
+
600
+ The library will look in all components if they define `_handle_event(event: ...)` and based on the annotation of `event` subscribe them to those events. This annotation can be a single type or a `Union` with multiple even types.
601
+
602
+ ## Inserting a component somewhere
603
+
604
+ Let's say that we are making the TODO list app and we want that when a new item is added to the list there is not a full re-render of the whole list, just that the Component handling a single Item is added to the list.
605
+
606
+ ```python
607
+ from djhtmx.component import HtmxComponent, SkipRender, BuildAndRender
608
+
609
+ class TodoListComponent(HtmxComponent):
610
+ _template_name = "TodoListComponent.html"
611
+ todo_list: TodoList
612
+
613
+ def create(self, name: str):
614
+ item = self.todo_list.items.create(name=name)
615
+ yield BuildAndRender.prepend(
616
+ f"#{self.id} .list",
617
+ ItemComponent,
618
+ id=f"item-{item.id}",
619
+ item=item,
620
+ )
621
+ yield SkipRender(self)
622
+
623
+ class ItemComponent(HtmxComponent):
624
+ ...
625
+ item: Item
626
+ ...
627
+ ```
628
+
629
+ `TodoListComponent.html`:
630
+
631
+ ```html
632
+ {% load htmx %}
633
+ <div {% hx-tag %}>
634
+ <form {% on "submit" "create" %}>
635
+ <input type="text" name="name">
636
+ </form>
637
+ <ul class="list">
638
+ {% for item in items %}
639
+ {% htmx "ItemComponent" id="item-"|add:item.id item=item %}
640
+ {% endfor %}
641
+ </ul>
642
+ </div>
643
+ ```
644
+
645
+ Use the `BuildAndRender.<helper>(target: str, ...)` to send a component to be inserted somewhere or updated.
646
+
647
+ ### Cascade Deletion
648
+
649
+ You can establish parent-child relationships so that when a parent component is destroyed, all its children are automatically destroyed recursively. This prevents memory leaks in complex component hierarchies.
650
+
651
+ #### Using BuildAndRender with parent_id
652
+
653
+ ```python
654
+ class TodoListComponent(HtmxComponent):
655
+ def create(self, name: str):
656
+ item = self.todo_list.items.create(name=name)
657
+ # Child component that will be automatically destroyed when parent is destroyed
658
+ yield BuildAndRender.append(
659
+ "#todo-items",
660
+ ItemComponent,
661
+ parent_id=self.id, # Establishes parent-child relationship
662
+ id=f"item-{item.id}",
663
+ item=item
664
+ )
665
+
666
+ class Dashboard(HtmxComponent):
667
+ def show_modal(self):
668
+ # Modal becomes child of dashboard - destroyed when dashboard is destroyed
669
+ yield BuildAndRender.prepend("body", SettingsModal, parent_id=self.id)
670
+ ```
671
+
672
+ #### Template Tag Automatic Tracking
673
+
674
+ When you use `{% htmx "ComponentName" %}` inside another component's template, parent-child relationships are automatically established:
675
+
676
+ ```html
677
+ <!-- In TodoList.html template -->
678
+ {% load htmx %}
679
+ <div {% hx-tag %}>
680
+ {% for item in items %}
681
+ <!-- Each TodoItem automatically becomes a child of this TodoList -->
682
+ {% htmx "ItemComponent" id="item-"|add:item.id item=item %}
683
+ {% endfor %}
684
+ </div>
685
+ ```
686
+
687
+ When the parent TodoList is destroyed, all child ItemComponent instances are automatically cleaned up.
688
+
689
+ #### Updating Components
690
+
691
+ Use `BuildAndRender.update()` to update existing components (preserves existing parent-child relationships):
692
+
693
+ ```python
694
+ # Update existing component without changing relationships
695
+ yield BuildAndRender.update(SidebarWidget, data=sidebar_data)
696
+ ```
697
+
698
+
699
+ ## Focusing an item after render
700
+
701
+ Let's say we want to put the focus in an input that inside the new ItemComponent rendered, for this use `yield Focus(target)`
702
+
703
+ ```python
704
+ from djhtmx.component import HtmxComponent, SkipRender, BuildAndRender, Focus
705
+
706
+ class TodoListComponent(HtmxComponent):
707
+ _template_name = "TodoListComponent.html"
708
+ todo_list: TodoList
709
+
710
+ def create(self, name: str):
711
+ item = self.todo_list.items.create(name=name)
712
+ item_id = f"item-{item.id}"
713
+ yield BuildAndRender.prepend(
714
+ f"{self.id} .list",
715
+ ItemComponent,
716
+ id=item_id,
717
+ item=item,
718
+ )
719
+ yield Focus(f"#{item_id} input")
720
+ yield SkipRender(self)
721
+ ```
722
+
723
+ ## Sending Events to the DOM
724
+
725
+ Suppose you have a rich JavaScript library (graphs, maps, or anything...) in the front-end and you want to communicate something to it because it is subscribed to some dome event. For that you can use `yield DispatchDOMEvent(target, event, detail, ....)`
726
+
727
+
728
+ ```python
729
+ from djhtmx.component import HtmxComponent, DispatchDOMEvent
730
+
731
+ class TodoListComponent(HtmxComponent):
732
+ _template_name = "TodoListComponent.html"
733
+ todo_list: TodoList
734
+
735
+ def create(self, name: str):
736
+ item = self.todo_list.items.create(name=name)
737
+ yield DispatchDOMEvent(
738
+ "#leaflet-map",
739
+ "new-item",
740
+ {"id": item.id, "name": item.name, "geojson": item.geojson}
741
+ )
742
+ ```
743
+
744
+ This will trigger that event in the front-end when the request arrives allowing rich JavaScript components to react accordingly without full re-render.
745
+
746
+ ## Template Tags you should know about
747
+
748
+ - `{% htmx-headers %}`: put it inside your `<header></header>` to load the right scripts and configuration.
749
+
750
+ ```html
751
+ <header>
752
+ {% htmx-headers %}
753
+ </header>
754
+ ```
755
+
756
+ - `{% htmx <ComponentName: str> **kwargs %}`: instantiates and inserts the result of rendering that component with those initialization parameters.
757
+
758
+ ```html
759
+ <div>
760
+ {% htmx 'Button' document=document name='Save Document' is_primary=True %}
761
+ </div>
762
+ ```
763
+
764
+
765
+ - `{% hx-tag %}`: goes in the root HTML Element of a component template, it sets the component `id` and some other basic configuration details of the component.
766
+
767
+ ```html
768
+ <button {% hx-tag %}>
769
+ ...
770
+ </button>
771
+ ```
772
+
773
+
774
+ - `{% oob <LocalId: str> %}`: goes in the root HTML Element of an element that will used for partial render (swapped Out Of Band). It sets the id of the element to a concatenation of the current component id and whatever you pass to it, and sets the right [hx-swap-oob](https://htmx.org/attributes/hx-swap-oob/) strategy.
775
+
776
+ ```html
777
+ <div {% oob "dropdown" %} class="dropdown">
778
+ ...
779
+ </div>
780
+ ```
781
+
782
+ - `{% on <EventName: str> <EventHandler: str> **kwargs %}` binds the event handler using [hx-trigger](https://htmx.org/attributes/hx-trigger/) to an event handler in the component with certain explicit parameters. Implicit parameters are passed from anything that has a name attribute defined inside the component.
783
+
784
+ ```html
785
+ <button {% on "click" "save" %}>
786
+ ...
787
+ </button>
788
+ ```
789
+
790
+ - `{% class <ClassName: str>: <BooleanExpr: bool>[, ...] %}` used inside of any html tag to set the class attribute, activating certain classes when corresponding boolean expression is `True`.
791
+
792
+
793
+ ```html
794
+ <button {% class "btn": True, "btn-primary": is_primary %}>
795
+ ...
796
+ </button>
797
+ ```
798
+
799
+ ## Testing
800
+
801
+ This library provides the class `djhtmx.testing.Htmx` which implements a very basic a dumb runtime for testing components. How to use:
802
+
803
+
804
+ ```python
805
+ from django.test import Client, TestCase
806
+ from djhtmx.testing import Htmx
807
+
808
+ from .models import Item
809
+
810
+ class TestNormalRendering(TestCase):
811
+ def setUp(self):
812
+ Item.objects.create(text="First task")
813
+ Item.objects.create(text="Second task")
814
+ self.htmx = Htmx(Client())
815
+
816
+ def test_todo_app(self):
817
+ self.htmx.navigate_to("/todo")
818
+
819
+ [a, b] = self.htmx.select('[hx-name="TodoItem"] label')
820
+ self.assertEqual(a.text_content(), "First task")
821
+ self.assertEqual(b.text_content(), "Second task")
822
+
823
+ [count] = self.htmx.select(".todo-count")
824
+ self.assertEqual(count.text_content(), "2 items left")
825
+
826
+ # Add new item
827
+ self.htmx.type("input.new-todo", "3rd task")
828
+ self.htmx.trigger("input.new-todo")
829
+
830
+ [count] = self.htmx.select(".todo-count")
831
+ self.assertEqual(count.text_content(), "3 items left")
832
+
833
+ [a, b, c] = self.htmx.select('[hx-name="TodoItem"] label')
834
+ self.assertEqual(a.text_content(), "First task")
835
+ self.assertEqual(b.text_content(), "Second task")
836
+ self.assertEqual(c.text_content(), "3rd task")
837
+ ```
838
+
839
+ ### API
840
+
841
+ `Htmx(client: Client)`: pass a Django test client, that can be authenticated if that's required.
842
+
843
+ `htmx.navigate_to(url, *args, **kwargs)`: This is used to navigate to some url. It is a wrapper of `Client.get` that will retrieve the page and parse the HTML into `htmx.dom: lxml.html.HtmlElement` and create a component repository in `htmx.repo`.
844
+
845
+ #### Look-ups
846
+
847
+ `htmx.select(css_selector: str) -> list[lxml.html.HtmlElement]`: Pass some CSS selector here to retrieve nodes from the DOM, so you can modify them or perform assertions over them.
848
+
849
+ `htmx.find_by_text(text: str) -> lxml.html.HtmlElement`: Returns the first element that contains certain text.
850
+
851
+ `htmx.get_component_by_type(component_type: type[THtmxComponent]) -> THtmxComponent`: Retrieves the only instance rendered of that component type in the current page. If there is more than one instance this fails.
852
+
853
+ `htmx.get_components_by_type(component_type: type[THtmxComponent]) -> list[THtmxComponent]`: Retrieves all instances of this component type in the current page.
854
+
855
+ `htmx.get_component_by_id(component_id: str) -> THtmxComponent`: Retrieves a component by its id from the current page.
856
+
857
+ #### Interactions
858
+
859
+ `htmx.type(selector: str | html.HtmlElement, text: str, clear=False)`: This simulates typing in an input or text area. If `clear=True` it clears it replaces the current text in it.
860
+
861
+ `htmx.trigger(selector: str | html.HtmlElement)`: This triggers whatever event is bound in the selected element and returns after all side effects had been processed.
862
+
863
+ ```python
864
+ self.htmx.type("input.new-todo", "3rd task")
865
+ self.htmx.trigger("input.new-todo")
866
+ ```
867
+
868
+ `htmx.send(method: Callable[P, Any], *args: P.args, **kwargs: P.kwargs)`: This sends the runtime to execute that a bound method of a HtmxComponent and returns after all side effects had been processed. Use as in:
869
+
870
+ ```python
871
+ todo_list = htmx.get_component_by_type(TodoList)
872
+ htmx.send(todo_list.new_item, text="New todo item")
873
+ ```
874
+
875
+ `htmx.dispatch_event(self, component_id: str, event_handler: str, kwargs: dict[str, Any])`: Similar to `htmx.send`, but you don't need the instance, you just need to know its id.
876
+
877
+ ```python
878
+ htmx.dispatch_event("#todo-list", "new_item", {"text": "New todo item"})
879
+ ```