10 Reasons to Embrace PyScript for Web Development

Published On Sun Oct 06 2024
10 Reasons to Embrace PyScript for Web Development

An Introduction to PyScript - Open Source For You

An open source framework introduced by Anaconda, PyScript is built to run Python from a browser. Though it still has performance issues and a few bugs, it is important to get familiar with it as it promises to be popular in the future.

We are aware that JavaScript client-side coding can be used for server-side coding. Let’s now discuss the reverse — using a server-side language from the browser with the help of Python. But why would one want to execute server-side programs in the browser? The answer is: for enhanced performance, features and convenience. In certain use cases running a code in C is much faster than using JavaScript — for example, for generating quick reports, dealing with heavy prime numbers, image processing or computer vision on-the-fly using the web page, and so on.

PyScript: Enhancing Python in the Browser

PyScript is a framework built to run Python from a browser; in simple terms, it can see Python code as part of HTML page content. PyScript allows the use of CPython based packages like Pandas and NumPy. It provides bi-directional communication between Python and JavaScript objects and namespaces.

Note that this is not something new in the history of Python. There are already modules that support browser side coding such as Brython, Skulpt, PyJS, and a few more. Everything in this world has its own merits and demerits. Brython does support packages if they are written entirely in Python. However, programs that use extensions written in C are not supported. For instance, NumPy, MatPlotLib, and Pandas cannot work with Brython. Skulpt, on the other hand, does not support a few Python features such as import of additional Python modules that are not available by default.

A First Look at PyScript: Python in the Web Browser – Real Python

PyScript has been launched by Anaconda and was officially announced at PyCon US 2022. As rightly stated by Anaconda, its Pyodide, Emscripten and WebAssembly (WASM) that create the magic of PyScript. So let’s look at each of these and see how they help in building PyScript.

WebAssembly (WASM)

As we all know, computers understand only binaries, and the same goes for JavaScript as well. A JavaScript engine is needed to read the code and convert it into binary instructions. WebAssembly or WASM is a language for the web, which is used to code in low level binary format. It can run in modern web browsers. When WebAssembly was originally developed in 2017 by Mozilla, web browsers only supported JavaScript. It quickly became the official World Wide Web Consortium (W3C) standard by 2019. WASM employs a human readable .wat text format language, which gets converted to a binary .wasm format that browsers can run on-the-fly. This allows us to write code in any language such as C, C++, Rust, etc; compile it to WebAssembly and then run in a web browser just like JavaScript. This is the fundamental technology that makes it possible to write websites in Python.

Emscripten

Emscripten, an open source compiler toolchain, enables developers to develop frontends from any programming language. It compiles any portable C/C++ codebase into WebAssembly, using the LLVM (low level virtual machine) compiler. The LLVM project started in 2000 at the University of Illinois at Urbana–Champaign, under the direction of Vikram Adve (IIT Mumbai alumnus) and Chris Lattner. The Emscripten output can be used to run on the web, in Node.js or in WASM runtimes. It focuses on speed, size, and the Web platform.

Pyodide

Pyodide is the foundation of PyScript. It is a Python distribution (a CPython version) for the browser and Node.js, based on WebAssembly. This Mozilla project allows for wide support of Python packages and the ability to run Python in the browser. PyScript uses Pyodide to execute code located in py-script elements directly in the browser. Using Pyodide, we can install and run Python packages in the browser with micropip. Micropip is used to install the given package and all its dependencies. This only works for packages that are either pure Python or for packages with C extensions that are built in Pyodide. Many packages with C extensions have also been ported for use with Pyodide. These include many general-purpose packages like RegEx, PyYAML, lxml and Python packages, including NumPy, Pandas, SciPy, MatPlotLib, and scikit-learn.

Conclusion

So, do we have a new language in PyScript that will replace JavaScript? That’s a million-dollar question. JavaScript is a high-level programming language and one of the core technologies of the World Wide Web. And it is used as a client-side programming language by 97.8 per cent of all websites. However, Python is not far behind in the popularity race and is one of the most liked languages in the world. So definitely, but not immediately, there will be a user space shift from JavaScript to PyScript. This is specifically in the data science world, as it helps build pages with plots easily. Nonetheless, performance is where PyScript lags far behind, but this is just the beginning and I am confident the language will evolve. I am sure we will continue to use loads of JavaScript. It’s not easy or necessary to replace it yet. Having said that, PyScript or Python and other language-based browser scripting will make their own user space too. PyScript is bound to grow using WASM technology.