Sunday, December 22, 2024
HomeTechInclude Python in HTML: PyScript makes it possible

Include Python in HTML: PyScript makes it possible

As part of PyCon 2022, Anaconda, the company behind the Python distribution of the same name, announced PyScript. The project embeds Python into the HTML and, like JavaScript, provides a direct connection to a website’s Document Object Model (DOM).

According to the short summary in the readme on GitHub, PyScript is intended to be a Pythonic alternative to easy-to-use programming tools and frameworks like Scratch or JSFiddle. It uses standard HTML and provides custom tags to embed code, which you pass to the Python interpreter.

The core stack mainly consists of WebAssembly, Emscripten, and Pyodide. Mozilla first introduced the last project in 2019 to get Python into the browser. The main impetus was the Iodide project, also started in Mozilla but now discontinued, as a web-based tool for data analysis in the style of Jupyter Notebooks. In the spring of 2021, Mozilla turned Pyodide into an independent project.

Emscripten is an open source translator toolchain, which relies on LLVM to translate code to WebAssembly. Finally added to an open source project CPython on WASM (python-wasm) Toolkit. Many Python packages such as NumPy, pandas, and scikit-learn can be used from within PyScript.

HTML pages must have the following two lines before using PyScript tags:

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

Actual Python code can now be edited with the . tag <py-script> Include, as in the following example from the examples folder on GitHub:

<body>
  Hello world! <br>
  This is the current date and time, as computed by Python:
  <py-script>
    from datetime import datetime
    now = datetime.now()
    now.strftime("%m/%d/%Y, %H:%M:%S")
  </py-script>
</body>

See also  Sony mobile phone for 1299 euros was hit by a 399 euros smartphone

As with JavaScript, it is also possible to include code in external Python files in tags:

<py-script src="/mein_code.py"></py-script>

Additionally, HTML pages can also have a REPL (Read Eval Print Loop) with which users can interactively execute Python code in the browser:

<py-repl id="my-repl"  auto-generate="true"> </py-repl>

Currently, PyScript is in an early stage. Blog Post, which was released in parallel with the presentation at PyCon 2022, talks about proving that the technology is mature enough to implement the larger vision of the project. However, the framework should remain easy to use in the long run, and the blog post talks about “Minecraft for software development”.

More details and some programming examples It can be found in the Github repository. Files required to write HTML pages can be with inline code Download from the project pagewhich also provides installation instructions.

Two weeks before the international PyCon 2022 conference, the German PyConDE & PyData took place in Berlin, where PyScript was not yet visible, but also discussed Python in the browser.


(rme)

to the home page

Robin Gregory
Robin Gregory
"Music scholar. Extreme pop culture nerd. Wannabe bacon trailblazer. Hipster-friendly alcohol fanatic. Twitter geek. Reader. Hardcore social media guru."
RELATED ARTICLES

Leave a reply

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments