PyScript, the framework for building advanced Python apps in the browser, allows you to embed Python and HTML


In a keynote address at PyCon 2022 Salt Lake City, Utah, Peter Wang, President and CEO of Anaconda, Inc. introduced another newcomer to Python interpreters in browsers. The Python community has long sought a way to be able to write Python, rather than JavaScript, to run in web browsers, and there have been several efforts in this direction over the years. Wang announced PyScript as a new framework, based on one of these earlier projects, to allow the creation of Python scripts directly in the browser.

These programs have access to much of the existing Python ecosystem and can interact directly with the browser’s Document Object Model (DOM). In addition, he gave quite revealing demonstrations as part of his presentation.

Wang began by introducing himself and the company he runs, Anaconda, which he co-founded with Travis Oliphant ten years ago. Oliphant is the creator of NumPy and one of the founders of SciPy, both of which are cornerstones of the Python scientific computing ecosystem. Anaconda is a distribution of the Python and R programming languages ​​for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.), which aims to simplify the management and deployment of packages .

The distribution includes data science packages suitable for Windows, Linux, and macOS. It is developed and maintained by Anaconda, Inc., which was founded by Peter Wang and Travis Oliphant in 2012. As a product of Anaconda Inc., it is also known as Anaconda Distribution or Anaconda Individual Edition, while the company’s other products are Anaconda Team. Edition and Anaconda Enterprise Edition, both paid.

There are several reasons why Anaconda and Oliphant focus their efforts on Python, including the fact that this language is accessible even to those with no computer science background. Another plus point is that the Python community is generally welcoming and fun to work with. This is a very important point if we want to continue expanding the user base.

Anaconda has created a number of different tools that are widely used in the community, and also founded the non-profit conferences NumFOCUS and PyData.

But there is another aspect of the language that makes it so desirable from his point of view: it can be extended with binary extensions that use an API written in C, but accessible from other languages. Wang compares Python to a Honda Civic with mounting bolts for a warp drive. So kids can learn the language and then they can open the trunk and drop warp capsules that allow code to run faster than C or C++ in some cases, Wang said.

This aspect is sometimes overlooked, but it means that Python can be used in a way that other similar languages ​​cannot. It’s not just like Node, it’s not just a Ruby alternative. The reason Wall Street firms took over Python 10 or 15 years ago was this warpability, he said.

What’s missing

While it is true that Anaconda aims to provide a distribution of Python, it is no less true that installing everything necessary for Python is too difficult. There are a lot of packages in the Python Package Index (PyPI), but it’s hard to get them to work together. 20% of Python programmers have little experience with the language. There are many different tools to help with this problem, but they all hover around the 80% mark, he said, meaning people have a bad experience 20% of the time, which doesn’t really matter.

It is strange that for the most popular language in the world, such as Python, it is difficult to write and distribute applications with a user interface. For example, you can’t write iOS apps with Python. You can’t create an app for Windows, the most popular business desktop, with one user interface; Even if you’re using a web front-end, you still need to write JavaScript, CSS and HTML, Wang said.

However, the consequences of these two points, namely the difficulties associated with compiling and creating user interfaces, make it difficult to share your work with others. To those who see Docker as a solution to this problem, Wang replies that when you make an application with Docker, you pack up a hard drive and send it to someone. This cannot be our way of getting millions of people to use this material.

To a large extent, Python is a victim of its own success. It’s a great binding language, but that means it’s bound to all of these things. Much of what we do in computing is tied to the ideas and architectures of the 1970s and 1980s, he said, beginning with the C language and the Unix process model; it also includes things like toolchains and internetworking protocols like TCP/IP. The basics of the Python language itself can be taught to anyone in a weekend, he said, but it takes a lot more effort to get them to the point where they can create an executable for Windows or an iOS app for an iPad. . Can we free Python from all this?

Python and WebAssembly

The web browser has clearly won the OS war, Wang said. He doesn’t know if 2022 will be the year of the Linux desktop (it won’t), but he knows there will be plenty of browsers on desktops. JavaScript is the king of some language popularity polls as it is the native language of the browser. So if we want to move forward in this area, he said, WebAssembly (or Wasm) is clearly the right answer.

WebAssembly is a fundamental game changer. This is a virtual CPU instruction set that recently became a W3C standard; it has a 32-bit address space and can perform 64-bit arithmetic. There is a build tool, Emscripten, that can be used to compile most C and C++ code into WebAssembly, which can then be run in the browser. According to Wang, WebAssembly is well supported by browsers, including mobile browsers.

CPython is, of course, a C program, and much of the Python digital stack is written in C or C++. In recent years, projects like JupyterLite compiled much of Python’s scientific and numerical stack to target WebAssembly.

By going to the Pyodide site, it is possible to get a Python read-eval-print (REPL) loop in your browser. From these three nice corner brackets it is possible to import NumPy and pandas. From the JupyterLite site, it is possible to get a notebook in the browser by running JupyterLab on the local system.

Christian Heimes, Lead Python Developer, has given talks and done a lot of work to get CPython working with WebAssembly. It will soon be a Level 2 compatible platform for CPython, Wang said. WebAssembly simply provides another computing architecture, beyond x86, Arm and others, that the CPython project can target.

pyscript

Wang and other Anaconda members reviewed their work and thought of ways to make it more accessible to many more people. To this end, Wang announced PyScript, but he did so by coding a “hello world” demo live from the conference stage. It was his first PyCon conference, maybe his last,” he laughed, as he wrote a short HTML file that loaded a pyscript.js file from pyscript.net into a tag.

1
2
3


   print("Hello PyCon 2022!")

Then he double-clicked the file and the greeting appeared in a browser tab; which was greeted with a round of applause. But it's all HTML, she said, so she wrapped the above code in a tag and reloaded. These days, of course, the tag has been removed from HTML, perhaps unfortunately; now I have to explain to the children that there is no beacon.

So you added the blink functionality to the PyScript code and demonstrated a few other things. He created a

with a name, which it then pointed to write the string accessing the DOM to retrieve the object for the

; also used the asyncio module to turn off the time for a second, then removed the

and put everything in a loop.

Thus, PyScript is a framework for building rich Python applications in the browser. It allows for Python and HTML embedding, provides full DOM access, and provides code access to JavaScript libraries, in both directions. Python code can call JavaScript or be called by JavaScript. Therefore, all the logic and code of the application can be in one language, in the browser, without the need for a web server. It is possible to put the HTML file on a USB key and give it to a friend. It is necessary to download the PyScript itself, but this is done from the HTML file using the .

PyScript isn't some kind of fork of CPython, it's the same code the participants were running on their laptops and servers, Wang said, freshly compiled for Wasm. It includes all the work that Pyodide has done to make major digital, scientific, and big data packages work for Wasm as well. PyScript is an opinion framework that provides an external function interface (FFI) to communicate with JavaScript and the DOM; Python has already integrated C, C++, and Fortran, so JavaScript can be added to the list as well. It really is serverless computing.

Font : LWN

And you ?

What is your opinion on PyScript?

Do you think PyScript can be as good as JavaScript to run in a web browser?

See also :

Version 3.2 of the Django framework is available, with automatic discovery of AppConfig, it brings new decorators for the admin module.

Python 3.11 will improve traceback error location and bring new features

Python 3.11 will gain performance at the cost of a bit more memory, speed gains seem to be between 10% and 60%

Linux kernel version 5.17 is out, it brings very important security fixes and a major improvement to the random number generator.

Version 1.15 of the Go programming language will be released soon, with improvements to tools, compiler performance, and the standard library.

Leave a Comment