Skip to content

Commit 0a5ae99

Browse files
committed
Updated examples to match the current article
1 parent eb1a097 commit 0a5ae99

File tree

21 files changed

+355
-86
lines changed

21 files changed

+355
-86
lines changed

brython/async/aio/main.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from browser import aio, document
2+
import javascript
3+
4+
def log(message):
5+
document["log"].value += f"{message} \n"
6+
7+
async def process_get(url):
8+
log("Before await aio.get")
9+
req = await aio.get(url)
10+
log(f"Retrieved data: '{req.data}'")
11+
return req.data
12+
13+
def aio_get(evt):
14+
log("Before aio.run")
15+
aio.run(process_get("/api.txt"))
16+
log("After aio.run")
17+
18+
document["get-btn"].bind("click", aio_get)

brython/async/aio/simple.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
<script src="main.py" type="text/python" defer></script>
17+
<style>
18+
body { padding: 30px; }
19+
aside {
20+
background: lightyellow;
21+
margin: 1em 0;
22+
padding: .3em 1em;
23+
border-radius: 3px;
24+
border: 1px solid gray;
25+
color: gray;
26+
}
27+
</style>
28+
</head>
29+
<body onload="brython(1)">
30+
<h2>Aio Requests</h2>
31+
<aside><p>Demonstrate the usage of GET using <tt>browser.aio</tt>.
32+
You need to start a server for this example to work.
33+
You can start the Python development web server with the following command:
34+
<tt>python3 -m http.server</tt>.
35+
</p></aside>
36+
<form class="pure-form" onsubmit="return false;">
37+
<legend>Actions</legend>
38+
<button id="get-btn" class="pure-button pure-button-primary">Async Get</button>
39+
<legend>Logs</legend>
40+
<textarea id="log" name="log" rows="20" cols="60"></textarea>
41+
</form>
42+
</body>
43+
44+
</html>

