Python in the browser?

I like Python and dislike JavaScript.

Unfortunately it seems like you can’t avoid JS if you want
your code to run in the browser.

I want to do some simple HTML/DOM/CSS fun, and it would
be very cool if I could use Python for it.

Do you have a solution?

1 Like

What have you tried?

https://duckduckgo.com/?q=python+in+the+browser

The first two links look good to me.

Do you mean simply running Python in a web-page, or do you mean running Python as your client-side web scripting?

The first link (Trinket) is a commercial service.

I am looking for an open source solution.

The second one (skulpt) tranlates to JavaScript. I will have a look at this.
But me feeling is that a wasm based solution would make more sense today.

I would like to do simple HTML/DOM manipulation, like jquery.

Sure, but please answer first the question of Laurie O first , (or your Private messages) , where will Python run and more specifically where is hosted the source code ?

manipulating the DOM can be done via websockets when python is remote eg https://github.com/amirouche/beyondjs, but if python is running inside the browser it needs an async framework + async RPC.
These are totally different ways and the last is quite complicated, why not then just use a web GUI framework like https://github.com/dddomodossola/remi/ ?

What private messages are you talking about?

Where is hosted the source code? The source code should be hosted where the developer wants it it. For example on github. I don’t care if python code gets transmitted from the server to the client. For example a wasm based solution would not transmit python code to the client. As long as I can execute my code inside the browser.

I see two ways: wasm or transpiling from Python to JS.

beyondjs: this transmits every dom event over the network from the client to the server. I think this will be much too slow.

remi: Python gets executed on the server side and creates HTML. This is roughly like django forms library. That’s cool, but not what I am looking for.

transpiling from Python to JS

that may lead to problems with refcounting when using python libraries that were designed with c-api. It happened here for example https://github.com/brython-dev/brython/issues/275 so asm.js (python2) and later wasm(py3) were choosen instead of transpilation.

if you are thinking of a bytecode only solution like eg only keep cpython VM, then it would not be Python but a subset of it because you would loose compile and eval : that’s good for size but …

But bytecode opcode map ( ceval.c ) is subject to change anytime each version so it’s a risky way with possibly multiple runtime maintenance ( for ever ?). I would choose to embed code in page or get it from a server like js but please without minifying :wink:

beyondjs

Right it is slow for remote dom, also probably very slow for webgl/canvas in a notebook as questionned there https://github.com/panda3d/panda3d/issues/887

but maybe not if running Python client side, problem is running a worker or async and communicating with js host is not standardized yet.
One could hope for a decent -fast- serialization module both side first ( may CBOR - RFC 7049 ? ), and perhaps direct access to utf-8 strings in wasm heap.

I see you have found the privmsg, so please try and elaborate on what you need or would like to achieve first with a Python in the browser : it’s uncharted territory and each player on the field may have different objectives.

Yes, I agree. It is the first time I see someone like OP that wants to (really?) do Python in the browser. Most people think Python in the browser will have the same properties as Qt or GTK dev. That is unlikely to happen and people have already tried. That is, even with a good Python implementation that runs in the browser, you will still need to learn CSS and browser API. Another misguided idea, is that Python in the browser must have a broad compatibility with existing Python packages and stdlib. That is wrong on some many ways, to get started most of the stdlib is backend specific, second even things like urllib would need to be re-implemented to make use of, again, browsers apis.

You could try pyodide, though as mentioned above you still would need to be aware of JS aspects in any case when running in the browser.

Another misguided idea, is that Python in the browser must have a broad compatibility with existing Python packages and stdlib. That is wrong on some many ways, to get started most of the stdlib is backend specific, second even things like urllib would need to be re-implemented to make use of, again, browsers apis.

I guess there are several ways of looking at it. For instance, pyodide mostly passes the CPython test suite, aside from tests involving threading, multiprocessing, sockets and other unsupported things in the WASM environment. Similarly one can install pure Python packages from PyPi and they would work assuming they don’t use too much of the above unsupported functionality. urllib etc yes would need re-implementing with browser APIs but potentially this could be done?

Thank you all for your answers.

Some days later I ask myself: Thomas, why not use JavaScript?

I use Python since 2001 (19 years). Of course Python is simpler and more clean…

But I think you can’t avoid JS at the moment, if you want to work with HTML in the browser.

I think I will have a look at JavaScript, TypeScript and React.