Skip to content

Commit 6904710

Browse files
committed
Added Vue example
Perf on storage Brython import and module.js
1 parent a0c4107 commit 6904710

File tree

12 files changed

+36217
-7
lines changed

12 files changed

+36217
-7
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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"
12+
integrity="sha256-U56d9Sn/Gtf1meSBXazW81LM1bUeyc1jFuoY3CBu6A8="
13+
crossorigin="anonymous">
14+
</script>
15+
<script
16+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js"
17+
integrity="sha256-twMHOlRBOpBkpyGFeXIBWoJqTpqf76Zp8HExOelwyRc="
18+
crossorigin="anonymous"
19+
defer>
20+
</script>
21+
<script src="main.py" type="text/python" defer></script>
22+
<style>
23+
body { padding: 30px; }
24+
</style>
25+
</head>
26+
<body onload="brython()">
27+
<form class="pure-form" onsubmit="return false;">
28+
<fieldset>
29+
<legend>Base64 Calculator</legend>
30+
<input type="text" id="text-src" placeholder="Text to Encode" autocomplete="off"/>
31+
<button type="submit" id="submit" class="pure-button pure-button-primary">Ok</button>
32+
<button id="clear-btn" class="pure-button">Clear</button>
33+
</fieldset>
34+
</form>
35+
<div id="b64-display"></div>
36+
</body>
37+
</html>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from browser import aio, document, prompt, html, alert
2+
from browser.local_storage import storage
3+
import json, base64
4+
5+
def load_data():
6+
b64_map = {}
7+
for key in storage.keys():
8+
b64_map[key] = storage.get(key)
9+
return b64_map
10+
11+
def save_data(key, value):
12+
storage[key] = value
13+
14+
def base64_compute(evt):
15+
key = document["text-src"].value
16+
if not key:
17+
alert("You need to enter a value")
18+
return
19+
if key in b64_map:
20+
alert(f"The base64 value of '{key}' already exists: '{b64_map[key]}'")
21+
return
22+
b64data = base64.b64encode(key.encode()).decode()
23+
b64_map[key] = b64data
24+
display_map()
25+
save_data(key, b64data)
26+
27+
def clear_map(evt):
28+
b64_map.clear()
29+
storage.clear()
30+
document["b64-display"].clear()
31+
32+
def display_map():
33+
if not b64_map:
34+
return
35+
table = html.TABLE(Class="pure-table")
36+
table <= html.THEAD(html.TR(html.TH("Text") + html.TH("Base64")))
37+
table <= (html.TR(html.TD(key) + html.TD(b64_map[key])) for key in b64_map)
38+
base64_display = document["b64-display"]
39+
base64_display.clear()
40+
base64_display <= table
41+
document["text-src"].value = ""
42+
43+
b64_map = load_data()
44+
display_map()
45+
document["submit"].bind("click", base64_compute)
46+
document["clear-btn"].bind("click", clear_map)
47+
48+

brython/import/functional.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# main.py
2+
3+
import itertools
4+
5+
def take(n, iterable):
6+
"Return first n items of the iterable as a list"
7+
return list(itertools.islice(iterable, n))

brython/import/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js" async></script>
5+
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js" async></script>
6+
</head>
7+
<body onload="brython()">
8+
9+
<script type="text/python">
10+
from browser import document, html, window
11+
import sys
12+
import functional
13+
14+
selection = functional.take(10, range(10000))
15+
numbers = ', '.join([str(x) for x in selection])
16+
17+
document <= html.P(f"{sys.version=}")
18+
document <= html.P(f"{numbers=}")
19+
</script>
20+
</body>
21+
</html>

brython/import_js/README.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
To run the demo, you can open the file demo.html from the browser "File/Open..." menu.
2+
3+
Another option is to start the built-in Python HTTP server by
4+
5+
python -m http.server
6+
7+
The default port is 8000. To specify another port:
8+
9+
python -m http.server 8080
10+
11+
Then load http://localhost:<port>/demo.html in the browser address bar.
12+
13+
For more information please visit http://brython.info.

0 commit comments

Comments
 (0)