Introduction
- Convert
Python
code toJavaScript
, allowing us to write and runPython
code in the browser. - It is possible to realize that python and js codes can call each other.
- Based on Python 3 implementation, supports HTML5 environment (providing DOM objects and event interfaces).
- Supports turtle drawing library, which can draw images. Brython will convert drawing programs into animations in svg format.
- Brython only supports native Python modules, not C built-in Python modules, unless they have been reimplemented in JavaScript.
- Because Brython runs in the context of a web browser, there are some limitations. For example, browsers do not allow direct access to the file system, so os.open() cannot open the file. Web browser-independent features may not be available.
Brython project mirror directory: brython download | SourceForge.net
Using Brython
- 1. Load the
brython.js
library into thesection of the
HTML
page
$ npm install brython <script src="node_modules/brython/brython.js"></script> // Or load the brython library from CDN. There are several CDNs to choose from: <!-- 1 : jsDelivr CDN --> <script src="//i2.wp.com/cdn.jsdelivr.net/npm/[email protected]/brython.min.js"></script> <script src="//i2.wp.com/cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.js"></script> <!-- 2: CloudFlare CDN --> <script src="//i2.wp.com/cdnjs.cloudflare.com/ajax/libs/brython/3.9.1/brython.min.js"></script> <script src="//i2.wp.com/cdnjs.cloudflare.com/ajax/libs/brython/3.9.1/brython_stdlib.min.js"></script> <!-- 3: GitHub as the CDN --> <!--If you want to use the latest development version, you can select this option --> <script src="//i2.wp.com/raw.githack.com/brython-dev/brython/master/www/src/brython.js"></script> <script src="//i2.wp.com/raw.githack.com/brython-dev/brython/master/www/src/brython_stdlib.js"></script>
Of course, you can also download it locally and then load it, so the loading speed will be much faster.
- 2. Translation in Brython occurs while loading the HTML page, so we need to call the function
brython()
in the body tag of the HTML document.
<body onload="brython()">
- 3. The
brython()
function will search for all script tags with the type =”text/python” attribute in the HTML document, thereby translating thePython
code in the tag. Therefore,Python
code must be surrounded bytags:
<script type="text/python"> <!-- Python code --> </script>
Alternatively, download the Python code from a separate file:
<script type="text/python" src="test.py"></script>
Example
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hello World</title> <script type="text/javascript" src="//i2.wp.com/cdn.jsdelivr.net/npm/brython@3.9.1/brython.min.js"> </script> <script type="text/javascript" src="//i2.wp.com/cdn.jsdelivr.net/npm/brython@3.9.1/brython_stdlib.js"> </script> </head> <body onload="brython()" > <script type="text/python"> from browser import document, alert def hello(ev): alert("Open Alert") document["button-click"].bind("click", hello) </script> <button id="button-click"> Open Alert </button> </body> </html>
How to get the execution result of Python code and pass the result information to a DOM element or as a string to a JS script?
<body onload="brython()"> <script type="text/python"> importsys from browser import document, window def _write(*args): document["output"].html + = "".join(args) + "<br>" def __write(*args): document["output"].html + = "<span style='color: red'>" + "".join(args) + "</span>" + "<br>" sys.stdout.write = _write sys.stderr.write = __write def exec_code(ev): exec(document['code'].value) document['run'].bind('click',exec_code) </script> <input id='code' value="print('1')"></i> <button id='run'>RUN</button> <div id='output'></div> </body>
Of course, Brython also has many fancy uses, and its source code also lists many demos. If you have any questions, you can go to the source code to find the answer.
---------------------------------END------------------- --------
Digression
Thank you for watching until the end, I have prepared some benefits for everyone!
Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.
CSDN gift package:The most complete "Python learning materials" on the entire network are given away for free! (Safe link, click with confidence)
1. Python learning routes in all directions
The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.
2. Essential development tools for Python
The tools have been organized for you, and you can get started directly after installation!
3. Latest Python study notes
When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.
4. Python video collection
Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher's ideas in the video, from basic to in-depth.
5. Practical cases
What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.
6. Interview Guide
Resume template
CSDN gift package:The most complete "Python learning materials" on the entire network are given away for free! (Safe link, click with confidence)
If there is any infringement, please contact us to delete it.