Running python in the browser through Pyscript

I recently stubled upon Pyscript project that helps you to run python in the browser. Sounds interesting !

Decided to look at it for little more detail.

Here's my two-cents.

  • What is PyScript?

Well - like I had said, PyScript is a new framework that lets you run Python directly in the browser using HTML tags. Announced by Anaconda - it simplifies integrating Python into web development.


  • How It Works?

PyScript is built on top of WebAssembly and Pyodide. It usestags to embed Python code in your HTML, making it as simple as writing JavaScript.


  • So why is it Exciting?

Well it bridges the gap between web and Python communities.

Developers can now combine Python’s vast libraries (like NumPy, Pandas, or Matplotlib) with web development tools without needing a backend server.

I am "guessing", we might have to import those packages at load-time or may be we could convert the python packages into some kind of wasm-like js-native libs. Not sure.

Some use cases that I can think of are for interactive data visualizations, scientific applications, or even teaching Python to beginners in a browser.


  • Here's a sample Code
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>PyScript Example</title>
	<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
	<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
	<h1>Welcome to PyScript!</h1>
	<py-script>
			# Python code running in your browser
			print("Hello, from PyScript in the browser!")
	</py-script>
</body>
</html>

  • Limitations

As of now, it’s still in its early stages, so performance might lag for heavy tasks, and browser compatibility can vary.

However it's an interesting project - let's see how it progresses in the future.