Skip to content

Commit 2206dad

Browse files
committed
Added async examples
Added WASM example
1 parent 6904710 commit 2206dad

File tree

12 files changed

+345
-0
lines changed

12 files changed

+345
-0
lines changed

brython/async/aio/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Real Python

brython/async/aio/index.html

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<!DOCTYPE html >
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<link
6+
rel="stylesheet"
7+
href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.min.css"
8+
integrity="sha256-jYujp4Kf07YDuUF9i1MHo4AnpXUKuHxIUXH7CrHxdKw="
9+
crossorigin="anonymous" />
10+
<script
11+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js" defer>
12+
</script>
13+
<script
14+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js" defer>
15+
</script>
16+
<style>
17+
body { padding: 30px; }
18+
aside {
19+
background: lightyellow;
20+
margin: 1em 0;
21+
padding: .3em 1em;
22+
border-radius: 3px;
23+
border: 1px solid gray;
24+
color: gray;
25+
}
26+
</style>
27+
</head>
28+
<body onload="brython(1)">
29+
<h2>Aio Requests</h2>
30+
<aside><p>Demonstrate the usage of GET using <tt>browser.aio</tt>.
31+
You need to start a server for this example to work.
32+
You can start the Python development web server with the following command:
33+
<tt>python3 -m http.server</tt>.
34+
</p></aside>
35+
<form class="pure-form" onsubmit="return false;">
36+
<legend>Actions</legend>
37+
<button id="get-btn" class="pure-button pure-button-primary">Async Get</button>
38+
<button id="clear-logs-btn" class="pure-button">Clear Logs</button>
39+
<legend>Logs</legend>
40+
<textarea id="log" name="log" rows="20" cols="60"></textarea>
41+
</form>
42+
<script type="text/python">
43+
from browser import aio, document
44+
import javascript
45+
46+
def counter():
47+
x = 1
48+
while True:
49+
yield x
50+
x += 1
51+
52+
cnt = counter()
53+
54+
def log(message):
55+
document["log"].value += f"{next(cnt):03}: {message} \n"
56+
57+
def add_log_sep():
58+
log_text = document["log"].value
59+
if not log_text:
60+
return
61+
else:
62+
document["log"].value += "======================================\n"
63+
64+
def clear_logs(evt):
65+
global cnt
66+
cnt = counter() # Reset counter
67+
document["log"].value = ""
68+
69+
async def process_get(url):
70+
log("Start process_get")
71+
log("Before await aio.get")
72+
req = await aio.get(url)
73+
log(f"Retrieved data: '{req.data}'")
74+
log("End process_get")
75+
76+
def aio_get(evt):
77+
add_log_sep()
78+
log("Before aio.run")
79+
aio.run(process_get("/api.txt"))
80+
log("After aio.run")
81+
82+
document["get-btn"].bind("click", aio_get)
83+
document["clear-logs-btn"].bind("click", clear_logs)
84+
</script>
85+
</body>
86+
87+
</html>

brython/async/ajax/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Real Python

brython/async/ajax/index.html

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<!DOCTYPE html >
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<link
6+
rel="stylesheet"
7+
href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.min.css"
8+
integrity="sha256-jYujp4Kf07YDuUF9i1MHo4AnpXUKuHxIUXH7CrHxdKw="
9+
crossorigin="anonymous" />
10+
<script
11+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js" defer>
12+
</script>
13+
<script
14+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js" defer>
15+
</script>
16+
<style>
17+
body { padding: 30px; }
18+
aside {
19+
background: lightyellow;
20+
margin: 1em 0;
21+
padding: .3em 1em;
22+
border-radius: 3px;
23+
border: 1px solid gray;
24+
color: gray;
25+
}
26+
</style>
27+
</head>
28+
<body onload="brython(1)">
29+
<h2>Ajax Requests</h2>
30+
<aside><p>Demonstrate the usage of GET using <tt>browser.ajax</tt>.
31+
You need to start a server for this example to work.
32+
You can start the Python development web server with the following command:
33+
<tt>python3 -m http.server</tt>.
34+
</p></aside>
35+
<form class="pure-form" onsubmit="return false;">
36+
<legend>Actions</legend>
37+
<button id="get-btn" class="pure-button pure-button-primary">Async Get</button>
38+
<button id="get-blocking-btn" class="pure-button pure-button-primary">Blocking Get</button>
39+
<button id="clear-logs-btn" class="pure-button">Clear Logs</button>
40+
<legend>Logs</legend>
41+
<textarea id="log" name="log" rows="20" cols="60"></textarea>
42+
</form>
43+
<script type="text/python">
44+
from browser import ajax, document
45+
import javascript
46+
47+
def counter():
48+
x = 1
49+
while True:
50+
yield x
51+
x += 1
52+
53+
cnt = counter()
54+
55+
def on_complete(req):
56+
if req.status == 200:
57+
log(f"Text received: '{req.text}'")
58+
elif req.status == 0:
59+
error = f"Error: Did you start a web server (ex: 'python3 -m http.server')?"
60+
log(error)
61+
else:
62+
error = f"Error: {req.status} - {req.text}"
63+
64+
def log(message):
65+
document["log"].value += f"{next(cnt):03}: {message} \n"
66+
67+
def add_log_sep():
68+
log_text = document["log"].value
69+
if not log_text:
70+
return
71+
else:
72+
document["log"].value += "======================================\n"
73+
74+
def clear_logs(evt):
75+
global cnt
76+
cnt = counter() # Reset counter
77+
document["log"].value = ""
78+
79+
def ajax_get(evt):
80+
add_log_sep()
81+
log("Before async get")
82+
ajax.get("/api.txt", oncomplete=on_complete)
83+
log("After async get")
84+
85+
def ajax_get_blocking(evt):
86+
add_log_sep()
87+
log("Before blocking get")
88+
try:
89+
ajax.get("/api.txt", blocking=True, oncomplete=on_complete)
90+
except Exception as exc:
91+
error = f"Error: {exc.__name__} - Did you start a web server (ex: 'python3 -m http.server')?"
92+
log(error)
93+
else:
94+
log("After blocking get")
95+
96+
document["get-btn"].bind("click", ajax_get)
97+
document["get-blocking-btn"].bind("click", ajax_get_blocking)
98+
document["clear-logs-btn"].bind("click", clear_logs)
99+
</script>
100+
</body>
101+
102+
</html>

