How to run Python in HTML using PyScript?

Before knowing about running the Pyscript, first we need to know what is pyscript. I have written a separate article kindly check that one if you wish am leaving the link here.

PyScript has three main components for writing python in HTML.

  • py-env defines the Python packages needed to run your Python code. 
  • py-script is where you write your Python code that gets executed within the web page.
  • py-repl creates a REPL (read-eval-print loop) component that evaluates the code users enter and displays the results.
1.Create a HTML file.
2. Go to https://pyscript.net/ website.
3. Click on install. Copy the code and paste it on the <head> tag of the HTML file.
pyscript website
4. Use PyScript tags to run python code.
sample code

OUTPUT:

output
References:
  1. https://anaconda.cloud/pyscript-python-in-the-browser
  2. https://pyscript.net/

PyScript: Python in Browser

What is PyScript?

PyScript is a framework that allows users to create Python applications in browser by using Python in standard HTML. PyScript aims to provide users first class programming language experience by providing consistent styling rules, more expressive and easier to learn. Anaconda’s CEO Peter Wang announced this new technology at PyCon US 2022.

Core components:
  • Python in browser
  • Python ecosystem: Runing many popular modules of python in browser.
  • Environment management: Allow users to manage which package and files to include.
  • Visual Application Development: No need to worry about deployment as everything happens in web browser.
  • Python with Javascript: Bidirectional communication between python and javascript.
  • Flexible framework.
How does it work?

             PyScript is built on Pyodide. Pyodide is a port of CPython to WebAssembly / Emscripten. Pyodide makes it possible to install and run Python packages in the browser with micropip. Web assembly is a technology that makes it possible to write websites in Python. It uses a human readable .wat text format language, which then gets converted to a binary.wasm format that browsers can run.

Aim:
  • Offer clean and simple API
  • Support standard HTML
  • Extend HTML to read custom components that are opinionated and reliable.
  • Provide a pluggable and extensible components system.
PyScript Techstack:

PyScript is an experimental project, very alpha and under heavy development. Javascript won’t die because of PyScript. Only it’s popularity may decrease as it relates to web development. The Project is developed by Anaconda.

In the next article, I will let you know about running python in HTML with hello world program example and attach the link below.

How to run python in HTML?

References:
  1. https://anaconda.cloud/pyscript-python-in-the-browser
  2. https://pyscript.net/