PyScript, the framework for creating advanced Python applications in the browser, it allows embedding Python and HTML

PyScript, the framework for creating advanced Python applications in the browser, it allows embedding Python and HTML


In a keynote address at PyCon 2022 Salt Lake City, Utah, Peter Wang, Chairman and CEO of Anaconda, Inc introduced another newcomer to Python interpreters in browsers. The Python community has long searched for a way to be able to write Python, instead of JavaScript, to run in web browsers, and there have been various efforts in this direction over the years. Wang announced PyScript as a new framework, built on one of these earlier projects, to allow Python scripting 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 Python and R programming languages ​​for scientific computing (data science, machine learning applications, large-scale data processing, predictive analytics, etc.), which aims to simplify package management and deployment.

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 of which are 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 without a computer background. Another point in its favor is that the Python community is generally welcoming and fun to work with. This is a very important point if we want to continue to expand the user base.

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

But there is another aspect of the language that makes it so desirable from its 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 engine. So the language can be learned by children who can then open the trunk and bolt on warp pods that allow the 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 Python was taken over by Wall Street firms 10 or 15 years ago was this warp ability, he said.

What is missing

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

It is strange that for the most popular language in the world, as Python would be, it is difficult to write and distribute applications with a user interface. For example, you can’t write iOS apps with Python. You cannot create an application for Windows, the most popular enterprise desktop, with a user interface; even if you’re using a web front-end, you have 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 his work with others. To those who see Docker as a solution to this problem, Wang responds that when you make an application with Docker, you pack up a hard drive and send it to someone. This can’t 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 linking language, but that means it’s linked 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, starting with the C language and the Unix process model; it also includes things like tool chains and interconnect protocols like TCP/IP. The basics of the Python language itself can be taught 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 a iOS app for an iPad. Can we free Python from all this?

Python and WebAssembly

The web browser has clearly won the operating system 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 surveys, as it is the native language of the browser. So if we want to move into 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 operations. There is a build tool, Emscripten, which 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 and JupyterLite have compiled large parts of the scientific and numerical Python stack to target WebAssembly.

By going to the Pyodide site, it is possible to obtain a Python read-evaluate-print (REPL) loop in your browser. From these three nice little 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 to work with WebAssembly. It will soon be a Level 2 supported platform for CPython, Wang said. WebAssembly simply provides another computing architecture, beyond x86, Arm and others, that the CPython project can target.

Python

Wang and other Anaconda members reviewed the work done 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 my last,” he said with a laugh, as he typed out a short HTML file that loaded a pyscript.js file from pyscript.net into a tag.

1
2
3


   print("Hello PyCon 2022!")

He then double-clicked on the file and the greeting appeared in a browser tab; which was greeted with a round of applause. But it's all just HTML, he said, so he 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 he added blinking functionality to the PyScript code and demonstrated a few other things. It created a

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

; he also used the asyncio module to time out for a second, then deleted the

and put the whole thing in a loop.

PyScript is therefore a framework for creating rich Python applications in the browser. It allows embedding Python and HTML, provides full DOM access, and gives code access to JavaScript libraries, in both directions. Python code can call JavaScript or be called by JavaScript. So all the application logic and code can be in a single language, in the browser, no web server needed. It is possible to put the HTML file on a USB key and give it to a friend. It is necessary to download 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 participants were running on their laptops and servers, Wang said, just compiled for Wasm. It includes all of the work that Pyodide has done to make major digital, science, and big data packages work for Wasm as well. PyScript is an opinion framework that provides a Foreign Function Interface (FFI) for talking to JavaScript and the DOM; Python has already integrated C, C++ and Fortran, so JavaScript can also be added to the list. It really is serverless computing.

Source: LWN

And you?

What is your opinion on PyScript?

Do you think PyScript can be as good as JavaScript for running in a web browser?

See as well :

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

Python 3.11 will improve the location of errors in tracebacks 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 available, 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 in tooling, compiler performance and the standard library

Leave a Comment

Your email address will not be published.