- Consulting
- Training
- Partners
- About Us
x
TABLE OF CONTENT |
1. Overview |
2. Introducing PyScript |
3. Core Elements of PyScript |
4. PyScript Architecture |
5. PyScript Components |
6. Conclusion |
7. About CloudThat |
8. FAQs |
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.
Do you work as a Python developer or a data scientist? Are you envious of coders that write JavaScript code and create sophisticated browser-based websites? Don’t you think it would be great if we could develop websites in Python? Surprisingly, Anaconda’s CEO Peter Wang introduced a dazzling new technology called PyScript at PyCon US 2022, allowing users to write Python and other languages in the browser.
PyScript is a JavaScript framework for building Python applications in the browser. That is right, just like any JavaScript developer, we can now embed Python code straight in HTML files. This project allows us to execute Python in our browser.
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.
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:
1 |
<py-env> #To declare the packages you need </py-env> |
1 |
<py-script> #To insert Python logic into HTML </py-script> |
1 |
<py-script src=”/python_file.py”> </py-script> |
1 |
<py-repl> #Creates a REPL shell, where user can execute interactive python code </py-repl> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <title>Page Title</title> </head> <body> <h1>This is a Heading in HTML</h1> <py-script> def hello(): print('Now you can in Python!') a=10 b=20 return(a*b) print(hello()) </py-script> </body> </html> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<html> <head> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> <py-env> - numpy - matplotlib </py-env> </head> <body> <h1>Plotting a histogram of Standard Normal distribution</h1> <div id="plot"></div> <py-script output="plot"> import matplotlib.pyplot as plt import numpy as np np.random.seed(42) rv = np.random.standard_normal(1000) fig, ax = plt.subplots() ax.hist(rv, bins=30) fig </py-script> </body> </html> |
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.
If you have any queries about PyScript, Python Language, or anything related to Javascript, feel free to drop in a comment. We will get back to you quickly. Visit our Consulting Page for more updates on our customer offerings, expertise, and cloud services.
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.
1 2 |
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> |
The link will act as a CDN, and our Python code gets executed from the browser directly without installing Python software.
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.
Voiced by Amazon Polly |
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.
Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!
Siva Sai R
Jul 26, 2022
This blog introduced us to a new emerging technological advancement in python, good one; thank you!
Venkatesh Uppalapati
Jul 21, 2022
It is very useful and informative blog for us , Thanks for sharing.
Munwar
Jul 21, 2022
Very useful, Great work 👍
Anusha Shanbhag
Jul 21, 2022
Very informative blog.
Click to Comment