Template Tag
Overview¶
Django template tags can be used within your HTML templates to provide ReactPy features.
Component¶
This template tag can be used to insert any number of server-side ReactPy components onto your page.
Each component loaded via this template tag will receive a dedicated WebSocket connection to the server.
1 2 3 4 5 6 7 |
|
See Interface
Parameters
Name | Type | Description | Default |
---|---|---|---|
dotted_path | str | The dotted path to the component to render. | N/A |
*args | Any | The positional arguments to provide to the component. | N/A |
class | str | None | The HTML class to apply to the top-level component div. | None |
key | Any | Force the component's root node to use a specific key value. Using key within a template tag is effectively useless. | None |
host | str | None | The host to use for ReactPy connections. If unset, the host will be automatically configured. Example values include: localhost:8000 , example.com , example.com/subdir | None |
prerender | str | If "true" the component will pre-rendered, which enables SEO compatibility and reduces perceived latency. | "false" |
offline | str | The dotted path to a component that will be displayed if your root component loses connection to the server. Keep in mind, this offline component will be non-interactive (hooks won't operate). | "" |
**kwargs | Any | The keyword arguments to provide to the component. | N/A |
Do not use context variables for the component path
The ReactPy component finder requires that your component path is a string.
Do not use Django template/context variables for the component path. Failure to follow this warning will result in render failures.
For example, do not do the following:
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 |
|
Note: If you decide to not follow this warning, you will need to use the register_component
function to manually register your components.
Can I use multiple components on one page?
You can add as many components to a webpage as needed by using the template tag multiple times. Retrofitting legacy sites to use ReactPy will typically involve many components on one page.
1 2 3 4 5 6 7 8 9 |
|
Please note that components separated like this will not be able to interact with each other, except through database queries.
Additionally, in scenarios where you are trying to create a Single Page Application (SPA) within Django, you will only have one component within your <body>
tag.
Can I use positional arguments instead of keyword arguments?
You can use any combination of *args
/**kwargs
in your template tag.
1 |
|
1 2 3 4 5 6 |
|
Can I render components on a different server (distributed computing)?
Yes! This is most commonly done through settings.py:REACTPY_HOSTS
. However, you can use the host
keyword to render components on a specific ASGI server.
1 2 3 |
|
This configuration most commonly involves you deploying multiple instances of your project. But, you can also create dedicated Django project(s) that only render specific ReactPy components if you wish.
Here's a couple of things to keep in mind:
- If your host address are completely separate (
origin1.com != origin2.com
) you will need to configure CORS headers on your main application during deployment. - You will not need to register ReactPy WebSocket or HTTP paths on any applications that do not perform any component rendering.
- Your component will only be able to access your template tag's
*args
/**kwargs
if your applications share a common database.
How do I pre-render components for SEO compatibility?
This is most commonly done through settings.py:REACTPY_PRERENDER
. However, you can use the prerender
keyword to pre-render a specific component.
1 2 3 |
|
How do I display something when the client disconnects?
You can use the offline
keyword to display a specific component when the client disconnects from the server.
1 2 3 |
|
Note: The offline
component will be non-interactive (hooks won't operate).
PyScript Component¶
This template tag can be used to insert any number of client-side ReactPy components onto your page.
By default, the only available dependencies are the Python standard library, pyscript
, pyodide
, reactpy
core.
The entire file path provided is loaded directly into the browser, and must have a def root()
component to act as the entry point.
Pitfall
Similar to JavaScript, your provided Python file is loaded directly into the client (web browser) as raw text to run using the PyScript interpreter. Be cautious about what you include in your Python file.
As a result being client-sided, Python packages within your local environment (such as those installed via pip install ...
) are not accessible within PyScript components.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 |
|
See Interface
Parameters
Name | Type | Description | Default |
---|---|---|---|
*file_paths | str | File path to your client-side component. If multiple paths are provided, the contents are automatically merged. | N/A |
initial | str | VdomDict | ComponentType | The initial HTML that is displayed prior to the PyScript component loads. This can either be a string containing raw HTML, a reactpy.html snippet, or a non-interactive component. | "" |
root | str | The name of the root component function. | "root" |
How do I execute JavaScript within PyScript components?
PyScript components several options available to execute JavaScript, including...
Pyodide JS Module
The Pyodide js
module has access to everything within the browser's JavaScript environment. Therefore, any global JavaScript functions loaded within your HTML <head>
can be called as well. However, you will need to be mindful of JavaScript load order if using async
or deferred
loading!
1 2 3 4 5 6 7 8 9 10 11 |
|
PyScript FFI
...
PyScript JS Modules
Assuming you have a local bundle stored within your project's static files, you can import JavaScript modules in a fashion similar to import {moment} from 'static/moment.js'
. You will first need to configure your {% pyscript_setup %}
block to make the moment.js
module available to PyScript. Then, this module can be accessed within pyscript.js_modules.*
.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Does my entire component need to be contained in one file?
Splitting a large file into multiple files is a common practice in software development.
However, PyScript components are run on the client browser. As such, they do not have access to your local development environment, and thus cannot import
any local Python files.
If your PyScript component file gets too large, you can declare multiple file paths instead. These files will automatically combined by ReactPy.
Here is how we recommend splitting your component into multiple files while avoiding local imports but retaining type hints.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 |
|
How do I display something while the component is loading?
You can configure the initial
keyword to display HTML while your PyScript component is loading.
The value for initial
is most commonly be a string containing raw HTML.
1 2 3 |
|
However, you can also insert a reactpy.html
snippet or a non-interactive @component
via template context.
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 |
|
Can I use a different name for my root component?
Yes, you can use the root
keyword to specify a different name for your root function.
1 2 3 |
|
1 2 3 4 5 6 |
|
PyScript Setup¶
This template tag configures the current page to be able to run pyscript
.
You can optionally use this tag to configure the current PyScript environment. For example, you can include a list of Python packages to automatically install within the PyScript environment.
1 2 3 4 5 6 |
|
See Interface
Parameters
Name | Type | Description | Default |
---|---|---|---|
*extra_py | str | Dependencies that need to be loaded on the page for your PyScript components. Each dependency must be contained within it's own string and written in Python requirements file syntax. | N/A |
extra_js | str | dict | A JSON string or Python dictionary containing a vanilla JavaScript module URL and the name: str to access it within pyscript.js_modules.* . | "" |
config | str | dict | A JSON string or Python dictionary containing PyScript configuration values. | "" |
How do I install additional Python dependencies?
Dependencies must be available on pypi
and declared in your {% pyscript_setup %}
block using Python requirements file syntax.
These dependencies are automatically downloaded and installed into the PyScript client-side environment when the page is loaded.
1 2 3 4 |
|
How do I install additional Javascript dependencies?
You can use the extra_js
keyword to load additional JavaScript modules into your PyScript environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 |
|
The value for extra_js
is most commonly a Python dictionary, but JSON strings are also supported.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
How do I modify the pyscript
default configuration?
You can modify the default PyScript configuration by providing a value to the config
keyword.
1 2 3 4 |
|
While this value is most commonly a JSON string, Python dictionary objects are also supported.
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 |
|
Can I use a local interpreter for PyScript?
Yes, you can set up a local interpreter by following PyScript's standard documentation.
To summarize,
- Download the latest Pyodide bundle from the Pyodide GitHub releases page (for example
pyodide-0.26.3.tar.bz2
). - Extract the contents of the bundle to your project's static files.
-
Configure your
{% pyscript_setup %}
template tag to usepyodide
as an interpreter.{% pyscript_setup config='{"interpreter":"/static/pyodide/pyodide.mjs"}' %}