|
1 | 1 | # Brython Examples |
2 | 2 |
|
3 | | -## Projects |
| 3 | +## async |
4 | 4 |
|
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` |
8 | 9 |
|
| 10 | +### aio |
9 | 11 |
|
| 12 | +* Demonstrates the usage of `browser.aio` the substitute to `asyncio` in Brython. |
| 13 | +* https://www.brython.info/static_doc/en/aio.html |
10 | 14 |
|
| 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 :-) |
0 commit comments