Skip to content

Commit b77ee1c

Browse files
committed
Linted all the files
Tested all projects Added a global README
1 parent 2206dad commit b77ee1c

File tree

15 files changed

+14438
-48
lines changed

15 files changed

+14438
-48
lines changed

brython/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.PHONY: lint
2+
lint:
3+
flake8 **/*.py
4+
5+
.PHONY: fmt
6+
fmt:
7+
black .
8+
9+
.PHONY: diff
10+
diff:
11+
black --diff .

brython/README.md

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,121 @@
11
# Brython Examples
22

3-
## Projects
3+
## async
44

5-
* github_install:
6-
* pip_install:
7-
* zero_install:
5+
* Project demonstrating how to use Brython asynchronous functions to request data from a server API.
6+
* In the following examples, the API is a simple text file that returns the text "Real Python"
7+
* To test the examples, execute a local web server in the respective directory
8+
* The recommended Python development server is started with: `python -m http.server`
89

10+
### aio
911

12+
* Demonstrates the usage of `browser.aio` the substitute to `asyncio` in Brython.
13+
* https://www.brython.info/static_doc/en/aio.html
1014

15+
### ajax
16+
17+
* Demonstrates the usage of `browser.ajax`.
18+
* https://www.brython.info/static_doc/en/ajax.html
19+
20+
### fetch
21+
22+
* Demonstrates the usage of JavaScript `fetch` from Brython.
23+
* https://www.brython.info/static_doc/en/ajax.html
24+
25+
## base64
26+
27+
Examples demonstrating how to access the DOM API starting from an app that takes a string as input, generates the base64 encoded value of the string, and displays it on the page. Each example is slightly different as stated in the following sections.
28+
29+
### embed
30+
31+
The application is a single `index.htm` with embedded Python code. It can be executed by opening the file with an internet browser. Starting a local web server is not required.
32+
33+
The user enters the string to be encoded through the standard prompt message box of the browser.
34+
35+
### form
36+
37+
The application is an `index.html` with the Python code in a separate `main.py` file. Starting a local webserver is required (`python3 -m http.server`).
38+
39+
The user enters the string in the HTML form of the main page.
40+
41+
### sep
42+
43+
This project is the same as `embed` but the Python code is a separate file, `main.py`. A separate Python file requires to start a local server to test this example (`python3 -m http.server`)
44+
45+
### storage
46+
47+
This example is an extension of the **form** project demonstrating how to use `localstorage` and save the data between page reload. It requires to start a local web server (`python3 -m http.server`).
48+
49+
The data is saved as a JSON document associated with a single key of the local storage. The performance is degrading as you add more elements in the JSON file.
50+
51+
### storate_perf
52+
53+
In an attempt to overcome the performance issue of the `storage` example, this example saves each base64 encoded value into a separate key. The key is the original string entered by the user.
54+
55+
## chrome_extensions
56+
57+
### hello_js
58+
59+
Example of a JavaScript Google Chrome extension.
60+
61+
### hello_py
62+
63+
Same example as hello_js using Brython.
64+
65+
## console
66+
67+
Brython console as an iframe embedded in an HTML file. Does not require to run a local web server. Opening `index.html` with a browser is sufficient to test it.
68+
69+
## github_install
70+
71+
`index.html` loading the Brython engine from GitHub. You can open the file directly. It only displays a message box with "Hello Real Python".
72+
73+
## hashes
74+
75+
In the same vein as the Base 64 encode application, this one generate the hash, SHA-1, SHA-256 or SHA-512, of a string. It adds a dropdown to select the desired algorithm (SHA-1, SHA-256, or SHA-512).
76+
77+
This serves as the basis for a translation to the same application with Vue.js (see **vuejs** project below).
78+
79+
It requires a local webserver.
80+
81+
## import
82+
83+
Shows how to import an external Python module. The external module is `functional.py`. The main Python code is embedded in the HTML page.
84+
85+
It requires a local webserver.
86+
87+
## import_js
88+
89+
Expands on the `import` example by allowing the creation of `brython_module.js` generated with `brython-cli --modules`.
90+
91+
This requires a Python virtual environment with Brython installed (`pip install brython`) to have `brython-cli` available in the PATH. The generated files are available in the sub-directory `dist_example`.
92+
93+
You can open `dist_example/index.html` with a browser, without the need for a webserver to run locally, because the dependencies are only JS files (`brython.js` and `brython_modules.js`).
94+
95+
## npm_install
96+
97+
Example of a Brython project installed with `npm`. See the corresponding tutorial section for more details.
98+
99+
## pip_install
100+
101+
Example of a Brython project installed with `pip`. See the corresponding tutorial section for more details.
102+
103+
## sha256
104+
105+
Application to generate the SHA-256 hash of a given string. The data is stored as JSON in a key of the localstorage to preserver the calculations between page reloads.
106+
107+
## vuejs
108+
109+
Brython and Vue.js implementation of `hashes`. Requires a local webserver to be running.
110+
111+
## wasm
112+
113+
An example demonstrating the generation of a WASM file, the loading of the file, and usage of the function from Brython. The source code of the WASM file is Rust.
114+
115+
This requires to have the Rust compiler installed on a local machine. Check the detail in the Brython tutorial. A local webserver is needed as it requires to load the wasm file.
116+
117+
The web server can be started in the directory `wasm/op/web`. The debug wasm file is included. This is only for demonstration. The `add` function does not handle negative and big integers.
118+
119+
## zero_install
120+
121+
An example demonstrating a minimum Brython project. The Brython engine is fetched from a CDN, and the Python code is embedded on the page. No need for a local web server, no need for a local Python environment either, just a browser with JavaScript enabled :-)

brython/base64/form/main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@
33

44
b64_map = {}
55

6+
67
def base64_compute(evt):
78
value = document["text-src"].value
89
if not value:
910
alert("You need to enter a value")
1011
return
1112
if value in b64_map:
12-
alert(f"The base64 value of '{value}' already exists: '{b64_map[value]}'")
13+
alert(
14+
f"The base64 value of '{value}' already exists: '{b64_map[value]}'"
15+
)
1316
return
1417
b64data = base64.b64encode(value.encode()).decode()
1518
b64_map[value] = b64data
1619
display_map()
1720

21+
1822
def clear_map(evt):
1923
b64_map.clear()
2024
document["b64-display"].clear()
2125

26+
2227
def display_map():
2328
table = html.TABLE(Class="pure-table")
2429
table <= html.THEAD(html.TR(html.TH("Text") + html.TH("Base64")))
@@ -28,5 +33,6 @@ def display_map():
2833
base64_display <= table
2934
document["text-src"].value = ""
3035

36+
3137
document["submit"].bind("click", base64_compute)
32-
document["clear-btn"].bind("click", clear_map)
38+
document["clear-btn"].bind("click", clear_map)

brython/base64/sep/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
b64_map = {}
55

6+
67
def base64_compute(evt):
78
default = "Real Python"
89
data = prompt("Enter a string:", default)
@@ -11,12 +12,14 @@ def base64_compute(evt):
1112
b64_map[data] = b64data
1213
display_map()
1314

15+
1416
def display_map():
15-
table = html.TABLE(style={'border': '1 solid grey'})
17+
table = html.TABLE(style={"border": "1 solid grey"})
1618
table <= html.TR(html.TH("Text") + html.TH("Base64"))
1719
table <= (html.TR(html.TD(key) + html.TD(b64_map[key])) for key in b64_map)
1820
base64_display = document["b64-display"]
1921
base64_display.clear()
2022
base64_display <= table
2123

24+
2225
document["b64-btn"].bind("click", base64_compute)

brython/base64/storage/main.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,39 @@
22
from browser.local_storage import storage
33
import json, base64
44

5+
56
def load_data():
6-
data = storage.get('b64data')
7+
data = storage.get("b64data")
78
if data:
89
b64_map = json.loads(data)
910
else:
10-
storage['b64data'] = json.dumps({})
11+
storage["b64data"] = json.dumps({})
1112
b64_map = {}
1213
return b64_map
1314

15+
1416
def base64_compute(evt):
1517
value = document["text-src"].value
1618
if not value:
1719
alert("You need to enter a value")
1820
return
1921
if value in b64_map:
20-
alert(f"The base64 value of '{value}' already exists: '{b64_map[value]}'")
22+
alert(
23+
f"The base64 value of '{value}' already exists: '{b64_map[value]}'"
24+
)
2125
return
2226
b64data = base64.b64encode(value.encode()).decode()
2327
b64_map[value] = b64data
24-
storage['b64data'] = json.dumps(b64_map)
28+
storage["b64data"] = json.dumps(b64_map)
2529
display_map()
2630

31+
2732
def clear_map(evt):
2833
b64_map.clear()
29-
storage['b64data'] = json.dumps({})
34+
storage["b64data"] = json.dumps({})
3035
document["b64-display"].clear()
3136

37+
3238
def display_map():
3339
if not b64_map:
3440
return
@@ -40,9 +46,8 @@ def display_map():
4046
base64_display <= table
4147
document["text-src"].value = ""
4248

49+
4350
b64_map = load_data()
4451
display_map()
4552
document["submit"].bind("click", base64_compute)
4653
document["clear-btn"].bind("click", clear_map)
47-
48-

brython/base64/storage_perf/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
from browser.local_storage import storage
33
import json, base64
44

5+
56
def load_data():
67
b64_map = {}
78
for key in storage.keys():
89
b64_map[key] = storage.get(key)
910
return b64_map
1011

12+
1113
def save_data(key, value):
1214
storage[key] = value
1315

16+
1417
def base64_compute(evt):
1518
key = document["text-src"].value
1619
if not key:
@@ -24,11 +27,13 @@ def base64_compute(evt):
2427
display_map()
2528
save_data(key, b64data)
2629

30+
2731
def clear_map(evt):
2832
b64_map.clear()
2933
storage.clear()
3034
document["b64-display"].clear()
3135

36+
3237
def display_map():
3338
if not b64_map:
3439
return
@@ -40,9 +45,8 @@ def display_map():
4045
base64_display <= table
4146
document["text-src"].value = ""
4247

48+
4349
b64_map = load_data()
4450
display_map()
4551
document["submit"].bind("click", base64_compute)
4652
document["clear-btn"].bind("click", clear_map)
47-
48-
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from browser import document, prompt
22

3+
34
def hello(evt):
45
default = "Real Python"
56
name = prompt("Enter your name:", default)
67
if not name:
78
name = default
89
document["hello"].innerHTML = f"Hello, {name}!"
910

11+
1012
document["hello-btn"].bind("click", hello)

brython/hashes/main.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
from browser import document, prompt, html, alert
2-
import json, hashlib
1+
from browser import document, html, alert
2+
import hashlib
33

44
hashes = {
55
"sha-1": hashlib.sha1,
66
"sha-256": hashlib.sha256,
7-
"sha-512": hashlib.sha512
7+
"sha-512": hashlib.sha512,
88
}
99

10+
1011
def compute_hash(evt):
1112
value = document["text-src"].value
1213
if not value:
@@ -19,17 +20,12 @@ def compute_hash(evt):
1920
hex_value = hash_object.hexdigest()
2021
display_hash(hex_value)
2122

22-
def clear_map(evt):
23-
hash_map.clear()
24-
storage[LOCAL_STORAGE] = json.dumps({})
25-
document["hash-display"].clear()
2623

2724
def display_hash(hex_value):
2825
text = html.P(hex_value)
2926
hash_display = document["hash-display"]
3027
hash_display.clear()
3128
hash_display <= text
3229

33-
document["submit"].bind("click", compute_hash)
34-
3530

31+
document["submit"].bind("click", compute_hash)

brython/import/functional.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import itertools
44

5+
56
def take(n, iterable):
67
"Return first n items of the iterable as a list"
78
return list(itertools.islice(iterable, n))

0 commit comments

Comments
 (0)