“Wait what?” – I think most readers will react to the headline in that way.
In the sense of “just use Python in a browser”?
Well, the above is a screenshot with the source code of my personal site. Take a look, maybe you will see something new for yourself.
Yes, this is Python!
Development of “Snake” on Brython
In order to try out Brython, I decided to write the classic Snake.
And also author wrote it in less than 5 minutes. We must pay tribute to Chris Deleon, it is very impressive.
So, I added to Chris’ implementation scoring and saving the best result, as well as slightly improved the interface (added a pause button and a button with instructions). Then I ported the game to Brython.
I also modified his code so that it works in mode
strict, since Chris’s implementation uses things like implicit global variables, which, in my opinion, do not reflect how most of the code in JS looks like (I do not criticize the author – he programmed for a while). I wanted to get a good comparison of Brython and JS code.
Although most of the Brython code was a “literal translation” with JS, some parts (for example, scoring functionality) were written directly in Brython and then implemented in JS to look at the differences.
The final result is as follows:
Snake built with Python!
High Score: 0
So, based on this snippet, let’s look at the basic concepts of Brython
No installation is required to use Brython. Just import the script inside
In order for Brython to translate and execute Python code as if it were JS code, we need to call
Brythonjust when the document body loads. For example, like this:
This tag will search for tags.
"text/python"and run their code.
windowrequired in any JS project. Accordingly, Brython should also be able to work with them.
To solve this problem, the creators of Brython could simply give developers the ability to access these objects from Python code, but this would lead to debugger cries about
undefined variableand reduced productivity.
Thus, to use these APIs, we must import them in the same way as we import any other module in Python:
from browser import document, html, window
And you do not need to execute the command
pip install. In the end, you embed it all in HTML! Just add the required imports, Brython will deal with the rest.
To see how well this works, I tried using several different methods from the Web API:
addEventListeneretc. They all worked as they should.
In Snake, as soon as the snake eats an apple, we need to generate a new apple in a random place.
However, I cannot use the random module from the Python * library. So how can I generate a random number (without writing my own library)?
Thanks to the module
In Python, if I want to unzip a list, I can write
list2 = [*list1]. Also, if I want to assign values to a variable based on some condition, I can write
foo = 10 if condition else 20.
[...arr]) and the ternary operator (
let foo = condition ? 10 : 20)
But does Brython support them?
I tried them and they worked great. You can see that list unpacking from Python and conditional assignment are used in my code.
Honestly, I thought that debugging in Brython would be awful.
In fact, everything is not so bad.
Of course, I wrote a very small and not very complicated project, but the errors thrown by Brython were mostly accurate and quite understandable.
This is true, at least with regard to syntax errors. Importing modules from the Python library is a completely different story.
I suspect that in more complex projects, Brython will be several times slower than pure JS.
I really tried to use Brython Editor to convert my Brython code to JS and run the resulting code on a web page, but because of the huge number of errors I have so far refused it. However, I did not put too much effort into this.
Final thoughts on Brython
Honestly, I was very impressed with Brython. Here are a few pros and cons of my own experience work with language:
- I managed to write "Snake" without unnecessary trouble, and the debugging experience was surprisingly positive.
- I appreciate the fact that my code looks cleaner in Python, and I also like that I can use useful Python constructs to write browser code.
- I am pleased to see Python in the source code of my site.
- Brighton is significantly slower than pure JS.
- You will inevitably encounter a lot of mistakes.
- Brython Documentation and his site there is room to grow in terms of ease of navigation and training opportunities
- Brython lacks a strong ecosystem and development tools.
In general, having completed my first project on Brython, I can confidently say that someday I will try again.
Other JS alternatives in browser
However, there are other alternatives.
Pyodide, for example, seems like an interesting option. It compiles Python (along with its scientific libraries) into WebAssembly, which allows it to run in a browser.
WebAssembly, as the name implies, is an assembler for the web. Just as the assembler on our computers can act as an intermediary between high-level languages and machine code, WebAssembly does the same on the web.
Thus, you can write a compiler that will translate Python (or any other language) into WebAssembly, which will allow it to work in the browser.
But honestly, this is a good start!
Learn the details of how to get a sought-after profession from scratch or Level Up in skills and salary by completing SkillFactory paid online courses:
- Profession Web Developer (8 months)
- Python for Web Development Course (9 months)
- Profession UX designer from scratch (9 months)
- Profession Web Designer (Seven months)
- Learning Data Science from scratch (12 months)
- Analyst profession with any starting level (9 months)
- Machine Learning Course (12 weeks)
- DevOps Course (12 months)
- Trends in Data Scenсe 2020
- Data Science is dead. Long live Business Science
- The coolest Data Scientist does not waste time on statistics
- How to Become a Data Scientist Without Online Courses
- 450 free courses from the Ivy League
- Data Science for the Humanities: What is Data
- Steroid Data Scenario: Introducing Decision Intelligence