brython/async/fetch/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Real Python

brython/async/fetch/index.html

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!DOCTYPE html >
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<link
6+
rel="stylesheet"
7+
href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.min.css"
8+
integrity="sha256-jYujp4Kf07YDuUF9i1MHo4AnpXUKuHxIUXH7CrHxdKw="
9+
crossorigin="anonymous" />
10+
<script
11+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js" defer>
12+
</script>
13+
<script
14+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js" defer>
15+
</script>
16+
<style>
17+
body { padding: 30px; }
18+
aside {
19+
background: lightyellow;
20+
margin: 1em 0;
21+
padding: .3em 1em;
22+
border-radius: 3px;
23+
border: 1px solid gray;
24+
color: gray;
25+
}
26+
</style>
27+
</head>
28+
<body onload="brython(1)">
29+
<h2>Fetch Requests</h2>
30+
<aside><p>Demonstrate the usage of GET using <tt>javascript.fetch</tt>.
31+
You need to start a server for this example to work.
32+
You can start the Python development web server with the following command:
33+
<tt>python3 -m http.server</tt>.
34+
</p></aside>
35+
<form class="pure-form" onsubmit="return false;">
36+
<legend>Actions</legend>
37+
<button id="get-btn" class="pure-button pure-button-primary">Fetch Get</button>
38+
<button id="clear-logs-btn" class="pure-button">Clear Logs</button>
39+
<legend>Logs</legend>
40+
<textarea id="log" name="log" rows="20" cols="60"></textarea>
41+
</form>
42+
<script type="text/python">
43+
from browser import ajax, document, window
44+
45+
def counter():
46+
x = 1
47+
while True:
48+
yield x
49+
x += 1
50+
51+
cnt = counter()
52+
53+
def on_complete(req):
54+
if req.status == 200:
55+
log(f"Text received: '{req.text}'")
56+
elif req.status == 0:
57+
error = f"Error: Did you start a web server (ex: 'python3 -m http.server')?"
58+
log(error)
59+
else:
60+
error = f"Error: {req.status} - {req.text}"
61+
62+
def log(message):
63+
document["log"].value += f"{next(cnt):03}: {message} \n"
64+
65+
def add_log_sep():
66+
log_text = document["log"].value
67+
if not log_text:
68+
return
69+
else:
70+
document["log"].value += "======================================\n"
71+
72+
def clear_logs(evt):
73+
global cnt
74+
cnt = counter() # Reset counter
75+
document["log"].value = ""
76+
77+
def fetch_result(response):
78+
log(response.status)
79+
response.text().then(lambda text: log(text))
80+
81+
def fetch_get(evt):
82+
add_log_sep()
83+
log("Before fetch get")
84+
window.fetch("/api.txt").then(fetch_result)
85+
log("After async get")
86+
87+
document["get-btn"].bind("click", fetch_get)
88+
document["clear-logs-btn"].bind("click", clear_logs)
89+
</script>
90+
</body>
91+
92+
</html>

brython/wasm/op/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

brython/wasm/op/Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

brython/wasm/op/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "op"
3+
version = "0.1.0"
4+
authors = ["Andre <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type=["cdylib"]
9+
10+
[dependencies]

brython/wasm/op/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[no_mangle]
2+
pub extern fn add(x: u32, y: u32) -> u32 {
3+
x + y
4+
}

0 commit comments

Comments
 (0)