A new release of "Python for browsers" is out, meet Brython 3.9



Brython Python code (screenshot - version 3.8.8)



The developers of the Brython Python project have released a new release, Brython 3.9 . The project itself is positioned as "Python for the browser". It is based on Python 3 and runs on the browser side, using Python instead of JavaScript to develop web scripts.



The project code is written in Python and distributed under the BSD license. The new release is compatible with Python 3.9, plus the standard library has been updated.



In order to define the logic of the client-side site using Python instead of JS, the web developer needs to include two libraries, brython.js and brython_stdlib.js. To include Python code on a page, a script tag with a mime type text / python is used .



From the Brython side, it is allowed to both embed code into the page and load external scripts. Example:



<script type="text/python" src="test.py">


Full access to DOM elements and events is provided from the script. In addition to accessing the Python standard library, there are specialized libraries for working with the DOM and JavaScript libraries such as jQuery, D3, Highcharts, and Raphael.



It supports the use of CSS frameworks by JavaScript libraries such as jQuery, D3, Highcharts, and Raphael.



Python blocks are executed from script blocks through code pre-compilation, which is executed by the Brython engine after the page has loaded. Compilation is started by calling the brython () function , for example by appending body onload="brython()".



Based on the Python code, a JS representation is formed, then it is executed by the standard JavaScript engine of the browser.



In terms of performance, most of the operations in embedded Python scripts are close to Cpython's performance. The only time a delay occurs is during the compilation phase. But the latency can be eliminated by precompiled JavaScript code, which is used to speed up the loading of the standard library.



<script type="text/python">
 
import time
import math
 
from browser import document
import browser.timer
 
content = document["content"]
 
...
 
canvas = content.select_one(".clock")
 
if hasattr(canvas, 'getContext'):
    ctx = canvas.getContext("2d")
 
    browser.timer.set_interval(set_clock, 100)
    show_hours()
else:
    content.select_one('.navig_zone').html = "canvas is not supported"
 
</script>


More on running Brython



In order for "Python for Browser" to be able to both translate and execute Python code like JS code, you need to call Brython when the document body is loaded.



<body onload="brython()">


In this case, the tag looks for script tags of type text / python and runs the code. In order to use the API for working with the web, the Brython developers implemented a simple solution - importing the API, similar to importing any other module in Python:



from browser import document, html, window


Brython handles everything on its own, so you don't need to run additional commands.



An important point - to work with Brython, you need experience with JavaScript, at least a basic level. Without this, it will be quite difficult to figure it out.






All Articles