Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
Python Programming

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.

This discussion has been archived. No new comments can be posted.

Want to Run Python Code in a Browser? Soon You Might Be Able To

Comments Filter:
  • Nope (Score:5, Informative)

    by drinkypoo ( 153816 ) <drink@hyperlogos.org> on Saturday May 14, 2022 @12:42PM (#62533122) Homepage Journal

    "Now, however, that may be about to change."

    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:Nope (Score:4, Informative)

      by caseih ( 160668 ) on Saturday May 14, 2022 @01:58PM (#62533290)

      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 applies!

      There is a question in the headline, and the answer is "no".

      • by Megane ( 129182 )
        But in this case "No" is the answer to the question "Want to run Python code in a browser?" And it's entirely correct here, because I don't want to!
    • 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

      • by Qwertie ( 797303 )

        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).

        • by shanen ( 462549 )

          ACK and thanks. I should have disclaimed my limited knowledge of Python and that the implementation in my link is partial.

  • 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.

    • by Z00L00K ( 682162 )

      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.

      • 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...!

      • 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.

  • Comment removed based on user account deletion
    • No. Why would I?

      Because the alternative is JavaScript.

  • 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 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.

    • by caseih ( 160668 )

      Funny to see so many posters that think server-side python (or ruby or whatever) is "in the browser." Especially here on slashdot.

      • 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.

  • by Espectr0 ( 577637 ) on Saturday May 14, 2022 @12:58PM (#62533154) Journal

    memories from java applets come to mind

  • I"m sure the implementation will be 100% secure, so there is no chance of malicious code taking taking advantage of this.
    • by gweihir ( 88907 )

      Just the usual JavaScript isolation, so not that bad.

    • 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.

    • 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.

  • My answer to "Want to Run Python Code in a Browser?" is "No".

    • 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.

  • I think Jython works in an applet.

    • by ceoyoyo ( 59147 )

      There are a pile of Python interpreters written in javascript too. And some more that are compiled to web assembly.

  • Doesn't seem to say whether they'll have their own methods for accessing the DOM, or depend on JavaScript libraries.

    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
  • Python in HTML? (Score:5, Insightful)

    by AnonymousNoel ( 6972222 ) on Saturday May 14, 2022 @01:31PM (#62533230)

    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?

    • That comment is FUD. HTML already respects white space in cdata and script tags. There are no concerns here.

    • The bigger concern is that Python is not async. Doing anything IO-heavy with Python makes zero sense. It's amazing the lengths people will go to to not learn Javascript.
      • by caseih ( 160668 )

        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

        • Everything async has a single thread event loop and non-blocking IO. That's what async means. Yes Python has async addon now but you can't use non-async libraries with it and those are 99.99% of Python's offering.
          • by caseih ( 160668 )

            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

            • I'm sorry but you're wrong. I don't dislike Python, I use it regularly but it's not appropriate for this particular application. It's mainly for simple CPU-bound tasks like machine learning which can accommodate it's non-async, non-thread-safe libraries. You pick the right tool for the job, and while that tool is sometimes the hammer, it's often something else.
      • by jabuzz ( 182671 )

        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.

      • by hawk ( 1151 )

        > 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.
        • Washing ones eyeballs out with bleach.
          Repeatedly mixing nitroglycerine until a batch explodes
        • Ra

    • Why do you hate makefiles?

    • I thought the idea was to compile Python to WebAssembly offline, so the browser would not see the Python source.

      • In which case it is not Python in the browser.

        • 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.

  • 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."

  • Well, better than JavaCrap, but why would I want to run code in a browser in the first place?

    • by ceoyoyo ( 59147 )

      He says, posting on Slashdot....

      • by gweihir ( 88907 )

        That is not me running it. I am allowing /. to run it. Subtle difference, I know.

        • by ceoyoyo ( 59147 )

          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.

    • why would I want to run code in a browser in the first place?

      Implementing things like webmail and online catalogues come to mind.

  • Python always seemed like the cheap out for people who didn't know C or C++ and always seemed to birth atrocious programs that could run smoother on ANYTHING else... But I'm an old curmudgeon.
    • 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

      • I'm sorry I didn't mean to offend! I often forget that they almost exclusively teach Python in CS nowadays! Sorry again!
  • It probably won't support Netscape.
  • Oh no, not more places you can use that crap language.
  • 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

  • This project is 4 years old and didn't need fanfare to work: https://webperl.zero-g.net/ [zero-g.net]
  • How is this different from replit.com ?

One person's error is another person's data.

Working...