brython/async/ajax/gen_image.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#! /usr/bin/env sh
2+
3+
center=0 # Start position of the center of the first image.
4+
# This can be ANYTHING, as only relative changes are important.
5+
6+
for image in ./images/*
7+
do
8+
9+
# Add 70 to the previous images relative offset to add to each image
10+
#
11+
center=`convert xc: -format "%[fx: $center +10 ]" info:`
12+
13+
# read image, add fluff, and using centered padding/trim locate the
14+
# center of the image at the next location (relative to the last).
15+
#
16+
convert -size 800x800 "$image" -thumbnail 800x800 \
17+
-set caption '%t' -bordercolor black -background black \
18+
-pointsize 12 -density 96x96 +polaroid -resize 200% \
19+
-gravity center -background None -extent 100x100 -trim \
20+
-repage +${center}+0\! MIFF:-
21+
22+
done |
23+
# read pipeline of positioned images, and merge together
24+
convert -background white MIFF:- -layers merge +repage \
25+
-bordercolor black -border 1x1 assortment_team.jpg

brython/async/ajax/index.html

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.min.css"
88
integrity="sha256-jYujp4Kf07YDuUF9i1MHo4AnpXUKuHxIUXH7CrHxdKw="
99
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>
10+
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.0/brython.min.js" defer></script>
11+
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.0/brython_stdlib.min.js" defer></script>
1612
<style>
1713
body { padding: 30px; }
1814
aside {
@@ -27,9 +23,9 @@
2723
</head>
2824
<body onload="brython(1)">
2925
<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:
26+
<aside><p>Demonstrate the usage of GET using <tt>browser.ajax</tt>.
27+
You need to start a server for this example to work.
28+
You can start the Python development web server with the following command:
3329
<tt>python3 -m http.server</tt>.
3430
</p></aside>
3531
<form class="pure-form" onsubmit="return false;">

brython/async/ajax/main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from browser import ajax, document
2+
import javascript
3+
4+
def show_text(req):
5+
if req.status == 200:
6+
log(f"Text received: '{req.text}'")
7+
else:
8+
log(f"Error: {req.status} - {req.text}")
9+
10+
def log(message):
11+
document["log"].value += f"{message} \n"
12+
13+
def ajax_get(evt):
14+
log("Before async get")
15+
ajax.get("/api.txt", oncomplete=show_text)
16+
log("After async get")
17+
18+
def ajax_get_blocking(evt):
19+
log("Before blocking get")
20+
try:
21+
ajax.get("/api.txt", blocking=True, oncomplete=show_text)
22+
except Exception as exc:
23+
log(f"Error: {exc.__name__} - Did you start a web server (ex: 'python3 -m http.server')?")
24+
else:
25+
log("After blocking get")
26+
27+
document["get-btn"].bind("click", ajax_get)
28+
document["get-blocking-btn"].bind("click", ajax_get_blocking)
29+

brython/async/ajax/simple.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.0.9/brython.min.js" defer>
12+
</script>
13+
<script
14+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.0/brython_stdlib.min.js" defer>
15+
</script>
16+
<script src="main.py" type="text/python"></script>
17+
<style>
18+
body { padding: 30px; }
19+
aside {
20+
background: lightyellow;
21+
margin: 1em 0;
22+
padding: .3em 1em;
23+
border-radius: 3px;
24+
border: 1px solid gray;
25+
color: gray;
26+
}
27+
</style>
28+
</head>
29+
<body onload="brython(1)">
30+
<h2>Ajax Requests</h2>
31+
<aside><p>Demonstrate the usage of GET using <tt>browser.ajax</tt>.
32+
You need to start a server for this example to work.
33+
You can start the Python development web server with the following command:
34+
<tt>python3 -m http.server</tt>.
35+
</p></aside>
36+
<form class="pure-form" onsubmit="return false;">
37+
<legend>Actions</legend>
38+
<button id="get-btn" class="pure-button pure-button-primary">Async Get</button>
39+
<button id="get-blocking-btn" class="pure-button pure-button-primary">Blocking Get</button>
40+
<legend>Logs</legend>
41+
<textarea id="log" name="log" rows="20" cols="60"></textarea>
42+
</form>
43+
</body>
44+
</html>

brython/base64/embed/index.html

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
<head>
55
<script
6-
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js"
7-
integrity="sha256-U56d9Sn/Gtf1meSBXazW81LM1bUeyc1jFuoY3CBu6A8="
6+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.0/brython.min.js"
7+
integrity="sha512-6xoV9qvQHPMnIFWzWrRwuq25qpgJ8FiGKekJ+7Bstb8/4XRC98k2c49IpdUVc5QitMNHKWPcFjFUkDxdIjJ7Nw=="
88
crossorigin="anonymous">
99
</script>
1010
<script
11-
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js"
12-
integrity="sha256-twMHOlRBOpBkpyGFeXIBWoJqTpqf76Zp8HExOelwyRc="
13-
crossorigin="anonymous"
14-
defer>
11+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.0/brython_stdlib.min.js"
12+
integrity="sha512-QbMgbJbXnZv0V/+jpdd68nkyOrYwP/UsJAH/TGQmVHgwwrxI7QNKzRn3dKuG3kG/lVWHx5hYXBISk7zX86TInw=="
13+
crossorigin="anonymous">
1514
</script>
1615
<style>
1716
table { border-collapse: collapse; }
@@ -27,15 +26,15 @@
2726

2827
b64_map = {}
2928

30-
def base64_compute(evt):
29+
def base64_compute(_) -> None:
3130
default = "Real Python"
3231
data = prompt("Enter a string:", default)
3332
data = data or default
3433
b64data = base64.b64encode(data.encode()).decode()
3534
b64_map[data] = b64data
3635
display_map()
3736

38-
def display_map():
37+
def display_map() -> None:
3938
table = html.TABLE(style={'border': '1 solid grey'})
4039
table <= html.TR(html.TH("Text") + html.TH("Base64"))
4140
table <= (html.TR(html.TD(key) + html.TD(b64_map[key])) for key in b64_map)

brython/base64/sep/index.html

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<script
5-
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js"
6-
integrity="sha256-U56d9Sn/Gtf1meSBXazW81LM1bUeyc1jFuoY3CBu6A8="
7-
crossorigin="anonymous">
8-
</script>
9-
<script
10-
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js"
11-
integrity="sha256-twMHOlRBOpBkpyGFeXIBWoJqTpqf76Zp8HExOelwyRc="
12-
crossorigin="anonymous"
13-
defer>
14-
</script>
3+
4+
<head>
5+
<script
6+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.0/brython.min.js"
7+
integrity="sha512-6xoV9qvQHPMnIFWzWrRwuq25qpgJ8FiGKekJ+7Bstb8/4XRC98k2c49IpdUVc5QitMNHKWPcFjFUkDxdIjJ7Nw=="
8+
crossorigin="anonymous">
9+
</script>
10+
<script
11+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.9.0/brython_stdlib.min.js"
12+
integrity="sha512-QbMgbJbXnZv0V/+jpdd68nkyOrYwP/UsJAH/TGQmVHgwwrxI7QNKzRn3dKuG3kG/lVWHx5hYXBISk7zX86TInw=="
13+
crossorigin="anonymous">
14+
</script>
1515
<script src="main.py" type="text/python" defer></script>
1616
<style>
1717
table { border-collapse: collapse; }
1818
table, th, td { border: 1px solid grey; }
1919
</style>
20-
</head>
21-
<body onload="brython(1)">
22-
<button id="b64-btn">Base64</button>
23-
<p/>
24-
<div id="b64-display"></div>
25-
</body>
20+
</head>
21+
22+
<body onload="brython(1)">
23+
<button id="b64-btn">Base64</button>
24+
<p/>
25+
<div id="b64-display"></div>
26+
</body>
27+
2628
</html>

brython/base64/sep/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from browser import document, prompt, html
1+
from browser import document, prompt, html # type: ignore
22
import base64
33

44
b64_map = {}
55

66

7-
def base64_compute(evt):
7+
def base64_compute(_) -> None:
88
default = "Real Python"
99
data = prompt("Enter a string:", default)
1010
data = data or default
@@ -13,7 +13,7 @@ def base64_compute(evt):
1313
display_map()
1414

1515

16-
def display_map():
16+
def display_map() -> None:
1717
table = html.TABLE(style={"border": "1 solid grey"})
1818
table <= html.TR(html.TH("Text") + html.TH("Base64"))
1919
table <= (html.TR(html.TD(key) + html.TD(b64_map[key])) for key in b64_map)
File renamed without changes.

0 commit comments

Comments
 (0)