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
xml Copy code $ 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.
ini Copy code <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:
xml Copy code <script type="text/python"> <!-- Python code --> </script>
Alternatively, download the Python code from a separate file:
xml Copy code <script type="text/python" src="test.py"></script>
Example
xml Copy code <!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?
xml Copy code <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.
github:brython-dev/brython: Brython (Browser Python) is an implementation of Python 3 running in the browser (github.com) https://github.com/brython-dev/brython"
Digression
In this era of rapidly growing technology, programming is like a ticket to a world of infinite possibilities for many people. Among the star lineup of programming languages, Python is like the dominant superstar. With its concise and easy-to-understand syntax and powerful functions, Python stands out and becomes one of the hottest programming languages in the world.
The rapid rise of Python is extremely beneficial to the entire industry, but "There are many popular people and not many people
", which has led to a lot of criticism, but it still cannot stop its popularity. development momentum.
If you are interested in Python and want to learn Python, here I would like to share with you a Complete set of Python learning materials, which I compiled during my own study. I hope it can help you, let’s work together!
Friends in need can click the link below to get it for free or Scan the QR code below to get it for free
CSDN Gift Package: Free sharing of the most complete "Python learning materials" on the entire network(safe link, click with confidence )
?
1Getting started with zero basics
① Learning route
For students who have never been exposed to Python, we have prepared a detailed Learning and Growth Roadmap for you. It can be said to be the most scientific and systematic learning route. You can follow the above knowledge points to find corresponding learning resources to ensure that you learn more comprehensively.
② Route corresponding learning video
There are also many learning videos suitable for beginners. With these videos, you can easily get started with Python~
③Exercise questions
After each video lesson, there are corresponding exercises to test your learning results haha!
2Domestic and foreign Python books and documents
① Documents and books
3Python toolkit + project source code collection
①Python toolkit
The commonly used development software for learning Python is here! Each one has a detailed installation tutorial to ensure you can install it successfully!
②Python practical case
Optical theory is useless. You must learn to type code along with it and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases. 100+ practical case source codes are waiting for you!
③Python mini game source code
If you feel that the practical cases above are a bit boring, you can try writing your own mini-game in Python to add a little fun to your learning process!
4Python interview questions
After we learn Python, we can go out and find a job if we have the skills! The following interview questions are all from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. I believe everyone can find a satisfactory job after reviewing this set of interview materials.
5Python part-time channel
Moreover, after learning Python, you can also take orders and make money on major part-time platforms. I have compiled various part-time channels + part-time precautions + how to communicate with customers into documents.
All the above information , if friends need it, you can scan the QR code below to get it for free
?