Write Python code on HTML pages like JavaScript & PHP

How to write Python code on HTML pages like JavaScript & PHP

Now it is possible to write Python code on HTML pages. The PyScript made all this possible.

The creator of PyScript is the Anaconda team of Peter Wang, Fabio Pliger, and Philipp Rudiger. This means you can write and run Python code in HTML pages like PHP and JavaScript, use PyScript to call Javascript libraries and this could be a powerful tool for creating web applications.

Try PyScript

To try PyScript, import the appropriate PyScript files to your HTML page with:

<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>

You can use PyScript in your HTML file. PyScript currently supports the following features:

<py-script>: The main PyScript element is <py-script>. You can add Python code that is executable within the web page here. The element is not displayed on the page.
<py-repl>: A REPL that can be used to run Python code interactively.

PyScript Hello World Example

<head>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  </head>
  <body>
    <py-script> print('Hello, World!') </py-script>
    <py-script> print( 10 + 5 ) </py-script>
  </body>

PyScript repl example

<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  </head>
  <body>
    <py-script> print('Hello, Friends!') </py-script>
    <py-repl id="my-repl" auto-generate="true"> </py-repl>
  </body>
</html>

Test this live example here.

Read more: Python Syntax cheat sheet

Conclusion

PyScript is now in the development stage, so although it is currently a little slow to get going, it will only get better.

Currently, PyScript is also deficient in some features. for instance, loading data from a server may require using PyScript-specific functions. On the plus side, though, PyScript works well with Javascript and the DOM, which adds functionality beyond that of regular Python.

So, is JavaScript in danger from PyScript? No. Will it be helpful for building simple online applications for scientists and data scientists? I agree, but there is still a great deal of work in progress that will decide it.

Add comment