-
-
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.
Client-side Python components using PyScript (#243)
- Client-side Python components can now be rendered via the new `{% pyscript_component %}` template tag - You must first call the `{% pyscript_setup %}` template tag to load PyScript dependencies - Client-side components can be embedded into existing server-side components via `reactpy_django.components.pyscript_component`. - Tired of writing JavaScript? You can now write PyScript code that runs directly within client browser via the `reactpy_django.html.pyscript` element. - This is a viable substitution for most JavaScript code.
- Loading branch information
1 parent
2e343aa
commit fb6c258
Showing
80 changed files
with
7,395 additions
and
183 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
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,14 @@ | ||
{% load reactpy %} | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup %} | ||
</head> | ||
|
||
<body> | ||
{% pyscript_component "./example_project/my_app/components/hello_world.py" %} | ||
</body> | ||
|
||
</html> |
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 @@ | ||
<body> | ||
{% pyscript_component "./example_project/my_app/components/root.py" initial=my_initial_object %} | ||
</body> |
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 @@ | ||
<body> | ||
{% pyscript_component "./example_project/my_app/components/root.py" initial="<div> Loading ... </div>" %} | ||
</body> |
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,14 @@ | ||
{% load reactpy %} | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup extra_js='{"/static/moment.js":"moment"}' %} | ||
</head> | ||
|
||
<body> | ||
{% component "example_project.my_app.components.root.py" %} | ||
</body> | ||
|
||
</html> |
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,15 @@ | ||
{% load reactpy %} | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup %} | ||
</head> | ||
|
||
<body> | ||
{% pyscript_component "./example_project/my_app/components/root.py" | ||
"./example_project/my_app/components/child.py" %} | ||
</body> | ||
|
||
</html> |
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 @@ | ||
<body> | ||
{% pyscript_component "./example_project/my_app/components/main.py" root="main" %} | ||
</body> |
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,4 @@ | ||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup config=my_config_object %} | ||
</head> |
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,4 @@ | ||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup config='{"experimental_create_proxy":"auto"}' %} | ||
</head> |
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,4 @@ | ||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup "dill==0.3.5" "markdown<=3.6.0" "nest_asyncio" "titlecase" %} | ||
</head> |
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,14 @@ | ||
{% load reactpy %} | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup extra_js=my_extra_js_object %} | ||
</head> | ||
|
||
<body> | ||
{% component "example_project.my_app.components.root.py" %} | ||
</body> | ||
|
||
</html> |
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,14 @@ | ||
{% load reactpy %} | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup extra_js='{"/static/moment.js":"moment"}' %} | ||
</head> | ||
|
||
<body> | ||
{% component "example_project.my_app.components.root.py" %} | ||
</body> | ||
|
||
</html> |
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,6 @@ | ||
{% load reactpy %} | ||
|
||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup %} | ||
</head> |
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,14 @@ | ||
{% load reactpy %} | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup %} | ||
</head> | ||
|
||
<body> | ||
{% component "example_project.my_app.components.server_side_component" %} | ||
</body> | ||
|
||
</html> |
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,14 @@ | ||
{% load reactpy %} | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>ReactPy</title> | ||
{% pyscript_setup %} | ||
</head> | ||
|
||
<body> | ||
{% component "example_project.my_app.components.server_side_component.py" %} | ||
</body> | ||
|
||
</html> |
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
def index(request): | ||
return render(request, "my-template.html") | ||
return render(request, "my_template.html") |
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,12 @@ | ||
from reactpy import component, html | ||
from reactpy_django.components import pyscript_component | ||
|
||
|
||
@component | ||
def server_side_component(): | ||
return html.div( | ||
pyscript_component( | ||
"./example_project/my_app/components/root.py", | ||
initial=html.div("Loading ..."), | ||
), | ||
) |
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,12 @@ | ||
from reactpy import component, html | ||
from reactpy_django.components import pyscript_component | ||
|
||
|
||
@component | ||
def server_side_component(): | ||
return html.div( | ||
pyscript_component( | ||
"./example_project/my_app/components/root.py", | ||
initial="<div> Loading ... </div>", | ||
), | ||
) |
12 changes: 12 additions & 0 deletions
12
docs/examples/python/pyscript-component-multiple-files-root.py
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,12 @@ | ||
from reactpy import component, html | ||
from reactpy_django.components import pyscript_component | ||
|
||
|
||
@component | ||
def server_side_component(): | ||
return html.div( | ||
pyscript_component( | ||
"./example_project/my_app/components/root.py", | ||
"./example_project/my_app/components/child.py", | ||
), | ||
) |
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,12 @@ | ||
from reactpy import component, html | ||
from reactpy_django.components import pyscript_component | ||
|
||
|
||
@component | ||
def server_side_component(): | ||
return html.div( | ||
pyscript_component( | ||
"./example_project/my_app/components/main.py", | ||
root="main", | ||
), | ||
) |
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,6 @@ | ||
from reactpy import component, html | ||
|
||
|
||
@component | ||
def root(): | ||
return html.div("Hello, World!") |
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,10 @@ | ||
from django.shortcuts import render | ||
from reactpy import html | ||
|
||
|
||
def index(request): | ||
return render( | ||
request, | ||
"my_template.html", | ||
context={"my_initial_object": html.div("Loading ...")}, | ||
) |
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 @@ | ||
import js | ||
from reactpy import component, html | ||
|
||
|
||
@component | ||
def root(): | ||
|
||
def onClick(event): | ||
js.document.title = "New window title" | ||
|
||
return html.button({"onClick": onClick}, "Click Me!") |
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,12 @@ | ||
from reactpy import component, html | ||
|
||
|
||
@component | ||
def root(): | ||
from pyscript.js_modules import moment | ||
|
||
return html.div( | ||
{"id": "moment"}, | ||
"Using the JavaScript package 'moment' to calculate time: ", | ||
moment.default().format("YYYY-MM-DD HH:mm:ss"), | ||
) |
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,6 @@ | ||
from reactpy import component, html | ||
|
||
|
||
@component | ||
def child_component(): | ||
return html.div("This is a child component from a different file.") |
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 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from reactpy import component, html | ||
|
||
if TYPE_CHECKING: | ||
from .child import child_component | ||
|
||
|
||
@component | ||
def root(): | ||
return html.div("This text is from the root component.", child_component()) |
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,6 @@ | ||
from reactpy import component, html | ||
|
||
|
||
@component | ||
def main(): | ||
return html.div("Hello, World!") |
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,9 @@ | ||
from django.shortcuts import render | ||
|
||
|
||
def index(request): | ||
return render( | ||
request, | ||
"my_template.html", | ||
context={"my_config_object": {"experimental_create_proxy": "auto"}}, | ||
) |
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,10 @@ | ||
from django.shortcuts import render | ||
from django.templatetags.static import static | ||
|
||
|
||
def index(request): | ||
return render( | ||
request, | ||
"my_template.html", | ||
context={"my_extra_js_object": {static("moment.js"): "moment"}}, | ||
) |
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,6 @@ | ||
from reactpy import component, html | ||
|
||
|
||
@component | ||
def root(): | ||
return html.div("This text is from my client-side component") |
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,10 @@ | ||
from reactpy import component, html | ||
from reactpy_django.components import pyscript_component | ||
|
||
|
||
@component | ||
def server_side_component(): | ||
return html.div( | ||
"This text is from my server-side component", | ||
pyscript_component("./example_project/my_app/components/root.py"), | ||
) |
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,15 @@ | ||
from reactpy import component, html | ||
from reactpy_django.html import pyscript | ||
|
||
example_source_code = """ | ||
import js | ||
js.console.log("Hello, World!") | ||
""" | ||
|
||
|
||
@component | ||
def server_side_component(): | ||
return html.div( | ||
pyscript(example_source_code.strip()), | ||
) |
Oops, something went wrong.