Traditionally, Python is used as a backend programming language in web development. To use Python in web applications, we need to depend on Python software which is hosted on the server but now we can directly run python code from the HTML file itself.
PyScript’s concept is simple: data scientists may use their chosen language to construct websites based on their models, as long as they have a basic understanding of HTML.
We can even host HTML files containing PyScript in the AWS cloud S3 bucket, making the HTML file dynamic instead of static. This Pyscript helps render dynamic HTML pages from the AWS Lambda service, making HTML files more robust.
Core Elements of PyScript:
Python in the browser: This allows us to host drop-in content, external files, and applications without requiring server-side settings
Python Ecosystem: Use NumPy, pandas, scikit-learn, and other popular Python and scientific stack packages
Environment management: Allow users to choose which packages and files should be included in order for the page code to run
PyScript is written in Pyodide, which is a CPython-WebAssembly bridge.
CPython is a fast Python implementation developed in both Python and C.
WebAssembly is a low-level language that, as the name suggests, is very similar to Assembly and has incredible performance. It runs in binary format. It acts as a trans piler for the Python code you create. It comprises a human-readable.wat text format language, which is then translated to a binary.wasm format that browsers can read. We can now create code in any language, compile it to WebAssembly, and run it in a web browser.
PyScript uses three key components to allow us to write Python in HTML:
py-env is a Python environment variable specifying the Python packages required to run your Python code. This tag is placed in Html <head> tag. Syntax:
<py-env>#To declare the packages you need </py-env>
We write Python code in py-script, which is then performed within the web page.
<py-script>#To insert Python logic into HTML </py-script>
If we like to use some python code in an external file, then we can use the syntax below:
py-repl generates a REPL (Read-Eval-Print-Loop) component that evaluates and displays the code entered by the user.
<py-repl>#Creates a REPL shell, where user can execute interactive python code </py-repl>
In the above example, we have included python related links in the head tag, and in the body, we have directly used the <py-script> tag to write code in Python. It will get executed, and the output will be displayed in UI.
<h1>Plottingahistogram of Standard Normal distribution</h1>
import matplotlib.pyplot asplt
import numpy asnp
In this example, we are using numpy and matplotlib. So we need to declare them in <py-env> tag and then we can import and use them directly from our <py-script> tag. This way, we can use this to display statistical data on the screen, as shown below output.
PyScript is still in the early stages of development. Several documented concerns range from usability to loading times and expect things to change frequently. We can use this to experiment with statistical data using PyScript to display charts directly in UI, but it is not suggested that utilize it for production at this time.
CloudThat is the official AWS Advanced Consulting Partner, Microsoft Gold Partner, and Training partner helping people develop knowledge on the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
Do we need to install Python in our System/hosted server?
No, we do not need Python to be installed in our application-hosted platform. We only need to include the script link in our HTML code, and that’s it.
The link will act as a CDN, and our Python code gets executed from the browser directly without installing Python software.
How to install a package to import it in Py Script?
We can mention all the packages that are required in the <py-env> tag in HTML, which loads the package in our environment so that we can directly import it into PyScript.
Yes, we can use JS and PyScript code in our HTML file in their script tags.
WRITTEN BY Imraan Pattan
Imraan is a Software Developer working with CloudThat Technologies. He has worked on Python Projects using the Flask framework. He is interested in participating in competitive programming challenges and Hackathons. He loves programming and likes to explore different functionalities for creating backend applications.