An alpha version of PyScript just came out, with tagline "Run Python in Your HTML". Let's check it out.
Documentation is mostly all wrong, so there's a lot of steps to get it even running.
The download instructions are:
- Download PyScript now
- Unzip the downloaded file
- Copy the assets you want to use and add the following lines to your html file
There's just one problem, there are no asset files in that zip.
That however does not work, as it then crashes trying to get
pyscript.py, so we need to get https://pyscript.net/alpha/pyscript.py as well.
I tried just creating a Hello World HTML and opening it as a local file, but that got into instant CORS error.
It's unfortunately more and more common with anything that uses modern web technologies.
Fortunately Python comes with a builtin HTTP server, so we can run
python3 -m http.server 8080 and then open
OK, with that out of the way, let's write the simplest possible Python script:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Hello World</title> <link rel="stylesheet" href="./pyscript.css" /> <script defer src="./pyscript.js"></script> </head> <body> <py-script> print("Hello World") </py-script> </body> </html>
If opened from local server, it at least no longer gets any errors in the network tab.
What happens then is that about 10% of time I get "Hello World" printed. And 90% of time there's an error in console like this:
JsException: SyntaxError: Failed to execute 'querySelector' on 'Document': '#-49bea52c-4893-412d-cba1-447d24c65f0a' is not a valid selector.
And only a pink bar in the document. I thought it might be some issues with some Chrome Extensions, so I tried it in an incognito window or in Safari, same thing.
It's very clearly some race condition.
Obviously not yet.
I want to come back to PyScript at some point, but in the next episode we'll actually take a look at Opal Ruby, which recently got 1.5 release.