💾 Archived View for dioskouroi.xyz › thread › 29348411 captured on 2021-11-30 at 20:18:30. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Transcrypt – Python in the Browser

Author: EntICOnc

Score: 87

Comments: 24

Date: 2021-11-26 08:35:15

Web Link

________________________________________________________________________________

JasonFruit wrote at 2021-11-30 15:33:23:

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.

jokoon wrote at 2021-11-30 16:28:25:

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 at 2021-11-30 17:00:55:

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.

BiteCode_dev wrote at 2021-11-30 18:15:35:

- 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

jokoon wrote at 2021-11-30 17:36:25:

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.

chrisseaton wrote at 2021-11-30 15:37:26:

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.

_ZeD_ wrote at 2021-11-30 16:35:39:

> Python is, IMO, a moderately better language than JavaScript

comprehension alone are a game changer - language wise

garyrob wrote at 2021-11-30 16:51:34:

JS's closest equivalent to comprehensions. Not as nice in my book, but really not bad either.

https://sebhastian.com/list-comprehension-javascript/

jkrubin wrote at 2021-11-30 16:40:13:

Js has awesome map filter reduce tho. I used to wish js has list comprehensions but now I prefer map filter reduce.

Scarbutt wrote at 2021-11-30 18:02:45:

Hardly a game changer

IshKebab wrote at 2021-11-30 15:49:14:

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

eatonphil wrote at 2021-11-30 12:56:11:

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

guipsp wrote at 2021-11-30 13:58:21:

You don't need a server, you just need to transpile beforehand.

eatonphil wrote at 2021-11-30 14:25:33:

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.

CivBase wrote at 2021-11-30 14:52:38:

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 at 2021-11-30 16:11:04:

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 at 2021-11-30 16:27:18:

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 at 2021-11-30 18:01:48:

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 at 2021-11-30 16:59:16:

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.

eesmith wrote at 2021-11-26 09:14:34:

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 at 2021-11-30 12:38:19:

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 at 2021-11-30 12:44:30:

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

hencoappel wrote at 2021-11-30 12:13:53:

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

wilsonfiifi wrote at 2021-11-30 13:18:06:

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/