PyScript, Will it Replace Javascript?

Post a Comment


Python has long been able to be used to create the web. However, Python's role on the web is mostly as a backend.

This means that Python is only run on the server side. While on the client side (web browser), most still use Javascript.

But take it easy.

Is there a new technology that can make Python run in a web browser?

What's that?

pyscript.

What is PyScript? 

PyScript is a new technology for running Python in the browser. The PyScript behind it uses Pyodie and WebAssembly technologies.

anaconda.cloud

We have been able to use WebAssembly for a long time to run programs in any programming language in a web browser.

The important thing is that it can be compiled into WebAssembly (wasm), then it will be able to run in the browser.

However, the method offered by PyScript is without compilation.

The program will automatically run if we just write or embed the Python code in HTML with the <py-script> tag.

Example:

 <py-script>print('Now you can!')</py-script>

It's like writing JavaScript code in HTML.

So easy.

This method has actually been tried by Brython, but they don't seem to use WebAssembly.

The Beginning of PyScript 

Originally appeared in April 2022. You can check @pyscript_dev's first Tweet on April 03, 2022.

I accidentally tweeted on the 3rd so it wouldn't be mistaken for April Fool's Day.

Then at PyCon US 2022, Anaconda CEO Peter Wang was the speaker there and brought up the topic of PyScript.

2022, PythonThe Anaconda team, including Peter Wang, created PyScript itself.

Then there was a lot of talk on the internet, and a lot of memes about PyScript popped up.

pyscript memes Some say Javascript will die because of PyScript.

There are also those who say it's impossible because Javascript itself can't be beat for web business.

Which one will prove true, we'll see in the future.

Try PyScript 

Let's not be curious, let's just try it ourselves.

First of all, we have to add PyScript to the HTML like this:

As usual, this code is written inside the <head> tag.

Then, we just create a <py-script> tag inside the <body> tag which contains the Python code.


The results of the code execution will not be displayed immediately. There is a behind-the-scenes process that takes place.
First, the browser will download the runtime to run the Python code. 


This takes a while because the file size is quite large.
On the internet connection I use, it took me 3 minutes to get it up and running.
Pyscript resources This will provide a bad experience for the user because of its long load time.
Now we are required to create a website that can be opened in less than 10 seconds.
Maybe using PyScript to create the web would not be suitable. But let's try another use case.
This time I will try to run the Mario game from the sample code provided in the PyScript repository.
Result:

I can't play the game yet, it's just stuck looking like this.
Maybe something went wrong in the way I tried.

The PyScript Future

Since it's still an alpha version, I can't say anything. Maybe in the future, there will be a new update and better optimization.
Or maybe in the future, PyScript will support other languages.
So we can not only write Python in the py-script> tag, but we can also write C, C++, Rust, R, Go, and so on.

Related Posts

Post a Comment

Subscribe Our Newsletter