-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
How to use inside javascript. closes #22
- Loading branch information
Showing
11 changed files
with
173 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FAQ | ||
=== |
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 was deleted.
Oops, something went wrong.
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,8 @@ | ||
Tutorials | ||
========= | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
javascript | ||
|
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,67 @@ | ||
Javascript | ||
========== | ||
|
||
Introduction | ||
************ | ||
|
||
You may use the ``adia`` inside Javascript (browser) because we pass all tests | ||
with both ``CPython`` and ``Brython`` environments before each commit. | ||
|
||
This page aims to demonstrate how to use the ``adia`` inside your javascript | ||
project. | ||
|
||
For the first you have to generate ``adia.bundle.js`` using: | ||
|
||
.. code-block:: bash | ||
cd path/to/adia | ||
make www | ||
``www/build/adia.bundle.js`` is what you need now. | ||
|
||
Copy and paste the generated file inside your Javascript project's static | ||
directory, where you can fetch it by a URL. then load it using: | ||
|
||
.. code-block:: html | ||
|
||
<html> | ||
<head> | ||
... | ||
<script type="text/javascript" src="adia.bundle.js"></script> | ||
</head> | ||
<body onload="brython()"> | ||
<script type="text/python"> | ||
from browser import window | ||
from adia import renders, BadSyntax | ||
def adia_renders(source): | ||
try: | ||
return renders(source) | ||
except BadSyntax as ex: | ||
return f'Error: {ex}' | ||
window.adiaRenders = adia_renders | ||
</script> | ||
|
||
... | ||
|
||
</body> | ||
</html> | ||
|
||
|
||
After the setup above you can use the function ``window.adiaRenders(...)`` | ||
anywhere. | ||
|
||
|
||
Javascript demo page | ||
******************** | ||
|
||
Let's make and visit the provided javascript demo page: ``www/jsdemo.html``. | ||
|
||
.. code-block:: bash | ||
cd path/to/adia | ||
make clean serve | ||
Now browse the http://localhost:8000/jsdemo.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
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,77 @@ | ||
<!doctype html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<script type="text/javascript" src="adia.bundle.js"></script> | ||
<style> | ||
font-family: monospace; | ||
</style> | ||
</head> | ||
|
||
<body onload="brython({debug: 1})"> | ||
|
||
<a href="/check.html">Jump to tests</a> | ||
<br /> | ||
|
||
<textarea cols="100%" rows="8" id="error"></textarea> | ||
<br /> | ||
<textarea cols="55" rows="40" id="source"> | ||
diagram: ADia demo | ||
version: 1.0 | ||
author: pylover | ||
|
||
sequence: | ||
|
||
@foo: Start | ||
foo -> bar: hello() -> HI | ||
bar -> baz: hey() -> Hey | ||
|
||
@foo: End | ||
</textarea> | ||
<textarea cols="120" rows="40" id="target"></textarea> | ||
|
||
<script type="text/python"> | ||
from browser import window | ||
from adia import renders, BadSyntax | ||
|
||
def adia_renders(source): | ||
try: | ||
return renders(source) | ||
except BadSyntax as ex: | ||
return f'Error: {ex}' | ||
|
||
window.adiaRenders = adia_renders | ||
</script> | ||
|
||
<script> | ||
let source = document.getElementById('source'); | ||
let target = document.getElementById('target'); | ||
let error = document.getElementById('error'); | ||
|
||
function refresh(ev) { | ||
if (window.adiaRenders == undefined) { | ||
setTimeout(refresh, 200); | ||
return; | ||
} | ||
|
||
if ((ev != undefined) && (ev.charCode != undefined) && | ||
(ev.charCode != 13)) { | ||
return; | ||
} | ||
var out = window.adiaRenders(source.value); | ||
if (out.startsWith('Error: ')) { | ||
error.value = out; | ||
} | ||
else { | ||
error.value = ''; | ||
target.value = out; | ||
} | ||
} | ||
|
||
window.addEventListener('load', refresh); | ||
source.addEventListener('keypress', refresh); | ||
</script> | ||
|
||
</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