|
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) into |
| 14 | +Django applications. IDOM is a pure Python library inspired by |
| 15 | +[ReactJS](https://reactjs.org/) for creating responsive web interfaces. |
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! |
22 |
| - |
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> |
| 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> |
34 | 27 |
|
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 | 28 |
|
38 |
| -### Or Install it Now |
| 29 | +# Install Django IDOM |
39 | 30 |
|
40 | 31 | ```bash
|
41 | 32 | pip install django-idom
|
42 | 33 | ```
|
43 | 34 |
|
44 | 35 | # Django Integration
|
45 | 36 |
|
46 |
| -This version of IDOM can be directly integrated into Django. For example |
| 37 | +To integrate IDOM into your application you'll need to modify or add the following files to `your_project`: |
| 38 | + |
| 39 | +``` |
| 40 | +your_project/ |
| 41 | +├── __init__.py |
| 42 | +├── asgi.py |
| 43 | +├── settings.py |
| 44 | +├── urls.py |
| 45 | +└── example_app/ |
| 46 | + ├── __init__.py |
| 47 | + ├── idom.py |
| 48 | + ├── templates/ |
| 49 | + │ └── your-template.html |
| 50 | + └── urls.py |
| 51 | +``` |
| 52 | + |
| 53 | +## `asgi.py` |
| 54 | + |
| 55 | +Follow the [`channels`](https://channels.readthedocs.io/en/stable/) |
| 56 | +[installation guide](https://channels.readthedocs.io/en/stable/installation.html) in |
| 57 | +order to create ASGI websockets within Django. Then, we will add a path for IDOM's |
| 58 | +websocket consumer using `IDOM_WEBSOCKET_PATH`. |
| 59 | + |
| 60 | +_Note: If you wish to change the route where this websocket is served from, see the |
| 61 | +available [settings](#settings.py)._ |
| 62 | + |
| 63 | +```python |
| 64 | + |
| 65 | +import os |
| 66 | + |
| 67 | +from django.core.asgi import get_asgi_application |
| 68 | + |
| 69 | +from django_idom import IDOM_WEB_MODULES_PATH |
| 70 | + |
| 71 | +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_app.settings") |
| 72 | + |
| 73 | +# Fetch ASGI application before importing dependencies that require ORM models. |
| 74 | +http_asgi_app = get_asgi_application() |
| 75 | + |
| 76 | +from channels.routing import ProtocolTypeRouter, URLRouter |
| 77 | + |
| 78 | +application = ProtocolTypeRouter( |
| 79 | + { |
| 80 | + "http": http_asgi_app, |
| 81 | + "websocket": URLRouter( |
| 82 | + # add a path for IDOM's websocket |
| 83 | + [IDOM_WEB_MODULES_PATH] |
| 84 | + ), |
| 85 | + } |
| 86 | +) |
| 87 | +``` |
| 88 | + |
| 89 | +## `settings.py` |
| 90 | + |
| 91 | +In your settings you'll need to add `django_idom` to the |
| 92 | +[`INSTALLED_APPS`](https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-INSTALLED_APPS) |
| 93 | +list: |
47 | 94 |
|
48 | 95 | ```python
|
49 |
| -# Example code goes here |
| 96 | +INSTALLED_APPS = [ |
| 97 | + ..., |
| 98 | + "django_idom", |
| 99 | +] |
50 | 100 | ```
|
51 | 101 |
|
52 |
| -For examples on how to use IDOM, [read the IDOM Documentation](https://idom-docs.herokuapp.com). |
| 102 | +You may configure additional options as well: |
| 103 | + |
| 104 | +```python |
| 105 | +# the base URL for all IDOM-releated resources |
| 106 | +IDOM_BASE_URL: str = "_idom/" |
| 107 | + |
| 108 | +# Set cache size limit for loading JS files for IDOM. |
| 109 | +# Only applies when not using Django's caching framework (see below). |
| 110 | +IDOM_WEB_MODULE_LRU_CACHE_SIZE: int | None = None |
| 111 | + |
| 112 | +# Configure a cache for loading JS files |
| 113 | +CACHES = { |
| 114 | + # Configure a cache for loading JS files for IDOM |
| 115 | + "idom_web_modules": {"BACKEND": ...}, |
| 116 | + # If the above cache is not configured, then we'll use the "default" instead |
| 117 | + "default": {"BACKEND": ...}, |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +## `urls.py` |
| 122 | + |
| 123 | +You'll need to include IDOM's static web modules path using `IDOM_WEB_MODULES_PATH`. |
| 124 | +Similarly to the `IDOM_WEBSOCKET_PATH`. If you wish to change the route where this |
| 125 | +websocket is served from, see the available [settings](#settings.py). |
| 126 | + |
| 127 | +```python |
| 128 | +from django_idom import IDOM_WEB_MODULES_PATH |
| 129 | + |
| 130 | +urlpatterns = [ |
| 131 | + IDOM_WEB_MODULES_PATH, |
| 132 | + ... |
| 133 | +] |
| 134 | +``` |
| 135 | + |
| 136 | +## `example_app/components.py` |
| 137 | + |
| 138 | +This is where, by a convention similar to that of |
| 139 | +[`views.py`](https://docs.djangoproject.com/en/3.2/topics/http/views/), you'll define |
| 140 | +your [IDOM](https://github.com/idom-team/idom) components. Ultimately though, you should |
| 141 | +feel free to organize your component modules you wish. The components created here will |
| 142 | +ultimately be referenced by name in `your-template.html`. `your-template.html`. |
| 143 | + |
| 144 | +```python |
| 145 | +import idom |
| 146 | + |
| 147 | +@idom.component |
| 148 | +def Hello(greeting_recipient): # component names are camelcase by convention |
| 149 | + return Header(f"Hello {greeting_recipient}!") |
| 150 | +``` |
| 151 | + |
| 152 | +## `example_app/templates/your-template.html` |
| 153 | + |
| 154 | +In your templates, you may inject a view of an IDOM component into your templated HTML |
| 155 | +by using the `idom_component` template tag. This tag which requires the name of a component |
| 156 | +to render (of the form `module_name.ComponentName`) and keyword arguments you'd like to |
| 157 | +pass it from the template. |
| 158 | + |
| 159 | +```python |
| 160 | +idom_component module_name.ComponentName param_1="something" param_2="something-else" |
| 161 | +``` |
| 162 | + |
| 163 | +In context this will look a bit like the following... |
| 164 | + |
| 165 | +```jinja |
| 166 | +<!-- don't forget your load statements --> |
| 167 | +{% load static %} |
| 168 | +{% load idom %} |
| 169 | +
|
| 170 | +<!DOCTYPE html> |
| 171 | +<html> |
| 172 | + <body> |
| 173 | + ... |
| 174 | + {% idom_component "your_project.example_app.components.Hello" greeting_recipient="World" %} |
| 175 | + </body> |
| 176 | +</html> |
| 177 | +``` |
| 178 | + |
| 179 | +## `example_app/views.py` |
| 180 | + |
| 181 | +You can then serve `your-template.html` from a view just |
| 182 | +[like any other](https://docs.djangoproject.com/en/3.2/intro/tutorial03/#write-views-that-actually-do-something). |
| 183 | + |
| 184 | +```python |
| 185 | +from django.http import HttpResponse |
| 186 | +from django.template import loader |
| 187 | + |
| 188 | + |
| 189 | +def your_view(request): |
| 190 | + context = {} |
| 191 | + return HttpResponse( |
| 192 | + loader.get_template("your-template.html").render(context, request) |
| 193 | + ) |
| 194 | +``` |
| 195 | + |
| 196 | +## `example_app/urls.py` |
| 197 | + |
| 198 | +Include your view in the list of urlpatterns |
| 199 | + |
| 200 | +```python |
| 201 | +from django.urls import path |
| 202 | +from .views import your_view # define this view like any other HTML template view |
| 203 | + |
| 204 | +urlpatterns = [ |
| 205 | + path("", your_view), |
| 206 | + ... |
| 207 | +] |
| 208 | +``` |
| 209 | + |
| 210 | +# Developer Guide |
| 211 | + |
| 212 | +If you plan to make code changes to this repository, you'll need to install the |
| 213 | +following dependencies first: |
| 214 | + |
| 215 | +- [NPM](https://docs.npmjs.com/try-the-latest-stable-version-of-npm) for |
| 216 | + installing and managing Javascript |
| 217 | +- [ChromeDriver](https://chromedriver.chromium.org/downloads) for testing with |
| 218 | + [Selenium](https://www.seleniumhq.org/) |
| 219 | + |
| 220 | +Once done, you should clone this repository: |
| 221 | + |
| 222 | +```bash |
| 223 | +git clone https://github.com/idom-team/django-idom.git |
| 224 | +cd django-idom |
| 225 | +``` |
| 226 | + |
| 227 | +Then, by running the command below you can: |
| 228 | + |
| 229 | +- Install an editable version of the Python code |
| 230 | + |
| 231 | +- Download, build, and install Javascript dependencies |
| 232 | + |
| 233 | +```bash |
| 234 | +pip install -e . -r requirements.txt |
| 235 | +``` |
| 236 | + |
| 237 | +Finally, to verify that everything is working properly, you'll want to run the test suite. |
| 238 | + |
| 239 | +## Running The Tests |
| 240 | + |
| 241 | +This repo uses [Nox](https://nox.thea.codes/en/stable/) to run scripts which can |
| 242 | +be found in `noxfile.py`. For a full test of available scripts run `nox -l`. To run the full test suite simple execute: |
| 243 | + |
| 244 | +``` |
| 245 | +nox -s test |
| 246 | +``` |
| 247 | + |
| 248 | +To run the tests using a headless browser: |
| 249 | + |
| 250 | +``` |
| 251 | +nox -s test -- --headless |
| 252 | +``` |
0 commit comments