Meet ‘PyScript’: New Framework From Anaconda That Allows Users To Create Rich Python Applications In The Browser Using HTML’s Interface

Please Don't Forget To Join Our ML Subreddit

Do you work as a data scientist or a Python developer? Are you envious of coders who write Javascript code via browser interface? Anaconda releases an unexpected project – PyScript — at PyCon US 2022. It’s a JavaScript framework that lets you construct Python apps on the web using a combination of Python and HTML. The project’s ultimate purpose is to make Python and its different libraries (statistical, ML/DL, etc.) accessible to a much broader audience (for example, front-end developers).

What exactly is PyScript?

PyScript, developed by the Anaconda is “a system for interleaving Python in HTML (like PHP),” as the CEO of Anaconda said in his lecture. This means users can create and run Python code in HTML, use PyScript to invoke Javascript libraries, and use Python for all of their web development. That sounds fantastic!

What You Should Know About PyScript

  • The users can utilize Python and its extensive ecosystem of libraries (including numpy, pandas, and scikit-learn).
  • Users can choose which packages and files are available when running the page’s code using environment management.
  • Some handpicked UI components are available, such as buttons, containers, and text boxes.
  • It is unnecessary for users to be worried about deployment because PyScript handles everything in our web browsers. Users may share HTML files, including dashboards and/or models, with their stakeholders as data scientists. They would be able to execute them on their browsers without any complicated setup.

What is the mechanism behind it?

Pyodide is the foundation for PyScript. There aren’t many data scientists who aren’t familiar with Pyodide. So it’s a WebAssembly-based Python distribution (version of CPython) for the browser and Node.js.

Fig. 1 PyScript Tech Stack Source: https://www.anaconda.com/blog/pyscript-python-in-the-browser

What exactly is WebAssembly, and how does it function?

WebAssembly is the technology that allows Python programmers to create web pages. It employs a readable format. The .wat text format language is then translated to a binary .wasm format that browsers can read. Thanks to this, users can write code in any language, compile it to WebAssembly, and then run it in a web browser.

PyScript now allows you to write and run Python code in your browser. It is hoped that it will also support other languages in the future.

This is also where there is the possibility of a restriction. Users can currently only utilize the libraries that Pyodide supports when using PyScript.

PyScript: How to Use It

PyScript’s alpha release may be accessed at pyscript.net. The source code may be found at https://github.com/pyscript. Follow these instructions to give it a go. PyScript uses three key components to allow you to write Python in HTML:

  • py-env is a Python environment variable that specifies the Python packages required to run your Python code.
  • You write your Python code in py-script, performed within the web page.
  • py-repl generates a REPL (read-eval-print loop) component that evaluates and displays the code entered by the user.

An example of py-env

Here’s an example of using py-env to establish our Python environment in HTML. The packages bokeh, numpy, pandas, and scikit-learn are loaded into the environment in this example.

Source: https://www.anaconda.com/blog/pyscript-python-in-the-browser

An example of a py-script

This example demonstrates how to make a live data Panel dashboard. Users can write our usual Python code in py-script just like they do in a Python file, as seen in the code. If users don’t want to write your Python code directly in py-script, they may use a Python file like this as the source code:

Source: https://www.anaconda.com/blog/pyscript-python-in-the-browser
Source: https://www.anaconda.com/blog/pyscript-python-in-the-browser

Conclusion

The PyScript project was founded by Anaconda. PyScript will allow researchers to execute Python (and other) programs straight from their browsers. Users can choose which packages and files are available when running the page’s code using environment management. Some UI components, such as buttons, containers, and text boxes, are available immediately. Several Pyodide-supported libraries are now public for researchers to test out, even though the project is still in its early phases.

References:

Prathamesh Ingle is a Consulting Content Writer at MarktechPost. He is a Mechanical Engineer and working as a Data Analyst. He is also an AI practitioner and certified Data Scientist with interest in applications of AI. He is enthusiastic about exploring new technologies and advancements with their real life applications