PyScript: Run Python Code in HTML

What is PyScript?

PyScript is defined as a Python script that uses Python and standard HTML combination to run in the browser. Therefore you can execute python code in html. It enhances web application development.

PyScript in Python

JavaScript is an important factor for web application development. However, you can now ignore it for the development of web apps. Anaconda unveiled a new framework known as PyScript. It uses python in HTML code to develop apps. 

PyScript is beyond HTML. It is a powerful framework, thanks to its rich and accessible Python libraries ecosystem. It helps to embed python code in HTML easily and efficiently.

Python code in HTML

What are the features of PyScript?

  • It is simple to use
  • It is simple to learn as compared to JavaScript
  • PyScript offers a clean API
  • Standard HTML supportable 
  • Easy to develop web apps using Python
  • Supporting Python code to be run in the browser. 
  • Everything happens in the web browser, so there is no need to worry about deployment.

WebAssembly/WASM:

According to the Anaconda’s press release. WebAssembly is the fundamental technology that enables writing the website in Python. Web browsers only supported JavaScript when the WebAssembly was originally developed. 

WebAssembly then quickly became the official Word Wide Web Consortium (W3C). It also includes .wat text format language which is human-readable.

Huge ecosystem of Developer Tools:

There is a large developer community in Reactjs. They create a massive ecosystem of component libraries, tools and extensions for web browsers and code editors. 

Reactjs is ideal for creating innovative path-breaking websites and apps. Thanks to its huge community of experts.

  • It also allows us to write the code in any language, then compile it to WebAssembly and then run in the web browser. 
  • WebAssembly code can be run in the modern web browsers. 
  • It is a low-level assembly language that includes a compact binary format. It runs with react native performance.
  • It provides the languages like C/C++, Python, C# and Rust along with a compilation target. 
  • It also provides a way to run code written in multiple languages.

How to install PyScript?

Method 1

  • Download from the https://pyscript.net/
  • Unzip that downloaded file
  • After that, use the below lines in your coding

<link rel=”stylesheet” href = “path/to/pyscript.css”/>
<script defer src =”path/to/pyscript.js”></script>

Method 2

Just use the CSS and js file without downloading it. 

<link rel=”stylesheet”

href=”htpps://pyscript.net/alpha/pyscript.css” />

<script defer 

src=”htpps://pyscript.net/alpha/pyscript.js”>

</script>

HTML: The language that tells how documents and web pages are displayed in the web browser. 

CSS(Cascading Style Sheets): The .CSS is for the layout and HTML elements customization. 

JavaScript: It allows you to change the HTML and CSS elements on the website after the loading.

How to use PyScript?

Use the <py-script> tag and then mention the Python code inside the tag.

<py-script>

“Hello World”

Your python code

</py-script>

After that you can pass the Python file directly.

<py-script src=”/my_own_file.py”></py-script>

Use the below code if you need to use the additional modules and packages. Use <py-env> tag to specify my environment needs:

<py-env>

-bokeh

-numpy

-matplotlib

-paths: 

-/utlis.py

</py-env>

<py-env>

  • Altair
  • Numpy
  • Pandas
  • Scikit-learn
  • Panel = 0.13.1a2

</py-env>

Use <py-repl> tag if you want to use REPL

<py-repl id=”my-repl” auto-generator=”true”></py-repl>

It will create a widget.

<py-script>

print(‘Hello World’)

<py-script>

Final Conclusion:

Pyscript is easy to learn and very simple to implement. It is nice to do web application development using Python in HTML. It will be like a notebook. Hope you get enough information about Pyscript and Run Python code in html. Are you are a professional Python developer? Implement it once in your coding.

Share This Story, Choose Your Platform!