Skip to content

Commit 9f4412d

Browse files
committed
add basic docs to README
1 parent cd12a7c commit 9f4412d

File tree

4 files changed

+156
-29
lines changed

4 files changed

+156
-29
lines changed

README.md

Lines changed: 151 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,169 @@
1010
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-purple.svg">
1111
</a>
1212

13-
A package for building highly interactive user interfaces in pure Python inspired by
14-
[ReactJS](https://reactjs.org/).
13+
`django-idom` allows you to integrate [IDOM](https://github.com/idom-team/idom), a
14+
package for building highly interactive user interfaces in pure Python inspired by
15+
[ReactJS](https://reactjs.org/), into Django applications.
1516

16-
**Be sure to [read the IDOM Documentation](https://idom-docs.herokuapp.com)!**
17-
18-
If you have ideas or find a bug, be sure to post an
19-
[issue](https://github.com/idom-team/django-idom/issues)
20-
or create a
21-
[pull request](https://github.com/idom-team/django-idom/pulls). Thanks in advance!
17+
**You can try IDOM now in a Jupyter Notebook:**
18+
<a
19+
target="_blank"
20+
href="https://mybinder.org/v2/gh/idom-team/idom-jupyter/main?filepath=notebooks%2Fintroduction.ipynb">
21+
<img
22+
alt="Binder"
23+
valign="bottom"
24+
height="21px"
25+
src="https://mybinder.org/badge_logo.svg"/>
26+
</a>
2227

23-
<h3>
24-
<a
25-
target="_blank"
26-
href="https://mybinder.org/v2/gh/idom-team/idom-jupyter/main?filepath=notebooks%2Fintroduction.ipynb"
27-
>
28-
Try it Now
29-
<img alt="Binder" valign="bottom" height="25px"
30-
src="https://mybinder.org/badge_logo.svg"
31-
/>
32-
</a>
33-
</h3>
28+
For more information on IDOM refer to [its documentation](https://idom-docs.herokuapp.com).
3429

35-
Click the badge above to get started! It will take you to a [Jupyter Notebooks](https://jupyter.org/)
36-
hosted by [Binder](https://mybinder.org/) with some great examples.
3730

38-
### Or Install it Now
31+
# Install Django IDOM
3932

4033
```bash
4134
pip install django-idom
4235
```
4336

4437
# Django Integration
4538

46-
This version of IDOM can be directly integrated into Django. For example
39+
To integrate IDOM into your application you'll need to modify or add the following files to `your_app`:
40+
41+
```
42+
your_app/
43+
├── asgi.py
44+
├── components.py
45+
├── idom.py
46+
├── settings.py
47+
├── templates/
48+
│ ├── your-template.html
49+
└── urls.py
50+
```
51+
52+
## `asgi.py`
53+
54+
To start, we'll need to use [`channels`](https://channels.readthedocs.io/en/stable/) to
55+
create a `ProtocolTypeRouter` that will become the top of our ASGI application stack.
56+
Under the `"websocket"` protocol, we'll then add a path for IDOM's websocket consumer
57+
using `django_idom_websocket_consumer_path`. If you wish to change the route where this
58+
websocket is served from see the [settings](#configuration-options).
59+
60+
```python
61+
62+
import os
63+
64+
from django.core.asgi import get_asgi_application
65+
66+
from django_idom import django_idom_websocket_consumer_path
67+
68+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_app.settings")
69+
70+
# Fetch ASGI application before importing dependencies that require ORM models.
71+
http_asgi_app = get_asgi_application()
72+
73+
from channels.routing import ProtocolTypeRouter, URLRouter
74+
75+
application = ProtocolTypeRouter(
76+
{
77+
"http": http_asgi_app,
78+
"websocket": URLRouter(
79+
# add a path for IDOM's websocket
80+
[django_idom_websocket_consumer_path()]
81+
),
82+
}
83+
)
84+
```
85+
86+
## `components.py`
87+
88+
This is where, by a convention similar to that of
89+
[`views.py`](https://docs.djangoproject.com/en/3.2/topics/http/views/), you'll define
90+
your [IDOM](https://github.com/idom-team/idom) components. Ultimately though, you should
91+
feel free to organize your component modules you wish.
92+
93+
```python
94+
import idom
95+
96+
@idom.component
97+
def Hello(name): # component names are camelcase by convention
98+
return idom.html.h1(f"Hello {name}!")
99+
```
100+
101+
## `idom.py`
102+
103+
This file is automatically discovered by `django-idom` when scanning the list of
104+
[`INSTALLED_APPS`](https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-INSTALLED_APPS).
105+
All apps that export components will contain this module.
106+
107+
Inside this module must be a `components` list that is imported from
108+
[`components.py`](#components.py):
109+
110+
```python
111+
from .components import Hello
112+
113+
components = [Hello]
114+
```
115+
116+
## `settings.py`
117+
118+
In your settings you'll need to add `django_idom` to the
119+
[`INSTALLED_APPS`](https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-INSTALLED_APPS)
120+
list:
47121

48122
```python
49-
# Example code goes here
123+
INSTALLED_APPS = [
124+
...,
125+
"django_idom",
126+
]
50127
```
51128

52-
For examples on how to use IDOM, [read the IDOM Documentation](https://idom-docs.herokuapp.com).
129+
## `templates/your-template.html`
130+
131+
In your templates, you may inject a view of an IDOM component into your templated HTML
132+
by using the `idom_view` Jinja tag. You can even pass parameters to your component from
133+
the template via keyword arguments:
134+
135+
```html
136+
<!-- don't forget your load statements -->
137+
{% load static %}
138+
{% load idom %}
139+
140+
<!DOCTYPE html>
141+
<html>
142+
<body>
143+
...
144+
{% idom_view "test_app.Hello" name="World" %}
145+
</body>
146+
</html>
147+
```
148+
149+
Your view for this template can be defined just like any other
150+
151+
## `urls.py`
152+
153+
To your list of URLs you'll need to include IDOM's static web modules path using
154+
`django_idom_web_modules_path`:
155+
156+
```python
157+
from django.urls import path
158+
from django_idom import django_idom_web_modules_path
159+
from .views import your_template
160+
161+
162+
urlpatterns = [
163+
path("", your_template),
164+
django_idom_web_modules_path(),
165+
]
166+
```
167+
168+
# Configuration Options
169+
170+
You may configure additional options in your `settings.py` file
171+
172+
```python
173+
# the base URL for all IDOM-releated resources
174+
IDOM_BASE_URL: str = "_idom/"
175+
176+
# ignore these apps during component collection
177+
IDOM_IGNORED_DJANGO_APPS: set[str] = {"some_app", "some_other_app"}
178+
```

src/django_idom/app_components.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.conf import settings
66
from idom.core.proto import ComponentConstructor
77

8-
from .app_settings import IDOM_IGNORED_DJANGO_APPS
8+
from .app_settings import IDOM_IGNORE_INSTALLED_APPS
99

1010

1111
logger = logging.getLogger(__name__)
@@ -21,8 +21,8 @@ def has_component(name: str) -> bool:
2121

2222

2323
for app_mod_name in settings.INSTALLED_APPS:
24-
if app_mod_name in IDOM_IGNORED_DJANGO_APPS:
25-
logger.debug(f"{idom_mod_name!r} skipped by IDOM_IGNORED_DJANGO_APPS")
24+
if app_mod_name in IDOM_IGNORE_INSTALLED_APPS:
25+
logger.debug(f"{app_mod_name!r} skipped by IDOM_IGNORE_INSTALLED_APPS")
2626
continue
2727

2828
idom_mod_name = f"{app_mod_name}.idom"

src/django_idom/app_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
for file in (APP_DIR / "templates" / "idom").iterdir()
1111
}
1212

13-
IDOM_IGNORED_DJANGO_APPS = set(getattr(settings, "IDOM_IGNORED_DJANGO_APPS", []))
13+
IDOM_IGNORE_INSTALLED_APPS = set(getattr(settings, "IDOM_IGNORE_INSTALLED_APPS", []))
1414

1515
IDOM_BASE_URL = getattr(settings, "IDOM_BASE_URL", "_idom/")
1616
IDOM_WEBSOCKET_URL = IDOM_BASE_URL + "websocket/"

src/django_idom/templatetags/idom.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
IDOM_WEBSOCKET_URL,
1010
TEMPLATE_FILE_PATHS,
1111
)
12+
1213
from ..app_components import has_component
1314

1415

0 commit comments

Comments
 (0)