|
1 | 1 | # Brython Examples |
2 | 2 |
|
3 | | -This set of example is intended to accompany the [Real Python](https://realpython.com/) tutorial [Brython: Python in Your Browser](https://realpython.com/brython-python-in-browser). |
| 3 | +This set of examples is intended to accompany the [Real Python](https://realpython.com/) tutorial [Brython: Python in Your Browser](https://realpython.com/brython-python-in-browser). |
4 | 4 |
|
5 | | -Most of the examples are lined in the article and others are more complete examples going beyond the scope of the Brython tutorial. |
| 5 | +Most of the examples are found in the article and others are extras that did not fit in the scope of the article. |
6 | 6 |
|
7 | 7 | ## async |
8 | 8 |
|
9 | | -* Project demonstrating how to use Brython asynchronous functions to request data from a server API. |
| 9 | +* Project demonstrating how to use Brython asynchronous functions to request data from a server API |
10 | 10 | * In the following examples, the API is a simple text file that returns the text "Real Python" |
11 | 11 | * To test the examples, execute a local web server in the respective directory |
12 | 12 | * The recommended Python development server is started with: `python -m http.server` |
| 13 | +* https://realpython.com/brython-python-in-browser/#asynchronous-development-in-brython |
13 | 14 |
|
14 | 15 | ### aio |
15 | 16 |
|
16 | | -* Demonstrates the usage of `browser.aio` the substitute to `asyncio` in Brython. |
| 17 | +* Demonstrates the usage of `browser.aio` the substitute to `asyncio` in Brython |
| 18 | +* This example is in the tutorial: https://realpython.com/brython-python-in-browser/#async-io-in-brython |
17 | 19 | * https://www.brython.info/static_doc/en/aio.html |
18 | 20 |
|
19 | 21 | ### ajax |
20 | 22 |
|
21 | | -* Demonstrates the usage of `browser.ajax`. |
| 23 | +* Demonstrates the usage of `browser.ajax` |
| 24 | +* This example is in the tutorial: https://realpython.com/brython-python-in-browser/#ajax-in-brython |
22 | 25 | * https://www.brython.info/static_doc/en/ajax.html |
23 | 26 |
|
24 | 27 | ### fetch |
25 | 28 |
|
26 | | -* Demonstrates the usage of JavaScript `fetch` from Brython. |
27 | | -* https://www.brython.info/static_doc/en/ajax.html |
| 29 | +* Demonstrates the usage of JavaScript `fetch` from Brython |
| 30 | +* This example is not in the tutorial and provided as an extra. It is related to section: https://realpython.com/brython-python-in-browser/#asynchronous-development-in-brython |
28 | 31 |
|
29 | 32 | ## base64 |
30 | 33 |
|
31 | 34 | 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. |
32 | 35 |
|
33 | 36 | ### embed |
34 | 37 |
|
35 | | -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. |
36 | | - |
37 | | -The user enters the string to be encoded through the standard prompt message box of the browser. |
38 | | - |
39 | | -### form |
| 38 | +* 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 |
| 39 | +* The user enters the string to be encoded through the standard prompt message box of the browser |
| 40 | +* This example is the same as the following one but with the Python code embedded in HTML |
| 41 | +* It is not in the tutorial in this format, but relates to https://realpython.com/brython-python-in-browser/#the-dom-api-in-brython |
40 | 42 |
|
41 | | -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`). |
| 43 | +### sep |
42 | 44 |
|
43 | | -The user enters the string in the HTML form of the main page. |
| 45 | +* This project is the same as `embed` but the Python code is a separate file, `main.py`. |
| 46 | +* A separate Python file requires to start a local server to test this example (`python3 -m http.server`) |
44 | 47 |
|
45 | | -### sep |
| 48 | +### form |
46 | 49 |
|
47 | | -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`) |
| 50 | +* 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`) |
| 51 | +* The user enters the string in the HTML form of the main page |
| 52 | +* You can find this example in the tutorial: https://realpython.com/brython-python-in-browser/#the-dom-api-in-brython |
48 | 53 |
|
49 | 54 | ### storage |
50 | 55 |
|
51 | | -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`). |
52 | | - |
53 | | -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. |
| 56 | +* 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`). |
| 57 | +* 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. |
| 58 | +* This example is documented in the tutorial: https://realpython.com/brython-python-in-browser/#browser-web-api |
54 | 59 |
|
55 | | -### storate_perf |
| 60 | +### storage_perf |
56 | 61 |
|
57 | | -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. |
| 62 | +* 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. |
| 63 | +* This example is an extra and not described in the tutorial but is related to https://realpython.com/brython-python-in-browser/#browser-web-api |
58 | 64 |
|
59 | 65 | ## chrome_extensions |
60 | 66 |
|
61 | 67 | ### hello_js |
62 | 68 |
|
63 | | -Example of a JavaScript Google Chrome extension. |
| 69 | +* Example of a JavaScript Google Chrome extension |
| 70 | +* In the tutorial https://realpython.com/brython-python-in-browser/#hello-world-extension-in-js |
64 | 71 |
|
65 | 72 | ### hello_py |
66 | 73 |
|
67 | | -Same example as hello_js using Brython. |
| 74 | +* Same example as hello_js using Brython |
| 75 | +* In the tutorial https://realpython.com/brython-python-in-browser/#hello-world-extension-in-python |
68 | 76 |
|
69 | 77 | ## console |
70 | 78 |
|
71 | | -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. |
| 79 | +* 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. |
| 80 | +* This is an extra not described in the tutorial |
| 81 | +* Check out https://brython.info/console.html to see it online |
72 | 82 |
|
73 | 83 | ## github_install |
74 | 84 |
|
75 | | -`index.html` loading the Brython engine from GitHub. You can open the file directly. It only displays a message box with "Hello Real Python". |
| 85 | +* `index.html` loading the Brython engine from GitHub. You can open the file directly. It only displays a message box with "Hello Real Python" |
| 86 | +* In the tutorial https://realpython.com/brython-python-in-browser/#github-install |
76 | 87 |
|
77 | 88 | ## hashes |
78 | 89 |
|
79 | | -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). |
80 | | - |
81 | | -This serves as the basis for a translation to the same application with Vue.js (see **vuejs** project below). |
82 | | - |
83 | | -It requires a local webserver. |
| 90 | +* 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). |
| 91 | +* This serves as the basis for a translation to the same application with Vue.js (see **vuejs** project below) |
| 92 | +* It requires a local webserver |
| 93 | +* This is an extra not described in the tutorial, but serves the bases as the Vue.js example and you can read about it at https://realpython.com/brython-python-in-browser/#web-ui-framework |
84 | 94 |
|
85 | 95 | ## import |
86 | 96 |
|
87 | | -Shows how to import an external Python module. The external module is `functional.py`. The main Python code is embedded in the HTML page. |
88 | | - |
89 | | -It requires a local webserver. |
| 97 | +* Shows how to import an external Python module. The external module is `functional.py`. The main Python code is embedded in the HTML page. |
| 98 | +* It requires a local webserver |
| 99 | +* Read about it in the tutorial at https://realpython.com/brython-python-in-browser/#import-in-brython |
90 | 100 |
|
91 | 101 | ## import_js |
92 | 102 |
|
93 | | -Expands on the `import` example by allowing the creation of `brython_module.js` generated with `brython-cli --modules`. |
94 | | - |
95 | | -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`. |
96 | | - |
97 | | -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`). |
| 103 | +* Expands on the `import` example by allowing the creation of `brython_module.js` generated with `brython-cli --modules`. |
| 104 | +* 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`. |
| 105 | +* 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`). |
| 106 | +* You can read more about this approach in the tutorial at https://realpython.com/brython-python-in-browser/#reduce-import-size |
98 | 107 |
|
99 | 108 | ## npm_install |
100 | 109 |
|
101 | | -Example of a Brython project installed with `npm`. See the corresponding tutorial section for more details. |
| 110 | +* Example of a Brython project installed with `npm`. See the corresponding tutorial section for more details. |
| 111 | +* Correspond to section https://realpython.com/brython-python-in-browser/#npm-install |
102 | 112 |
|
103 | 113 | ## pip_install |
104 | 114 |
|
105 | | -Example of a Brython project installed with `pip`. See the corresponding tutorial section for more details. |
| 115 | +* Example of a Brython project installed with `pip` |
| 116 | +* Tutorial section: https://realpython.com/brython-python-in-browser/#pypi-install |
106 | 117 |
|
107 | 118 | ## sha256 |
108 | 119 |
|
109 | | -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. |
| 120 | +* 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. |
| 121 | +* This is an extra not described in the tutorial, but serves the bases as the Vue.js example and you can read about it at https://realpython.com/brython-python-in-browser/#web-ui-framework |
110 | 122 |
|
111 | 123 | ## vuejs |
112 | 124 |
|
113 | | -Brython and Vue.js implementation of `hashes`. Requires a local webserver to be running. |
| 125 | +* Brython and Vue.js implementation of `hashes`. Requires a local webserver to be running. |
| 126 | +* The Vue.js example is documented at https://realpython.com/brython-python-in-browser/#web-ui-framework |
114 | 127 |
|
115 | 128 | ## wasm |
116 | 129 |
|
117 | | -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. |
118 | | - |
119 | | -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. |
120 | | - |
121 | | -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. |
| 130 | +* 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. |
| 131 | +* 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. |
| 132 | +* 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. |
| 133 | +* Documented in the tutorial at https://realpython.com/brython-python-in-browser/#webassembly |
122 | 134 |
|
123 | 135 | ## zero_install |
124 | 136 |
|
125 | | -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 :-) |
| 137 | +* 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 :-) |
| 138 | +* In the tutorial at https://realpython.com/brython-python-in-browser/#cdn-server-install |
0 commit comments