|
10 | 10 | <img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-purple.svg">
|
11 | 11 | </a>
|
12 | 12 |
|
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. |
15 | 16 |
|
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> |
22 | 27 |
|
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). |
34 | 29 |
|
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. |
37 | 30 |
|
38 |
| -### Or Install it Now |
| 31 | +# Install Django IDOM |
39 | 32 |
|
40 | 33 | ```bash
|
41 | 34 | pip install django-idom
|
42 | 35 | ```
|
43 | 36 |
|
44 | 37 | # Django Integration
|
45 | 38 |
|
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: |
47 | 121 |
|
48 | 122 | ```python
|
49 |
| -# Example code goes here |
| 123 | +INSTALLED_APPS = [ |
| 124 | + ..., |
| 125 | + "django_idom", |
| 126 | +] |
50 | 127 | ```
|
51 | 128 |
|
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 | +``` |
0 commit comments