[HN Gopher] Python 3.11 in the Web Browser
       ___________________________________________________________________
        
       Python 3.11 in the Web Browser
        
       Author : tosh
       Score  : 125 points
       Date   : 2022-03-26 20:35 UTC (2 hours ago)
        
 (HTM) web link (2022.pycon.de)
 (TXT) w3m dump (2022.pycon.de)
        
       | teddyh wrote:
       | > _there is one place that Python has not yet conquered: the
       | browser_
       | 
       | Well, there was that one web browser written in Python, with
       | built-in Python scripting support: Grail.
        
       | stavros wrote:
       | I find WASM in general very cool. Is there a way to run a WASM
       | "binary" locally? For example, can I distribute a program as WASM
       | and execute ./mywasmprogram and have it run?
       | 
       | I imagine I need a WASM runtime installed, and to somehow get my
       | shell to recognize that WASM programs should be loaded with it
       | (for lack of a hashbang line), but is that actually doable?
        
         | simonw wrote:
         | Yes, definitely. Wasmer is one way to do that:
         | https://docs.wasmer.io/ecosystem/wasmer/getting-started
        
           | stavros wrote:
           | Excellent, thank you! I knew of Wasmer but didn't realize
           | that's what it was. I look forward to the day where we can
           | just distribute one architecture-independent binary.
        
             | wffurr wrote:
             | Like a .jar file?
        
           | teddyh wrote:
           | Note: There was some discussion of Wasmer here a couple of
           | days ago: https://news.ycombinator.com/item?id=30758651
        
         | gmatejka wrote:
         | You can also use node to execute webassembly programs.
         | 
         | You do have to create a js host file, load in your webassembly
         | and then run it with node.
        
       | mcintyre1994 wrote:
       | I'm a big fan of Python in WASM! It really reduces the friction
       | of playing around with things. Something I find pretty useful
       | that I hacked together using it is https://pyground.vercel.app/ -
       | I use it whenever I have a CSV or JSON file I want to do some
       | poking around in using Python.
       | 
       | You can just drag and drop a file in, it'll be imported in as the
       | 'data' variable, and then you can run Python code/do matplotlib
       | visualisations without installing anything.
        
         | wishawa wrote:
         | Have you seen Skulpt [1]? It doesn't do matplotlib (or most
         | other library) but it's pure JS.
         | 
         | [1]: https://skulpt.org/
        
           | mcintyre1994 wrote:
           | Ah yep! I came across it a few years ago when I was chatting
           | to the team at anvil.works, they were using it for their
           | client side python. Looks really cool, really ambitious and
           | impressive project.
        
       | syrusakbary wrote:
       | I'm super excited about this being integrated upstream.
       | 
       | Long ago we compiled Python 3.6 and published to WAPM:
       | https://wapm.io/python/python (you can run it online there!)
       | 
       | I wonder if we could publish the new version also! I think we got
       | the Python repl properly running so it would be interesting
       | trying to have that working on the new version too
        
       | nabakin wrote:
       | Found his work
       | https://github.com/tiran/cpython/tree/wasi/Tools/wasm
        
       | pjmlp wrote:
       | The irony is that in the old plug-in days ActiveState did had a
       | Python plugin for the browser.
        
         | jeremycarter wrote:
         | We've gone full circle
        
       | modeless wrote:
       | I really, really wish it were possible for wasm modules like this
       | to be shared between sites. That is, python.org could host an
       | official wasm module, and the first time you visited a site using
       | python the browser would download and compile it once and cache
       | the result, and then any other website could use python with no
       | download size or load time penalty. But unfortunately privacy
       | concerns killed this utopia. Caches are now partitioned per
       | origin, so every site that uses python has to download and
       | compile the wasm module separately even if it's the exact same
       | module from the same place, and that is unlikely to ever change:
       | https://developer.chrome.com/blog/http-cache-partitioning/
        
         | xpressvideoz wrote:
         | Wait, so the traditional "use the JavaScript CDN as a cross-
         | site cache" method does not work anymore? How did I not know
         | that!
        
           | simonw wrote:
           | It was a huge privacy hole: malicious pages could measure how
           | long it took for URLs to load and use that to tell which
           | other sites you had visited in the past.
        
       | heavyset_go wrote:
       | I think it would be cool if browsers shipped with WASM-compiled
       | interpreters and runtimes for other languages, like they do with
       | JavaScript.
       | 
       | That way you'd be able to use other languages in the browser
       | without needing users to download 20MB of a WASM-compiled
       | interpreter just to run 1KB of code.
        
         | kristopolous wrote:
         | That's what the old plugin architecture was about. People use
         | cdns to try to emulate that benefit but it's not the same.
         | 
         | Haven't seen a good hybrid solution, it probably exists already
         | and is poorly utilized
        
         | klodolph wrote:
         | You're still downloading the 20MB WASM-compiled interpreter,
         | it's just that now you're redownloading it every time your
         | browser updates.
        
           | sieabahlpark wrote:
        
           | heavyset_go wrote:
           | Yes, but that's the point, if the user has it installed by
           | default then there won't be much of a penalty if a website
           | chooses to use another language than JavaScript.
           | 
           | Right now, WASM interpreters only really make sense for
           | teaching and exposition with REPLs, where the user probably
           | won't mind large downloads in order to do something out of
           | the ordinary. Shipping interpreters would instead make that
           | ordinary.
        
             | klodolph wrote:
             | It's still a big penalty, you're just changing who pays the
             | penalty, when they pay the penalty, how often they pay the
             | penalty.
             | 
             | Personally, I'm pretty happy that we have WASM at all, and
             | I think there's a lot of work we can do (over the next
             | years) to make interpreters that work well in WASM.
        
           | [deleted]
        
           | krapp wrote:
           | > it's just that now you're redownloading it every time your
           | browser updates
           | 
           | There's no reason browsers couldn't have a persistent cache
           | for this. Think of it more as an integrated dependency
           | manager and VM that just happens to use web infrastructure.
           | No one would blink at downloading 20MB of dependencies
           | anywhere else, after all.
        
             | klodolph wrote:
             | Browsers _already_ have a persistent cache!
             | 
             | If you're downloading it on-demand and caching it, why
             | would it need to be integrated in the browser at all? Why
             | not just stick it in a CDN and treat it like a regular
             | file, the way it works right now?
        
               | krapp wrote:
               | > Browsers already have a persistent cache!
               | 
               | And yet whenever this comes up, someone always insists
               | that you'd have to re-download the entire thing with
               | every request, as if caching wasn't a thing.
               | 
               | As far as shipping vs caching goes, I see no reason not
               | to do both. Maybe ship with the latest version of a few
               | popular languages including javascript already pre-cached
               | and allow for downloading others as required. I don't
               | know what would be more optimal. Maybe when you download
               | a browser, you can select language support options.
               | 
               | My point is, this is just an implementation detail, it
               | doesn't have to be awkward or inefficient.
        
         | tester756 wrote:
         | >WASM-compiled interpreters and runtimes
         | 
         | in which version?
        
         | 999900000999 wrote:
         | I'd love that too, if I was a better programer I'd likely fork
         | Chromium and build in Python support.
        
       | spicybright wrote:
        
         | xapata wrote:
         | You could have asked for benchmarks without saying, "Yaaawn." I
         | think the author would be more likely to volunteer to improve
         | the code if it were encouraged rather than yawned at.
        
       ___________________________________________________________________
       (page generated 2022-03-26 23:00 UTC)