-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Deferred static file components - Docs for deferred static files - Bump minimum IDOM version - Create PR templates - Sync issue form template with `idom-team/idom` - Pin selenium to 4.2 for compatibility - Bump django-idom to v1.1.0 - Docstring for component template tag - Minor readme wordsmithing
- Loading branch information
1 parent
b9ca296
commit 18a419f
Showing
18 changed files
with
273 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Description | ||
|
||
A summary of the changes. | ||
|
||
# Checklist: | ||
|
||
Please update this checklist as you complete each item: | ||
|
||
- [ ] Tests have been included for all bug fixes or added functionality. | ||
- [ ] The `changelog.rst` has been updated with any significant changes, if necessary. | ||
- [ ] GitHub Issues which may be closed by this PR have been linked. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
## Django CSS | ||
|
||
Allows you to defer loading a CSS stylesheet until a component begins rendering. This stylesheet must be stored within [Django's static files](https://docs.djangoproject.com/en/dev/howto/static-files/). | ||
|
||
```python title="components.py" | ||
from idom import component, html | ||
from django_idom.components import django_css | ||
|
||
@component | ||
def MyComponent(): | ||
return html.div( | ||
django_css("css/buttons.css"), | ||
html.button("My Button!"), | ||
) | ||
``` | ||
|
||
??? question "Should I put `django_css` at the top of my component?" | ||
|
||
Yes, if the stylesheet contains styling for your component. | ||
|
||
??? question "Can I load static CSS using `html.link` instead?" | ||
|
||
While you can load stylesheets with `html.link`, keep in mind that loading this way **does not** ensure load order. Thus, your stylesheet will be loaded after your component is displayed. This would likely cause some visual jankiness, so use this at your own discretion. | ||
|
||
Here's an example on what you should avoid doing for Django static files: | ||
|
||
```python | ||
from idom import component, html | ||
from django.templatetags.static import static | ||
|
||
@component | ||
def MyComponent(): | ||
return html.div( | ||
html.link({"rel": "stylesheet", "href": static("css/buttons.css")}), | ||
html.button("My Button!"), | ||
) | ||
``` | ||
|
||
??? question "How do I load external CSS?" | ||
|
||
`django_css` can only be used with local static files. | ||
|
||
For external CSS, substitute `django_css` with `html.link`. | ||
|
||
```python | ||
from idom import component, html | ||
|
||
@component | ||
def MyComponent(): | ||
return html.div( | ||
html.link({"rel": "stylesheet", "href": "https://example.com/external-styles.css"}), | ||
html.button("My Button!"), | ||
) | ||
``` | ||
|
||
??? question "Why not load my CSS in `#!html <head>`?" | ||
|
||
Traditionally, stylesheets are loaded in your `#!html <head>` using the `#!jinja {% load static %}` template tag. | ||
|
||
To help improve webpage load times, you can use the `django_css` component to defer loading your stylesheet until it is needed. | ||
|
||
## Django JS | ||
|
||
Allows you to defer loading JavaScript until a component begins rendering. This JavaScript must be stored within [Django's static files](https://docs.djangoproject.com/en/dev/howto/static-files/). | ||
|
||
```python title="components.py" | ||
from idom import component, html | ||
from django_idom.components import django_js | ||
|
||
@component | ||
def MyComponent(): | ||
return html.div( | ||
html.button("My Button!"), | ||
django_js("js/scripts.js"), | ||
) | ||
``` | ||
|
||
??? question "Should I put `django_js` at the bottom of my component?" | ||
|
||
Yes, if your scripts are reliant on the contents of the component. | ||
|
||
??? question "Can I load static JavaScript using `html.script` instead?" | ||
|
||
While you can load JavaScript with `html.script`, keep in mind that loading this way **does not** ensure load order. Thus, your JavaScript will likely be loaded at an arbitrary time after your component is displayed. | ||
|
||
Here's an example on what you should avoid doing for Django static files: | ||
|
||
```python | ||
from idom import component, html | ||
from django.templatetags.static import static | ||
|
||
@component | ||
def MyComponent(): | ||
return html.div( | ||
html.script({"src": static("js/scripts.js")}), | ||
html.button("My Button!"), | ||
) | ||
``` | ||
|
||
??? question "How do I load external JS?" | ||
|
||
`django_js` can only be used with local static files. | ||
|
||
For external JavaScript, substitute `django_js` with `html.script`. | ||
|
||
```python | ||
from idom import component, html | ||
|
||
@component | ||
def MyComponent(): | ||
return html.div( | ||
html.script({"src": static("https://example.com/external-scripts.js")}), | ||
html.button("My Button!"), | ||
) | ||
``` | ||
|
||
??? question "Why not load my JS in `#!html <head>`?" | ||
|
||
Traditionally, JavaScript is loaded in your `#!html <head>` using the `#!jinja {% load static %}` template tag. | ||
|
||
To help improve webpage load times, you can use the `django_js` component to defer loading your JavaScript until it is needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
channels >=3.0.0 | ||
idom >=0.38.0, <0.39.0 | ||
idom >=0.39.0, <0.40.0 | ||
aiofile >=3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
django | ||
selenium | ||
selenium <= 4.2.0 | ||
twisted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from . import hooks | ||
from . import components, hooks | ||
from .websocket.consumer import IdomWebsocket | ||
from .websocket.paths import IDOM_WEBSOCKET_PATH | ||
|
||
|
||
__version__ = "1.0.0" | ||
__all__ = ["IDOM_WEBSOCKET_PATH", "IdomWebsocket", "hooks"] | ||
__version__ = "1.1.0" | ||
__all__ = ["IDOM_WEBSOCKET_PATH", "IdomWebsocket", "hooks", "components"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
|
||
from django.contrib.staticfiles.finders import find | ||
from idom import component, html | ||
|
||
from django_idom.config import IDOM_CACHE | ||
|
||
|
||
@component | ||
def django_css(static_path: str): | ||
"""Fetches a CSS static file for use within IDOM. This allows for deferred CSS loading. | ||
Args: | ||
static_path: The path to the static file. This path is identical to what you would | ||
use on a `static` template tag. | ||
""" | ||
return html._( | ||
html.script( | ||
""" | ||
let parentTag = document.currentScript; | ||
console.log(parentTag); | ||
//parentTag.attachShadow({ mode: 'open' }); | ||
""" | ||
), | ||
html.style(_cached_static_contents(static_path)), | ||
) | ||
|
||
|
||
@component | ||
def django_js(static_path: str): | ||
"""Fetches a JS static file for use within IDOM. This allows for deferred JS loading. | ||
Args: | ||
static_path: The path to the static file. This path is identical to what you would | ||
use on a `static` template tag. | ||
""" | ||
return html.script(_cached_static_contents(static_path)) | ||
|
||
|
||
def _cached_static_contents(static_path: str): | ||
# Try to find the file within Django's static files | ||
abs_path = find(static_path) | ||
if not abs_path: | ||
raise FileNotFoundError( | ||
f"Could not find static file {static_path} within Django's static files." | ||
) | ||
|
||
# Fetch the file from cache, if available | ||
# Cache is preferrable to `use_memo` due to multiprocessing capabilities | ||
last_modified_time = os.stat(abs_path).st_mtime | ||
cache_key = f"django_idom:static_contents:{static_path}" | ||
file_contents = IDOM_CACHE.get(cache_key, version=last_modified_time) | ||
if file_contents is None: | ||
with open(abs_path, encoding="utf-8") as static_file: | ||
file_contents = static_file.read() | ||
IDOM_CACHE.delete(cache_key) | ||
IDOM_CACHE.set( | ||
cache_key, file_contents, timeout=None, version=last_modified_time | ||
) | ||
|
||
return file_contents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#static-css button { | ||
color: rgba(0, 0, 255, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let el = document.body.querySelector("#static-js"); | ||
el.textContent = "StaticJS: True"; | ||
el.dataset.success = "true"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters