ADVERTISEMENT

Sports

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

ADVERTISEMENT


In a keynote address at PyCon 2022 in 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 been looking for a way to write Python, instead of JavaScript, to work in web browsers, and various efforts have been made in this direction over the years. Wang announced PyScript as a new framework, built on top of one of these previous 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). Furthermore, he gave quite revealing demonstrations as part of his presentation.

Wang began by introducing himself and the company he manages, 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 implementation.

The distribution includes data science packages suitable for Windows, Linux and macOS. It is developed and operated 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 for a fee.

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 are to continue expanding the user base.

Anaconda has created a number of different tools widely used in the community and also founded the nonprofit NumFOCUS and 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 likens 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 screw in warp pods that allow the code to run faster than C or C ++ in some cases, Wang said.

This 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’s missing

While it is true that Anaconda aims to provide a Python distribution, it is no less true that installing everything needed for Python is too difficult. There is a huge number of packages on 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’re all around 80 percent, he said, which means people have a bad experience 20 percent of the time, which doesn’t really matter. “not great”.

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. It is not possible to create a Windows application, the most popular corporate 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 your work with others. To those who see Docker as a solution to this problem, Wang replies that when you build an application with Docker, you pack a hard drive and send it to someone. This cannot be our way of convincing 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 connected all of these things. Much of what we do in computer science 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 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 of this?

Python and WebAssembly

The web browser 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 many browsers on the desktops. JavaScript is the king of some language popularity surveys, as it is the browser’s native language. So if we want to move into this area, he said, WebAssembly (or Wasm) is clearly the right answer.

WebAssembly is a fundamental turning point. This is a virtual CPU instruction set that has recently become a W3C standard; it has a 32-bit address space and can perform 64-bit arithmetic operations. There is a compilation tool, Emscripten, which can be used to compile most of the 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 have compiled much of Python’s science and number stack to address WebAssembly.

By accessing the Pyodide site, you can get a Python Read-Evaluate-Print (REPL) loop in your browser. From these three cute angle brackets you can import NumPy and panda. 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 worked hard to get CPython to work with WebAssembly. It will soon be a supported Tier 2 platform for CPython, Wang said. WebAssembly simply provides another processing architecture, besides x86, Arm, and others, that the CPython project can address.

Python

Wang and other Anaconda members looked at the work and thought about how to make it more accessible to many more people. To this end, Wang announced PyScript, but he did so by coding a live “hello world” demo from the conference stage. It was his first PyCon lecture, possibly his last, “he said with a laugh, as he wrote a short HTML file that loaded a pyscript.js file from pyscript.net into a tag.

1
2
3

<py-script>
   print("Hello PyCon 2022!")
</py-script>

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

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

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

; it also used the asyncio module for the one second timeout, so it eliminated the

and put it all in a loop.

PyScript is therefore 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 from JavaScript. So all the logic and application code can be in one language, in the browser, without the need for a web server. You can put the HTML file on a USB stick and give it to a friend. You need to download PyScript itself, but this is done from the HTML file using the file.

PyScript isn't some sort of fork of CPython, it's the same code that participants ran on their laptops and servers, Wang said, just compiled for Wasm. It includes all of the work 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 a serverless computer.

Source: LWN

And she ?

What is your opinion on PyScript?

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

See also:

Version 3.2 of Django framework is available, with automatic detection of AppConfig, brings new decorators to the admin module

Python 3.11 will improve the location of errors in tracebacks and bring new features

Python 3.11 will boost performance at the cost of a little more memory, speed gains appear to be between 10% and 60%

Linux kernel version 5.17 is available, which 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 tools, compiler performance and standard library

ADVERTISEMENT

Leave a Comment