Want to Run Python Code in a Browser? Soon You Might Be Able To (zdnet.com) 88
ZDNet reports news from PyCon 2022 ("the first in-person meet-up for Python contributors since 2019 due to the pandemic")
"Developers revisited the idea of running Python code in the browser...." CPython developer Christian Heimes and fellow contributor Ethan Smith detailed how they enabled the CPython main branch to compile to WebAssembly. CPython, short for Core Python, is the reference implementation that other Python distributions are derived from. CPython now cross-compiles to Wasm using Emscripten, a toolchain that compiles projects written in C or C++ to Node.js or Wasm runtimes. The Python Software Foundation highlighted the work in a blog post: "Python can be run on many platforms: Linux, Windows, Apple Macs, microcomputers, and even Android devices. But it's a widely known fact that, if you want code to run in a browser, Python is simply no good — you'll just have to turn to JavaScript," it notes.
"Now, however, that may be about to change."
While the Foundation notes cross-compiling to WebAssembly is still "highly experimental" due to missing modules in the Python standard library, nonetheless, PyCon 2022 demonstrated growing community interest in making Python a better language for the browser.
The article notes additional news from Anaconda (makers of the a Python distribution for data science): the announcement of PyScript, "a system for interleaving Python in HTML (like PHP)." It allows developers to write and run Python code in HTML, and call Javascript libraries in PyScript. This system allows a website to be written entirely in Python.
PyScript is built on Pyodide, a port of CPython, or a Python distribution for the browser and Node.js that's based on WebAssembly and Emscripten.... "Pyodide makes it possible to install and run Python packages in the browser with micropip. Any pure Python package with a wheel available on PyPI is supported," the Pyodide project states. Essentially, it compiles Python code and scientific libraries to WebAssembly using Emscripten.
"Developers revisited the idea of running Python code in the browser...." CPython developer Christian Heimes and fellow contributor Ethan Smith detailed how they enabled the CPython main branch to compile to WebAssembly. CPython, short for Core Python, is the reference implementation that other Python distributions are derived from. CPython now cross-compiles to Wasm using Emscripten, a toolchain that compiles projects written in C or C++ to Node.js or Wasm runtimes. The Python Software Foundation highlighted the work in a blog post: "Python can be run on many platforms: Linux, Windows, Apple Macs, microcomputers, and even Android devices. But it's a widely known fact that, if you want code to run in a browser, Python is simply no good — you'll just have to turn to JavaScript," it notes.
"Now, however, that may be about to change."
While the Foundation notes cross-compiling to WebAssembly is still "highly experimental" due to missing modules in the Python standard library, nonetheless, PyCon 2022 demonstrated growing community interest in making Python a better language for the browser.
The article notes additional news from Anaconda (makers of the a Python distribution for data science): the announcement of PyScript, "a system for interleaving Python in HTML (like PHP)." It allows developers to write and run Python code in HTML, and call Javascript libraries in PyScript. This system allows a website to be written entirely in Python.
PyScript is built on Pyodide, a port of CPython, or a Python distribution for the browser and Node.js that's based on WebAssembly and Emscripten.... "Pyodide makes it possible to install and run Python packages in the browser with micropip. Any pure Python package with a wheel available on PyPI is supported," the Pyodide project states. Essentially, it compiles Python code and scientific libraries to WebAssembly using Emscripten.
Nope (Score:5, Informative)
But it isn't, because Betteridge's law applies. Not in a mystical way, but in a that's-not-python-in-the-browser-that's-webassembly kind of way. And it's not a distinction without a difference in any way, either. If you can't run interpreted python in the browser, that's not python.
The article notes additional news from Anaconda (makers of the a Python distribution for data science): the announcement of PyScript, "a system for interleaving Python in HTML (like PHP)."
Which is also not in the browser, so still no. Also, it's not the first way you could write mixed Python and HTML... That was actually ASP! You could plug in python as a scripting language for ASP [timgolden.me.uk], and then write the code portions of your ASP in Python! There is also mod_python, which appeared around the same time as python for win32, but that's really not the same thing.
Re: (Score:2)
Re:Nope (Score:4, Informative)
And yet, PyScript is running in a browser. Instead of javascript in script tags in the html, there are py-script tags and python to do DOM manipulation and event handling. The fact that it is running a python interpreter on top of javacript (yes turtles all the way down) is an implementation detail that doesn't change the fact that python code is running (eventually... after the python interpreter webasm loads) in the browser.
A little example: https://github.com/amrrs/dom-m... [github.com]
The slow load time at present would seem to limit PyScript's usefulness, but that could change in the future.
Betteridge's Law of Headlines! [Re:Nope] (Score:2)
Betteridge's Law of Headlines applies!
There is a question in the headline, and the answer is "no".
Re: (Score:2)
Re:Nope [but I haven't tried it] (Score:2)
I wish I'd seen the story when it was fresh rather than just before it died. But browser-resident Python already exists. No connection that I can see to this story. And a bit surprised that no one on Slashdot added the link. It's from a popular Coursera class on Python.
So here's a link to a browser resident version of Python that implements a limited version of the old Asteroids game. https://py2.codeskulptor.org/#... [codeskulptor.org]
It's already 6 years old, but it still seems to work with Firefox, though it also has a not
Re: (Score:2)
There is a JavaScript implementation of Python [skulpt.org] that I assume is being used there.
A WebAssembly implementation would presumably support Python more reliably/completely. Most notably - though the Python Software Foundation's blog post doesn't talk about this - it should be able to run C/C++-based Python libraries (since that's what WebAssembly was originally designed for).
Re: (Score:2)
ACK and thanks. I should have disclaimed my limited knowledge of Python and that the implementation in my link is partial.
Many Years (Score:1)
You have been able to use Python as a scripting language (directly) in (some) web clients for many years. You have been able to create HTTP servers in python for many years.
This adds nothing new -- but I am sure the unwashed kiddies will love it -- useless though it is.
This is an excellent reason to keep webasm completely disabled.
Re: (Score:2)
I'd prefer to run something that would be less prone to runtime crashes, like something written in C#. We have had Java and that had some issues.
However whatever we do run in a browser it's prone to issues, either by itself or by annoying users.
Re: (Score:1)
Seems like this is a question if a browser should be allowed invoke scripts in a given language and/or have the functionality of a language served to it...but the real question is should a browser behave like a terminal/shell as well...hey, a powershell...!
Re: (Score:3)
I'd prefer to run something that would be less prone to runtime crashes, like something written in C#. We have had Java and that had some issues.
However whatever we do run in a browser it's prone to issues, either by itself or by annoying users.
But C# is mired deep in the OO tar pit.
Python does OO in a pretty nice, unobtrusive way.
That said "In a browser" is the place where good languages go to die. There's a reason I don't work on web stuff. It's just nastiness at each layer. Give me a good logic gate, they don't change on you every three weeks.
Re: (Score:2)
Re: (Score:2, Insightful)
Re: (Score:2)
Because you're using another machine which you don't have access to python on and want to bash out a quick script?
That would be sweet, but it's not the use case that is being addressed here.
This means people can download the (compiled) Python you wrote and run it in the browser, it's not actually Python in the browser.
Re: (Score:2)
That would be sweet, but it's not the use case that is being addressed here.
There's a gazillion web pages that let you type python in your browser and run it.
OTOH I can't imaging why anybody would want "a quick script" that runs in a browser.
Surely the use case for a "quick script" is to manipulate some files or do something on the local machine.
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
Then I'd use bash, batch or PowerShell because I'm not a one trick pony.
Re: (Score:2)
Because you're using another machine which you don't have access to python on and want to bash out a quick script?
But you do have time to create a web page, download CPython along with the appropriate libraries and compile it to webassembly via Emscriptem?
Uhuh.
Re: (Score:2)
No. Why would I?
Because the alternative is JavaScript.
IIUC. Ruby has been able to do that for ~~ 10 yrs (Score:2)
Are you sure you can't run Python in a web page? I'll admit I've never tried, but I always assumed you could, because Ruby has been able to do that for about a decade. Perhaps a bit longer. Or perhaps you have some special meaning that I don't grasp.
https://www.tutorialspoint.com... [tutorialspoint.com]
but of course it can also do cgi scripts, etc.
That's the inverse, run on the server (Score:2)
That's run on the server. It's not run in the browser, and therefore cannot do anything after the user loads the page.
Or to put it another way, you said "run Python in a web page" - you've linked to creating a web page in a programming language, which is the inverse of running the language in the page.
It's the difference between you controlling your computer and your computer.controlling you. Same words, but the order matters.
Re: (Score:3)
Funny to see so many posters that think server-side python (or ruby or whatever) is "in the browser." Especially here on slashdot.
Re: (Score:2)
Not everyone does web programming. Or even want scripts running in the browser in the first place. That dark corner of the programming world is a bit scary and full of creepy crawlies.
what's old is new again (Score:3)
memories from java applets come to mind
Can't imainge that being a security risk (Score:2, Funny)
Re: (Score:3)
Just the usual JavaScript isolation, so not that bad.
Re: (Score:2)
I"m sure the implementation will be 100% secure, so there is no chance of malicious code taking taking advantage of this.
Summary says compiles to webassembly so it's not going to introduce any new vulnerabilities by itself.
Re: (Score:2)
I"m sure the implementation will be 100% secure, so there is no chance of malicious code taking taking advantage of this.
My thoughts exactly- nothing bad could possibly happen by letting your browser run code that can manipulate your file system.
Betteridge is right (Score:2)
My answer to "Want to Run Python Code in a Browser?" is "No".
Re: (Score:2)
My answer to "Want to Run Python Code in a Browser?" is "No".
If I were given the opportunity to run Python rather than JavaScript in a browser, I would prefer Python. That is simply because I have written far more code in Python than in JavaScript. When I did an online simulation of ice age cycles, what I really wanted was a language like Python. Instead, I had quite a struggle to get anything working in JavaScript, before I got anywhere near implementing the core code.
Jython (Score:2)
I think Jython works in an applet.
Re: (Score:2)
There are a pile of Python interpreters written in javascript too. And some more that are compiled to web assembly.
DOM? (Score:2)
I only code for my own use these days, and only have one piece of JavaScript I maintain. It's a GreaseMonkey script that runs on every page I download. It goes through and changes the fonts and sizes of most everything to match my standards for what looks attractive. I started it years ago on a day where I encountered too many pages that made you want to go find the designer and ask, "Did
Re: (Score:3)
Of course you can access DOM. If not, there's simply no point in this endeavour. As an example, see https://github.com/amrrs/dom-m... [github.com]
Python in HTML? (Score:5, Insightful)
Does anyone think that co-mingling a language where white-space is syntactically crucial and a language where all white-space is ignored is anything other than a disaster waiting to happen?
Re: (Score:1)
Python: white-space is syntactically crucial
HTML: white-space is ignored
I love Python but I do wonder how that would be resolved for Python in HTML.
Re: (Score:2)
It all looks the same in ASCII.
There aren't any problems "commingling" languages where ASCII 123 and 125 have special meanings and ones where they don't, so the only problems with 09 and 32 will be if someone is using an editor that for some reason takes it upon itself to screw with them.
Re: (Score:2)
Indeed. Requires some actual understanding though and the person you responded to is clearly lacking that.
Incidentally, HTML does not ignore white-space. Displaying HTML just does not magically turn things separated by whitespace into one word. So, on _formatting_, a HTML _renderer_ may (and usually will) regard repeated whitespace that is not specially tagged as just one whitespace, but that is it. On the other hand, a web-server or web-proxy is very much _not_ allowed to mess with whitespace.
Re: (Score:2)
This is fairly easy to determine by observing all the whitespace separating words.
It would be fairly difficult to read anything on the web if this were not the case. (This sentence was originally written with no spaces, but Slashdot thinks it's ASCII art.)
Re: (Score:1)
Simple: Not an issue because whitespace is not ignored in HTML. It usually is compacted on displaying a web-page, but it is still there in the sources.
Re: (Score:2)
So are you suggesting that the correct syntax would be:
<button onclick="
some
indented
python
code
">
I mean, that could work, perhaps, but seems like it would lead to chaos.
(Or, perhaps, you're saying that inline scripting won't be supported, in which case this is dead in the water.)
Re: (Score:2)
So are you suggesting that the correct syntax would be:
<button onclick="
some
indented
python
code
">
I mean, that could work, perhaps, but seems like it would lead to chaos.
(Or, perhaps, you're saying that inline scripting won't be supported, in which case this is dead in the water.)
It would be better than the unreadable block of obfuscated javascript that you get these days.
Re: (Score:2)
Yes, so?
Re: (Score:2)
Re: Python in HTML? (Score:2)
That comment is FUD. HTML already respects white space in cdata and script tags. There are no concerns here.
Re: (Score:2)
Re: (Score:3)
No it's not a concern. Because Python does do async. Python might be late to the party, but it certainly *is* "async" now. And before that a library called Twisted made asynchronous I/O possible for many years, and it was widely used. Certainly with syntactic sugar it's easier now.
Python is multi-paradigm. You can use async primitives if you wish, you can use threading if you wish, or a combination of both. A combination of thread pools and asynchronous event handling---either through built-in async pri
Re: (Score:2)
Re: (Score:2)
Python's async support is not "addon.". It's baked into the language now. And like other async-capable languages there are facilities for calling non async code, which use threads under the hood. This is no different than c# or any other language with baked in async primitives.
Look you can dislike python all you want but let's be honest about what python offers as features here. And you can argue all you want about whether the gil makes threads less capable (in this case it does not) Python definitely offe
Re: (Score:2)
Re: (Score:2)
What amazes me no end is that a language with no concept of arrays is popular in scientific computing. What the hell that is about is utterly beyond me, but as far as I can tell for Physics at least is is totally unsuitable for huge swaths problems.
Re: (Score:2)
Re: (Score:2)
> It's amazing the lengths people will go to to not learn Javascript.
Common examples:
Running screaming into the street, jumping into the car, and moving out of town to take up a new identity.
Repeatedly mixing nitroglycerine until a batch explodes
Ra
Re: (Score:2)
Why do you hate makefiles?
Re: (Score:2)
I thought the idea was to compile Python to WebAssembly offline, so the browser would not see the Python source.
Re: (Score:1)
In which case it is not Python in the browser.
Re: (Score:2)
I need to read the article again. Perhaps I missed something. The mention of WebAssembly implied that what is sent to the browser is bytecode, which originates from compiling Python source code. As far as developing the code is concerned, you are in effect running Python in the browser.
Want ANOTHER way to Run Python Code in a Browser? (Score:1)
There, fixed it for you.
By the comments above, the consensus seems to be "nope, at least not in the manner described in the current proposal."
Hmmmm. No? (Score:2)
Well, better than JavaCrap, but why would I want to run code in a browser in the first place?
Re: (Score:2)
He says, posting on Slashdot....
Re: (Score:2)
That is not me running it. I am allowing /. to run it. Subtle difference, I know.
Re: (Score:2)
You clicked an icon to start an internet enabled interpreter/renderer, then typed an address or clicked a button that downloaded some UTF8 encoded text files from the Internet and ran them through the interpreter.
Hate to tell you, but you're running code you downloaded from the Internet just as much as if you'd clicked on a .bat file in an e-mail.
Re: (Score:2)
why would I want to run code in a browser in the first place?
Implementing things like webmail and online catalogues come to mind.
Ehhhh I don't know about this (Score:1)
Re: (Score:2)
No, you're just posting about things you know nothing about. What you just posted amounted to "Hammers always seem like the cheap out for people who can't use a saw." That makes no sense because they're two different tools.
C and C++ have their own use cases. Python fulfill different needs. All languages can be good tools if you use them for what they're good at.
As for performances, Python really is quite impressive for an interpreted language. Sure if you need raw speed it's not ideal, but it's good enough
Re: (Score:1)
It's alright, but I see one glaring problem. (Score:1)
Oh no.. (Score:2)
In wasm more important than in browser (Score:2)
In the article, Guido is quoted as saying "I mean, nobody is asking Rust when you can write Rust in the browser; at least that wouldn't seem a useful sort of target for Rust either." And yet, people are asking when you can run Rust in the browser and the answer is you can. Because it's compiles to wasm. Surely that is the point, and one that is more important that running in the browser per se. Python is good for data science, sure, but many of the cool visualisation libraries, especially the interactive on
Just so you know (Score:1)
Python in a browser (Score:1)
Re: (Score:1)
Re: (Score:1)
Re: (Score:2)
Arbitrary sized integers baked into the language. Python's got that and for doing cryptography and difficult statistics, it's invaluable. For big floats Python's got MPFR which is also a breeze to use.
Fighting with GMP in C and C++ is not fun in comparison and I've only done it for really long computations where the programming effort will lead to an earlier result.