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 the Python code on HTML pages. The PyScript made all this possible.

PyScript, created by 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>

PyScript can now be used 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

References

https://github.com/anaconda/pyscript

https://anaconda.cloud/pyscript-python-in-the-browser

https://pyscript.net/examples/

Add comment