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