Brython: an implementation of Python 3 running in the browser
github.com
github.com
Brython: Python in the Browser - https://news.ycombinator.com/item?id=26781461 - April 2021 (1 comment)
Brython – A Python 3 implementation for client-side web programming - https://news.ycombinator.com/item?id=23746067 - July 2020 (214 comments)
Brython – Client-side web programming in Python - https://news.ycombinator.com/item?id=15914805 - Dec 2017 (1 comment)
Brython – A Python 3 implementation for client-side web programming - https://news.ycombinator.com/item?id=9792536 - June 2015 (60 comments)
A Python 3 implementation for client-side web programming - https://news.ycombinator.com/item?id=5807059 - June 2013 (117 comments)
Brython, Python in the browser - https://news.ycombinator.com/item?id=5133047 - Jan 2013 (65 comments)
Brython - Python to Javascript translator - https://news.ycombinator.com/item?id=4923530 - Dec 2012 (68 comments)
(That last thread and the June 2013 thread are good for anyone worried that HN has become more negative lately...)
The GitHub page is linked at the end.
Some articles/content that relates to this ([4] is pretty cool and definitely check it out, so is [6]):
[1] https://brython.info/static_doc/en/intro.html
[2] https://towardsdatascience.com/run-python-code-on-websites-e...
[3] https://stackabuse.com/an-introductory-guide-to-brython
[4] https://rayluo.github.io/brython-project-template/
[5] https://github.com/rayluo/brip
[6] https://github.com/brython-dev/brython/wiki/Brython%20in%20t...
[7] https://academy.cs.cmu.edu/ (CMU's academy website runs on Brython)
Here's the GitHub page: https://github.com/brython-dev/brython/
And the Wiki, for examples -https://github.com/brython-dev/brython/wiki/Brython%20in%20t...
If you're familiar with all that's going on with the source code or at least some parts of it, and have time to spare, do take the time out to contribute to this project. It's pretty insane what they're building.
You can join their Google Groups forum here: https://groups.google.com/forum/?fromgroups=#!forum/brython
I work exclusively as a DevOps/DataOps/Backend engineer and have little contact with stuff running in the browser. I did, however, work with AngularJS back in the day, and although the framework itself didn't lend itself to nicely to productivity and simplicity, I believe that what I found most confusing was how unergonomic the browser environment itself was, from not dealing with a filesystem (and thus having to resort to tricks like transpiling and bundling) to the rather terse API, not having for instance a SQLite natively and not being able to enable something like this without bringing copious amounts of megabytes of extra javascript deps...
Is the browser environment becoming more dev-friendly with the adoption of WebAssembly? I ask that because, in my opinion, if the browser itself doesn't evolve, it doesn't matter which language we use, interacting with it will still be a hassle. All in all, it feels like the browser advanced a lot in technology for optimization and resource management, but not so much in becoming more user-friendly to the developer.
We had this for a brief moment in the form of Web SQL, but as a rule web standards require independent implementations to be developed, and all the implementors were using SQLite under the hood. The current alternative is IndexedDB - for better or worse.
> Is the browser environment becoming more dev-friendly with the adoption of WebAssembly?
Yes, but that doesn't have much to do with WebAssembly.
JavaScript as a language evolved, we have TypeScript with its flexible and expressive type system and bundling is becoming less of a pain with each consecutive framework re-written using ES2015 modules(and loaded via e.g. Snowpack https://www.snowpack.dev/).
WebAssembly is helpful when there's a need for some heavy lifting - e.g. in software codecs, but that's still a niche application.
If WebAssembly replaced js, then newer, more efficient browser languages could be invented. Like a new implementation of typescript could be created that is statically compiled and more efficient. All of this transpiling from x to js and y to css would be unnecessary.
But it's going to be a very "heavy" custom framework that takes time to download and load. Overkill in most cases.
WebGL is a type of HTML5 canvas context. Unfortunately, there are major drawbacks which I also outlined - it's too low level and loses platform consistency, accessibility, usability, and really every affordance of an operating system - you're starting over from scratch and loading in dependencies or writing your own implementation of some immensely challenging concepts (font rendering on its own is usually several hundred kb of code and could fill textbook upon textbook).
If you're familiar with GL UI frameworks like dear-imgui, they do all work pretty well in WebAssembly. But again, they're compromised from the start because you lose all OS consistency and accessibility. And unlike in games, where these frameworks are often employed, OS consistency isn't expected, and accessibility is sadly ignored, the experience of a cross-platform UI framework appearing in a canvas element on the web becomes jarring very quickly.
There is an alternative approach, like Google Docs employs and the sibling poster linked - combining the DOM for input elements with custom rendering. But, this is really, really complex and challenging.
Every time I develop applications for the web, I yearn for an application-focused presentation layer which provides the expected fundamentals around accessibility, OS consistency, and rendering layout while ditching the hacks-on-hypertext minefield of CSS and the DOM.
However I do see the appeal of the ergonomic of the TypeScript syntax. I hope TC39 will add something like optional type annotations to the spec.
tbh i haven't checked, but isn't the transpilation step for typescript just stripping out the type annotations? TSC is build-time only, so it won't factor into your bundle size, and i can't imagine the generated JS is significantly bigger than source -- if anything, it should be smaller ;p
[0] https://dev.to/haruanm/does-typescript-increase-the-bundle-s...
I would guess running the TypeScript output through the Google Closure compiler (or adding similar optimizations directly to the TypeScript compiler) could make TypeScript bundles comparable to JavaScript bundles or smaller. It looks like people see significant improvements running TypeScript output through Closure compared to just WebPack [0].
[0]: https://medium.com/appmonet/using-closure-compilers-advanced...
Does the web browser (an application for reading documents written in hypertext), need to evolve? Or, has the web browser been asked to do more than it should and we need a new type of application? So much of the internet is based around the fact that the basics of the web browser can't change (don't break the web!). If the basics can't change then maybe the need for a new standard where the basics fit the application is required.
If I had a magic wand,
- I'd make Web maximally interoperable, give an API to everything: look up Youtube recommendations or listen to Spotify programmatically or from the command line;
- introduce legislation to forbid non-interoperable for-profit (or infrastructure-level) software; otherwise businesses are incentivized to make walled gardens and lock-in users; e.g. no more countless chat apps;
- get rid of Javascript: it creates too many security and fingerprinting problems, and, if everything has an API, you don't need it;
- break up the browser into a composition of smallest independent components possible; it will get you `apt install facebook-local-events`;
- get rid of password-based authentication: what a horrible idea that was;
- get rid of any client-side possibility to track or fingerprint users; if you're making choices about what services to use or how, those choices can be fingerprinted, depending on some other things, but that's another matter; the other party should not be exposed to what hardware or software or configuration you use or how you use your peripherals;
- introduce consumer and competition protection laws to bring software out of the wild wild west in terms of legislation.
I've more or less described the destruction of Web as we know it, but also a creation of something much better, in my eyes. Doesn't sound very feasible, but that's what I'd do if I had a magic wand.
Blazor, Unity, Uno, Qt, TeaVM, the list is only getting bigger and it will only stop if WebAssembly is removed from the browsers.
Pandora box is open and the lid won't close.
Web Assembly isn’t going to get rid of the sandbox.
Sandbox doesn't imply not having a file system available (for example, for use in imports), I believe this is more of a historic limitation than a strictly technical one (just because we need the sandboxing concept).
I know. Fakeroot is not strictly sandboxing, but I think it illustrates the point.
Pyodide is used in https://jupyterlite.readthedocs.io/
Maybe things will improve with web assembly GC, though it will require work to port GC languages to it.
The existence of both sentiments speaks to the idiosyncratic history of the web. The browser is powerful because it has grown to be ubiquitous, but also has this odd dual mandate of displaying static content and running dynamic apps.
Saying that I am really interested in Webassembly and think the word web in Webassembly does it a disservice.
I have started to think of Webassembly as a light weight JVM. It's heavy sandboxing makes it really interesting as an evolution to FaaS (Function as a service) such as AWS Lambda. Lucet which Fastly is using for their Compute@Edge reports 35 micro second code start time! I can't wait until Fasly put there Compute@Edge on general release so I can try it. I've been playing with Cloudflare workers and as they us Chrome to run WASM files you have to mess about writing Javascript shims so a bit rough around the edges debugging.
Webassembly should realise the original goals of serverless providing a cross platform runtime with security guarantees and be fast and lightweight.
Most people package an entire OS in Docker containers, with WASM they get a small cross platform artifact. This project already looks like a way to run them in K8s https://github.com/deislabs/krustlet.
It also looks great for cross platform media devices. Have the bulk of your code as a WASM binary and then have very thin platform specific shim the WASM interacts with.
WebAssembly provides an amazing opportunity to do general computing in webpages more efficiently than with the JavaScript scripting language, but it's not designed to interact directly with the user or documents. Certainly that _could_ change in the future, but that's not one of its design goals and I don't think it would even be particularly effective at it.
What are there the performance and usability consequences of each approach (py to js vs. py to wasm)?
The short answer is that basically all the current Python-in-the-browser implementations (and there are a few!) predate widespread support of WASM. But even once WASM is a thing, you get a choice between:
1. Compile a full Python environment to WASM (via Emscripten), and load it in the browser. This is what Pyodide does - it lets you use a boatload of existing libraries, but you have to download and bytecode-compile the world to do it. On my machine Pyodide takes 10+ seconds to load. (PyPy.js, which predated WASM, was even worse!) The upside is that you can run more or less anything from the existing ecosystem. This tradeoff is great for Pyodide's use case (notebooks in the browser) - long load times are OK, but you really want those numerical libraries.
2. Ship a lightweight, JS-based runtime. This is what Brython and Skulpt do. You'll pay in performance (compilation overhead, interpretation, and the overhead of preserving Python rather than JS semantics for things like attribute lookup), but your payload will be much smaller (Skulpt is ~250kb on the wire), and you'll be interactive much faster. We chose Skulpt for the client side of our full-stack Python-in-the browser platform, Anvil (https://anvil.works). Unfortunately, you'll be amazed how many Python libraries don't work in this environment. Most "normal" Python code is pretty straightforward, but dig into any big library and you'll find a native dependency really fast. (Skulpt's original 'datetime' implementation was ported from PyPy, and even that depended on native code. In PyPy, the interpreter written in nearly pure Python!) In something like Anvil, this is no biggie, because you can call server-side code with one function call, but it could be a nasty surprise in a traditional app.
3. Compile "not quite Python" to JS (this is what Transcrypt does). This more or less transliterates Python code to the nearest Javascript equivalent. This produces really compact, JIT-friendly JS code, but it will bite you the moment you rely on Python semantics that aren't there in JS. Worse, you're now neither fish nor fowl: If you have a question, you can't consult StackOverflow for Javascript answers, but half the Python answers won't work for you.
(To my knowledge, nobody has yet attempted the "WASM + lightweight runtime" approach. We've occasionally discussed it, but it would be hard to retrofit to an existing project, because you'd want to move a-lot-but-not-all of the runtime into WASM to avoid being hammered by the JS-WASM bridge, and building a Python runtime from scratch is a heavy lift...all for a performance boost you might just get from generating faster JS.)
Before the pandemic, a colleague of mine wrote an in-depth comparison of six popular Python-in-the-browser implementations, with sample code and a discussion of their trade-offs, with a lot more detail than I can fit into this comment: https://anvil.works/blog/python-in-the-browser-talk
MicroPython has a version working in the browser using unicorn.js (which emulates ARM CPU). [3]
[1] https://micropython.org/ [2] https://sneklang.org/ [3] https://micropython.org/unicorn/
I've only been exploring Pyodide for the 10 minutes since I switched over and discovered these two pieces.
I'm curious if there's a good reason to use Brython over Pyodide.
Edit: Ok one weird thing with Pyodide is that I cannot use subscripts with JavaScript objects in Python. I have to use dot accessors. So not `row["age"]` but `row.age`. Kinda weird, not too Pythonic.
Transcrypt's (https://github.com/QQuick/Transcrypt) approach made the most sense for me. The project seems to be a heroic effort by one guy.
This is Carnegie Mellon's academy website, running on Brython. See the Brython Wiki page on their GitHub, they got a list there.
I wrote a bit more about my experience using it (and a guide for others) here: https://datastation.multiprocess.io/blog/2021-06-16-language....
My biggest gripe is that so far I haven't been able to get good stacktraces out of it on errors. It also wraps all objects in custom Brython types that you have to rewrap in Python types to do stuff like get `.keys()` on JavaScript objects.
I guess being able to take the C parts of extensions and have them compiled with emscripten would be a logical step further.
https://github.com/pyodide/pyodide
They offer pure client-side Jupyter notebooks. Wasm compiled, no cloud servers needed.
This brings standard PyPi packages to Brython
While I love Python and feel most comfortable with it, it always bothered me that it make a distinction between accessing a dictionary item and accessing an object member. E.g.:
a["getStuff"]() # call the "getStuff" function of dictionary "a"
b.getStuff() # call the "getStuff" method of object "b"
In contrast, in JavaScript, these two are equivalant. a["getStuff"]()
a.getStuff()
I find JavaScript more dev-friendly. Any idea why Python didn't opt for the same approach? Would make sense to me, since "everything is a dictionary/object".In theory, implementing attribute-style access on user-defined Python object is just a matter of overriding its __(get|set)attr__ methods, for example:
In [1]: import collections
In [2]: class DotDict(collections.UserDict):
2 def __getattr__(self, k):
3 try:
4 return self.__getitem__(k)
5 except:
6 return super().__getattribute__(k)
7 def __setattr__(self, k, v):
8 try:
9 self.__setitem__(k, v)
10 except Exception as e:
11 super().__setattr__(k,v)
In [3]: d = DotDict({'a': 'b', 'c': [1,2,3], 'square': lambda x: x*x})
In [4]: d.square(4)
Out[4]: 16
In [5]: d
Out[5]: {'a': 'b', 'c': [1, 2, 3], 'square': <function <lambda> at 0x7f9ef5d5b0d0>}
In [6]: import datetime
In [7]: d.created_at = datetime.datetime.now()
In [8]: d
Out[8]: {'a': 'b', 'c': [1, 2, 3], 'square': <function <lambda> at 0x7f9ef5d5b0d0>, 'created_at': datetime.datetime(2021, 7, 19, 19, 42, 32, 341393)}
In practice, this can lead to headaches. Take, for example, what happens if we try to serialize `d` (which has a non-serializable lambda function in its keys) using the built-in pickle module: In [9]: import pickle
In [10]: pickle.dumps(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class 'DotDict'>: attribute lookup DotDict on builtins failed
Thankfully there's a third-party library called cloudpickle which can serialize just about any python object to bytes -- even including the user-defined DotDict class (!): In [12]: deserialized_d = cloudpickle.loads(cloudpickle.dumps(d))
In [13]: deserialized_d
Out[13]: {'a': 'b', 'c': [1, 2, 3], 'square': <function <lambda> at 0x7f9ef650fee0>, 'created_at': datetime.datetime(2021, 7, 19, 19, 42, 32, 341393)}
Since the entire class definition is serialized along with the instance, the deserialized copy preserves the object's attribute-style interface as before: In [14]: deserialized_d.created_at.strftime("%c")
Out[14]: 'Mon Jul 19 19:42:32 2021'
But I recommend Munch over rolling your own because subclassing `dict` is fraught with a surprising number of edge cases.And so, text/python or application/python?