[HN Gopher] Transcrypt - Python in the Browser
       ___________________________________________________________________
        
       Transcrypt - Python in the Browser
        
       Author : EntICOnc
       Score  : 87 points
       Date   : 2021-11-26 08:35 UTC (4 days ago)
        
 (HTM) web link (www.transcrypt.org)
 (TXT) w3m dump (www.transcrypt.org)
        
       | JasonFruit wrote:
       | This is one where I don't see the advantages outweighing the
       | disadvantages. Python is, IMO, a _moderately_ better language
       | than JavaScript, but you don 't get the Python ecosystem, and you
       | aren't running the same code you wrote, so you won't get nice
       | stack traces or even useful error messages. You'll be writing
       | JavaScript in Python, using JavaScript libraries, but all the
       | references are written in JavaScript. It's a lot of mess just to
       | avoid writing JavaScript, which contains a pretty decent language
       | at this point.
        
         | IshKebab wrote:
         | > Python is, IMO, a moderately better language than JavaScript
         | 
         | Debatable. But either way it's definitely a much _worse_
         | language than Typescript so I agree.
        
         | _ZeD_ wrote:
         | > Python is, IMO, a moderately better language than JavaScript
         | 
         | comprehension alone are a game changer - language wise
        
           | garyrob wrote:
           | JS's closest equivalent to comprehensions. Not as nice in my
           | book, but really not bad either.
           | 
           | https://sebhastian.com/list-comprehension-javascript/
        
           | Scarbutt wrote:
           | Hardly a game changer
        
           | jkrubin wrote:
           | Js has awesome map filter reduce tho. I used to wish js has
           | list comprehensions but now I prefer map filter reduce.
        
         | chrisseaton wrote:
         | If you have some business logic that you want to run on both
         | the front-end (maybe for quick local validation) and the back-
         | end (for definitive validation) then this is useful over
         | possibly rewriting you Python into JavaScript.
        
         | jokoon wrote:
         | The base syntax of python is already a big plus when compared
         | to js.
         | 
         | It's only about avoiding the ambiguity of js, it js in general.
         | 
         | I remember making a small webapp in flask and ajax. Most of the
         | code is on the server, it's as little js as possible, and it
         | has no js dependencies.
        
           | peanut_worm wrote:
           | What do you think is better about Python's syntax? I
           | generally like modern JavaScript's syntax. I always figured
           | the appealing part about python is the very good standard
           | library.
        
             | jokoon wrote:
             | List comprehension, the indent syntax that makes it very
             | readable, the builtin functions, globally everything. The
             | philosophy of python is well described here:
             | 
             | https://www.python.org/dev/peps/pep-0020/
             | 
             | I especially likes this one:
             | 
             | > There should be one-- and preferably only one --obvious
             | way to do it.
             | 
             | It's true that the standard library is good, but generally
             | I do more things with basic python than the base of other
             | languages.
             | 
             | And honestly I've never understood how js prototypes work.
             | 
             | Python just feels better. The syntax is less cluttered.
             | That's a huge point of python.
        
             | BiteCode_dev wrote:
             | - sequence[-1]
             | 
             | - sequence[start:finish:step]
             | 
             | - no need for "==="
             | 
             | - try/except with specific exceptions
             | 
             | - context managers
             | 
             | - yield from
             | 
             | - "in" on any iterable
             | 
             | - raise from
             | 
             | - every empty object is false, all other are true
             | 
             | - spread work on all iterables, not just arrays
             | 
             | - f"{2:03}", f"{2:.2f}" and f"{date:%m/%d/%Y}"
             | 
             | - / and //
             | 
             | - no need for variable declaration keywords
             | 
             | - 0b1111
             | 
             | - "else" in loops
             | 
             | - := makes for explicit assignation + comparison instead of
             | a footgun
             | 
             | - None instead of null + undefined
             | 
             | - set literals
             | 
             | - matmul operator
             | 
             | - foo > bar > baz
             | 
             | - sorting callback semantics
             | 
             | - dunder methods for operator overriding
             | 
             | - list, dict and set comprehension
             | 
             | - generator expressions
             | 
             | - byte literals
             | 
             | - raise SystemExit() for clean script exit
             | 
             | - raw strings
             | 
             | - automatic string literal concatenation
             | 
             | - positional/keyword only parameters
             | 
             | - can use *args and **kwargs together, as opposed to spread
             | 
             | - docstrings
             | 
             | - typing is part of the language, no need for a transpiled
             | dialect like typescript
             | 
             | - "async for" and "async with"
             | 
             | That's only what I come up on the fly, I'm pretty sure I
             | can double the list. Then of course, do we consider builtin
             | to be part of the stdlib, since it doesn't require any
             | import? Because there is a load to say there.
             | 
             | JS does have some nice stuff too though:
             | 
             | - ? and ??
             | 
             | - futures are more ergonomic than coroutines and awaitable
             | anyway
             | 
             | - more powerful destructuration
             | 
             | - regex literals
        
       | wilsonfiifi wrote:
       | Played around with Transcrypt a bit. Works quite well with
       | Mithril.js [0]. Would be great if it could work seamlessly with
       | esbuild but I couldn't find any existing plugins.
       | 
       | [0] https://mithril.js.org/
        
       | hencoappel wrote:
       | Transcrypt is a tool to compile a fairly extensive subset of
       | Python into compact, readable JavaScript. It has the following
       | characteristics:                   It's lightweight, in
       | accordance Pareto's rule adapted for IT: 99% of the result can be
       | achieved with 1% of the effort.         It offers the semantic
       | essence of Python along with the clean syntax that the language
       | is famous for         It cooperates flawlessly with the
       | JavaScript universe of web-oriented libraries         It
       | translates Python into highly readable, easy to debug JavaScript
        
       | eatonphil wrote:
       | Unlike brython, pyodide or coldbrew you'll need a server to run
       | the transcrypt program to get JavaScript from your Python.
       | 
       | I use pyodide at the moment in datastation [0] to provide an in-
       | browser-memory demo environment. It means that environment is
       | completely free other than static hosting costs while you get a
       | nice (demo) computational notebook in your browser.
       | 
       | And while brython only implements a subset of Python and breaks
       | some things like dict access for JavaScript interop, pyodide and
       | coldbrew are complete cpython environments just running on wasm
       | through emscripten.
       | 
       | The biggest issue with these binaries on wasm is that they take
       | forever to load each time you come to the app. It takes like
       | 20-30s to load. That's a pretty bad experience for my use case.
       | 
       | So I'm still testing every implementation I see out to find the
       | right combo of fast load time and most pythonic implementation.
       | 
       | [0] https://github.com/multiprocessio/datastation
        
         | CivBase wrote:
         | One approach I've still never seen is native browser support
         | for Python - something like what Google's Dart team did with
         | Dartium.
         | 
         | Of course that means your site would be inaccessible to anyone
         | with a normal browser so you couldn't run it in production...
         | but were you really ever going to run something like this in a
         | production environment anyways?
        
           | dec0dedab0de wrote:
           | That would be Brython. It's a Python interpreter and part of
           | the standard library written in javascript. Along with some
           | dom manipulation libraries.
        
             | CivBase wrote:
             | I don't think that's true.
             | 
             | https://brython.info/static_tutorial/en/index.html
             | 
             | > In the <head> zone we load the script brython.js : it is
             | the Brython engine, the program that will find and execute
             | the Python scripts included in the page.
             | 
             | It looks like Brython works by embedding a Python
             | interpreter into the page as Javascript. That's 5 MB if you
             | include the standard library - almost 800 KB for just the
             | interpreter. Not to mention the computational overhead of
             | running an interpreter in an interpreter.
        
               | dec0dedab0de wrote:
               | I did say it was an interpreter written in JS, but I had
               | misunderstood what you were saying.
               | 
               | I think Grail could do what you were talking about, but
               | it never got any traction. I also vaguely remember there
               | being a python plugin floating around back in the day,
               | kind of like flash, but I can't find any reference to it.
        
           | eesmith wrote:
           | From back when (non-avian) dinosaurs roamed the world -
           | "Grail is an extensible Internet browser written entirely in
           | the interpreted object-oriented programming language Python",
           | available at http://grail.sourceforge.net/
           | 
           | > Grail lets you download Python programs that execute inside
           | Grail on your local machine. These little applications
           | ("applets") can do things like display animations, interact
           | with the user in new ways, even create additional menus that
           | pop up dialogs if you like.
        
         | guipsp wrote:
         | You don't need a server, you just need to transpile beforehand.
        
           | eatonphil wrote:
           | Sure I meant that when used in prod you'd probably compile
           | your Python to JavaScript on a (CI) server and/or if you
           | wanted to dynamically run Python in the browser using
           | Transcrypt you'd have to send the code to a server to be
           | translated before you could run the resulting code in the
           | browser.
           | 
           | This all sounds pedantic but it's because not all "python in
           | browser" implementations work like this.
        
       | eesmith wrote:
       | It's come up before on HN
       | (https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...
       | ) but not much in the way of comments.
       | 
       | It was certainly not something I knew about before.
       | 
       | Got the demo to work. Seems neat. But I'm so far on the back-end
       | these days all I can do is nod my head politely.
        
         | smitty1e wrote:
         | Somehow seeing the function keyword to the right of the '=' in
         | Javascript bothers me, and I'd rather not do JS if possible.
         | 
         | JSON is a win, though.
        
           | nicoburns wrote:
           | > Somehow seeing the function keyword to the right of the '='
           | in Javascript bothers me, and I'd rather not do JS if
           | possible.
           | 
           | Ah, but you've hit against ones of Python's biggest
           | limitations compared to JavaScript (the two languages being
           | semantically very similar): poor support for functional
           | programming paradigms. And that is in no small part due to
           | limited lambda/closure support (no multiline lambdas).
           | 
           | You can also use arrow functions to avoid the function in
           | modern JavaScript.
        
       ___________________________________________________________________
       (page generated 2021-11-30 23:02 UTC)