These languages are known for their flexibility, and they have become the go-to choices for developing web applications, though they also carry the drawback of having older formats.
However, there is a newer development in the coding world – “PyScript”.
Have you ever heard of it? No? Well then,
About PyScript –
Peter Wang, the CEO and co-founder of Anaconda, Inc., revealed it during his keynote speech at PyCon US 2022 that – It is Python inside HTML, a whole-new framework which will enable the users to create Python applications using in browser HTML interface.
It is a better alternative to Emscripten, Pyodide, WASM, and other cutting-edge web technologies which were used earlier in development.
Following are the objectives-
- To offer simple, streamlined APIs.
- Create a system with expandable and pluggable components.
- To fulfil the goals of “Programming for the 99 Percent” and also to use standard HTML which must be supported and extended to read reliable, opinionated bespoke components.
What PyScript has to offer?
Along with helping in specific genres, Pyscript helps data scientists in various conditions. For example, Data scientists frequently create models but are unable to make them representable to stakeholders.
With this scripting language, they will be able to make the most of their talents and cognitive processes and because most models and charts lack an appropriate user interface, it will give targeted audiences a window to consume them.
Key Features of PyScript-
PyScript provides standardized style rules, increased expressiveness, and simplicity of learning. Besides that, let’s take a look at what else this language explores-
- Hosting and support for Python in the browser without the need for servers or settings.
- Support for the ecosystem: Enables the usage of several well-known Python packages, including Pandas, NumPy, and many more.
- Flexibility of the framework: It is a versatile framework that programmers may build upon to quickly construct extendable components in Python.
- Environment Management: Enables programmers to specify which files and packages should be used while writing page code.
- UI Development: Help in quickly creation by using a variety of UI elements, including buttons, containers, and more.
WebAssembly in PyScript-
Writing websites with Python is now feasible thanks to a technique called Web Assembly.
After its initial release in 2017, WebAssembly swiftly attained official status with the World Wide Web Consortium (W3C) by 2019. It contains a.wat text format language that can be read by humans, which is then translated into a binary.wasm format that browsers can read.
We can now create code in any language, compile it to WebAssembly, and then run it in a web browser thanks to this.
A WebAssembly-based Python distribution for the browser and Node.js is called Pyodide. With this, we can use micropip to install and run Python packages in the browser. For usage with Pyodide, several programs with C extensions have also been migrated. Numerous general-purpose Python packages, such as NumPy, pandas, SciPy, Matplotlib, and Scikit-learn, as well as RegEx, PyYAML, and lxml are included.
Emscripten is a full-featured WebAssembly compiler tool-chain that uses LLVM, with an emphasis on performance, size, and the Web platform.
How to Use PyScript-
Pyodide is the base of PyScript, and it is a WebAssembly/Emscripten version of CPython. Micropip-based Python packages may be installed and used in browsers thanks to Pyodide. It employs a binary.wasm format that can be read by browsers after being converted from a human-readable.wat text format language.
You can download the alpha release of PyScript on pyscript.net.
For reference, all HTML files that use PyScript must have a link to these:
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
PyScript allows you to write Python in HTML using the following components:
py-env– Defines the Python packages needed to run your Python code.
py-script– Part of the HTML file where you write your Python code.
py-repl– Creates a REPL component that evaluates the code user enters and displays the result.
Advantages of PyScript –
- The most obvious benefit is that we can now create online apps and write Python (and maybe other languages) in HTML thanks to PyScript. A far larger group of front-end developers and designers may now benefit from Python’s capability, thanks to PyScript.
- The world’s most widely used, portable computing environment is the web browser. Web browsers are available to everyone, whether they are on a computer or a mobile device. This implies that there are no infrastructural restrictions on who may access and begin programming.
- We no longer have to be concerned about deployment thanks to PyScript because of the “architectural shift beyond the cloud”
- Your web browser will be the center of everything. As data scientists, we may distribute our models and dashboards via HTML files that, when opened in a web browser by others, will execute code.
You will, however, concentrate on the browser APIs in this section.
The following tech stack diagram shows Emscripten, an open-source compiler tool chain which make possible to generate WebAssembly from any portable C/C++ source.
The only limitation at this point is that we can only use libraries that Pyodide supports now while using PyScript.
How to build a webpage with PyScript?
To build a webpage, we need to know about HTML and CSS.
Let’s start with a basic example of printing “Hello world” by defining the HTML structure for webpage-
Fortunately, end users don’t have to fully grasp what’s going on in the background, other than for some security reasons. However, more languages will eventually be supported, according to the contemporary advancements in technology world.
BeforePyScript, running and carrying out Python operations on the web wasn’t simple. Anyone interested in utilizing Python on the web will find this to be a fantastic tool, despite being still in its infancy.
PyScript will indeed prove to be a head-turning evolution in the world of scripting languages.
Thanks for reading the blog!
Hoping for you to find it insightful! 😊