@@ -27,65 +27,53 @@ interfaces in pure Python.
27
27
src="https://mybinder.org/badge_logo.svg"/>
28
28
</a >
29
29
30
- # Install Django IDOM
30
+ # Quick Example
31
31
32
- ``` bash
33
- pip install django-idom
34
- ```
32
+ ## ` example_app/components.py `
35
33
36
- # Django Integration
34
+ This is where you'll define your [ IDOM] ( https://github.com/idom-team/idom ) components. Ultimately though, you should
35
+ feel free to organize your component modules you wish. Any components created will ultimately be referenced
36
+ by Python dotted path in ` your-template.html ` .
37
37
38
- To integrate IDOM into your application you'll need to modify or add the following files to ` your_project ` :
38
+ ``` python
39
+ import idom
39
40
40
- ```
41
- your_project/
42
- ├── __init__.py
43
- ├── asgi.py
44
- ├── settings.py
45
- ├── urls.py
46
- └── example_app/
47
- ├── __init__.py
48
- ├── components.py
49
- ├── templates/
50
- │ └── your-template.html
51
- └── urls.py
41
+ @idom.component
42
+ def Hello (websocket , greeting_recipient ): # Names are CamelCase by ReactJS convention
43
+ return idom.html.header(f " Hello { greeting_recipient} ! " )
52
44
```
53
45
54
- ## ` asgi.py `
46
+ ## ` example_app/templates/your-template.html `
55
47
56
- Follow the [ ` channels ` ] ( https://channels.readthedocs.io/en/stable/ )
57
- [ installation guide] ( https://channels.readthedocs.io/en/stable/installation.html ) in
58
- order to create ASGI websockets within Django. Then, we will add a path for IDOM's
59
- websocket consumer using ` IDOM_WEBSOCKET_PATH ` .
48
+ In your templates, you may add IDOM components into your HTML by using the ` idom_component `
49
+ template tag. This tag requires the dotted path to the component function. Additonally, you can
50
+ pass in keyworded arguments into your component function.
60
51
61
- _ Note: If you wish to change the route where this websocket is served from, see the
62
- available [ settings] ( #settingspy ) ._
52
+ In context this will look a bit like the following...
63
53
64
- ``` python
54
+ ``` jinja
55
+ {% load idom %}
65
56
66
- import os
57
+ <!DOCTYPE html>
58
+ <html>
59
+ <body>
60
+ ...
61
+ {% idom_component "my_django_project.example_app.components.Hello" greeting_recipient="World" %}
62
+ </body>
63
+ </html>
64
+ ```
67
65
68
- from django.core.asgi import get_asgi_application
66
+ # Installation
69
67
70
- from django_idom import IDOM_WEBSOCKET_PATH
68
+ Install ` django-idom ` via pip.
71
69
72
- os.environ.setdefault(" DJANGO_SETTINGS_MODULE" , " test_app.settings" )
70
+ ``` bash
71
+ pip install django-idom
72
+ ```
73
73
74
- # Fetch ASGI application before importing dependencies that require ORM models.
75
- http_asgi_app = get_asgi_application()
74
+ ---
76
75
77
- from channels.auth import AuthMiddlewareStack
78
- from channels.routing import ProtocolTypeRouter, URLRouter
79
-
80
- application = ProtocolTypeRouter(
81
- {
82
- " http" : http_asgi_app,
83
- " websocket" : SessionMiddlewareStack(
84
- AuthMiddlewareStack(URLRouter([IDOM_WEBSOCKET_PATH ]))
85
- ),
86
- }
87
- )
88
- ```
76
+ You'll also need to modify a few files in your Django project...
89
77
90
78
## ` settings.py `
91
79
@@ -100,110 +88,70 @@ INSTALLED_APPS = [
100
88
]
101
89
```
102
90
103
- You may configure additional options as well:
91
+ You may configure additional options as well...
104
92
105
93
``` python
106
- # the base URL for all IDOM-releated resources
107
- IDOM_BASE_URL : str = " _idom/"
108
-
109
- # Set cache size limit for loading JS files for IDOM.
110
- # Only applies when not using Django's caching framework (see below).
111
- IDOM_WEB_MODULE_LRU_CACHE_SIZE : int | None = None
112
-
113
- # Maximum seconds between two reconnection attempts that would cause the client give up.
114
- # 0 will disable reconnection.
115
- IDOM_WS_MAX_RECONNECT_DELAY : int = 604800
116
-
117
94
# Configure a cache for loading JS files
118
95
CACHES = {
119
96
# Configure a cache for loading JS files for IDOM
120
97
" idom_web_modules" : {" BACKEND" : ... },
121
98
# If the above cache is not configured, then we'll use the "default" instead
122
99
" default" : {" BACKEND" : ... },
123
100
}
101
+
102
+ # If your project has no caching configured, django-idom will use an in-memory
103
+ # LRU cache for caching JavaScript.
104
+ IDOM_WEB_MODULE_LRU_CACHE_SIZE : int | None = None
105
+
106
+ # Maximum seconds between two reconnection attempts that would cause the client give up.
107
+ # 0 will disable reconnection.
108
+ IDOM_WS_MAX_RECONNECT_DELAY : int = 604800
109
+
110
+ # The URL for IDOM to serve its Websockets
111
+ IDOM_WEBSOCKET_URL : str = " idom/"
124
112
```
125
113
126
114
## ` urls.py `
127
115
128
- You'll need to include IDOM's static web modules path using ` IDOM_WEB_MODULES_PATH ` .
129
- Similarly to the ` IDOM_WEBSOCKET_PATH ` . If you wish to change the route where this
130
- websocket is served from, see the available [ settings] ( #settings.py ) .
116
+ Add Django-IDOM http URLs to your ` urlpatterns ` .
131
117
132
118
``` python
133
- from django_idom import IDOM_WEB_MODULES_PATH
134
-
135
119
urlpatterns = [
136
- IDOM_WEB_MODULES_PATH ,
120
+ path( " idom/ " , include( " django_idom.http.urls " )) ,
137
121
...
138
122
]
139
123
```
140
124
141
- ## ` example_app/components.py `
142
-
143
- This is where, by a convention similar to that of
144
- [ ` views.py ` ] ( https://docs.djangoproject.com/en/3.2/topics/http/views/ ) , you'll define
145
- your [ IDOM] ( https://github.com/idom-team/idom ) components. Ultimately though, you should
146
- feel free to organize your component modules you wish. The components created here will
147
- ultimately be referenced by name in ` your-template.html ` . ` your-template.html ` .
148
-
149
- ``` python
150
- import idom
151
-
152
- @idom.component
153
- def Hello (websocket , greeting_recipient ): # component names are camelcase by convention
154
- return idom.html.header(f " Hello { greeting_recipient} ! " )
155
- ```
156
-
157
- ## ` example_app/templates/your-template.html `
158
-
159
- In your templates, you may inject a view of an IDOM component into your templated HTML
160
- by using the ` idom_component ` template tag. This tag which requires the name of a component
161
- to render (of the form ` module_name.ComponentName ` ) and keyword arguments you'd like to
162
- pass it from the template.
163
-
164
- ``` python
165
- idom_component module_name.ComponentName param_1= " something" param_2= " something-else"
166
- ```
125
+ ## ` asgi.py `
167
126
168
- In context this will look a bit like the following...
127
+ If you do not have an ` asgi.py ` , first follow the [ ` channels ` installation guide] ( https://channels.readthedocs.io/en/stable/installation.html ) in
128
+ order to create websockets within Django.
169
129
170
- ``` jinja
171
- {% load idom %}
130
+ We will add IDOM's websocket consumer path using ` IDOM_WEBSOCKET_PATH ` .
172
131
173
- <!DOCTYPE html>
174
- <html>
175
- <body>
176
- ...
177
- {% idom_component "your_project.example_app.components.Hello" greeting_recipient="World" %}
178
- </body>
179
- </html>
180
- ```
181
-
182
- ## ` example_app/views.py `
183
-
184
- You can then serve ` your-template.html ` from a view just
185
- [ like any other] ( https://docs.djangoproject.com/en/3.2/intro/tutorial03/#write-views-that-actually-do-something ) .
132
+ _ Note: If you wish to change the route where this websocket is served from, see the
133
+ available [ settings] ( #settingspy ) ._
186
134
187
135
``` python
188
- from django.shortcuts import render
189
-
190
- def your_view (request ):
191
- context = {}
192
- return render(request, " your-template.html" , context)
193
- ```
194
136
195
- ## ` example_app/urls.py `
137
+ import os
138
+ from django.core.asgi import get_asgi_application
139
+ from django_idom import IDOM_WEBSOCKET_PATH
196
140
197
- Include your view in the list of urlpatterns
141
+ os.environ.setdefault(" DJANGO_SETTINGS_MODULE" , " test_app.settings" )
142
+ http_asgi_app = get_asgi_application()
198
143
199
- ``` python
200
- from django.urls import path
201
- from .views import your_view # define this view like any other HTML template view
144
+ from channels.auth import AuthMiddlewareStack
145
+ from channels.routing import ProtocolTypeRouter, URLRouter
202
146
203
- urlpatterns = [
204
- path(" " , your_view),
205
- ...
206
- ]
147
+ application = ProtocolTypeRouter(
148
+ {
149
+ " http" : http_asgi_app,
150
+ " websocket" : SessionMiddlewareStack(
151
+ AuthMiddlewareStack(URLRouter([IDOM_WEBSOCKET_PATH ]))
152
+ ),
153
+ }
154
+ )
207
155
```
208
156
209
157
# Developer Guide
0 commit comments