17 ms·
Pyodide: Python for the Browser
- adsharma 5y agoWhy not transpile python to JS instead of running an interpreter?
- Daegalus 5y agoBecause many want JS to die in a giant hellfire and then shat on the ashes. So alternatives are being made that don't rely on it.
- adsharma 5y agoIs dart your preferred alternative?
- amitport 5y agoThe parent is talking about transpiling python to JS. I don't see why people who hate JS would like python. The language itself is not any better IMO (excluding ecosystem here). edit: following some comments I want to add that the situation is symmetric IMO. I don't see the point in transpiling any of the two to the other.
- egeozcan 5y agoIt's just what you are used to. I dislike Python, I hate PHP, but I really like JS. I've seen people like PHP and hate Python too. You see more JS hate because it's a more common scenario that people happy with PHP/Python/Whatever would be exposed to JS later, than the other way around. You see a bit more Python hate lately because ML is all the rage and Python is to ML what JS is to the web. All mentioned languages having some weird backwards compatibility quirks doesn't help either. I find it good that people are trying to port their favorite languages, although personally I'd stay on the happy path for the target platform. Especially the browser, as it already has weird APIs, JS or not.
- amitport 5y agoI agree, I was fluent in JS and other languages before learning python and hated it for a while. Eventually, you get used to the bad parts of any language so they become mostly transparent (and every language has bad parts). Specifically, now that I'm very much OK with both python and JS I find transpiling one to the other kind of pointless in terms of any language property (can make sense in terms of ecosystem).
- adsharma 5y agoAggressive type coercions in js frequently comes up 16 == [16] true in js false in python
- egeozcan 5y ago>>> a = 5 >>> b = 5 >>> a is b True >>> a = -4 >>> b = -4 >>> a is b True >>> a = 300 >>> b = 300 >>> a is b False Every language has these, JS a bit more (because mostly impossible to fix thanks to backward compatibility). I don't understand why you would do that anyway. Most linters would display errors even if you did (in both languages).
- adsharma 5y agoGreat. Thank you. I'll remove the "is" operator from the supported subset and perhaps rewrite the commonly used "a is None" to idiomatic code in the target language.
- h8hawk 5y agoYou should always use 'is' with None. == Is operator that can be overloaded and 'is' is object identity comparison. It make sense when you understand object system. You will be terribly messed up when use some numpy array == None.
- el_oni 5y agoa==b would be true. The is operator is not the same as the equality operator The is operator checks to see if both point to the same area in memory. Small numbers are preallocated in python so they point to the same memory. >>>a=300 >>>b=a >>>a is b True
- amitport 5y agoOf course there is some internal logic. The point was that both languages have pitfalls.
- globular-toast 5y agoPython is loads better than JS. I have experience with both.
- deleted 5y ago[deleted]
- neolog 5y agoThat's https://github.com/QQuick/Transcrypt https://github.com/QQuick/Transcrypt
- adsharma 5y agoI'm looking into a python -> dart -> js approach. Emphasis on static type checking and small feature set that's easy to understand. https://github.com/adsharma/py2many/ https://github.com/adsharma/py2many/ Made a new release earlier today: https://adsharma.github.io/py2many0.2.1/ https://adsharma.github.io/py2many0.2.1/
- rtpg 5y agoPython's object semantics are extremely different from JS. There might be _some_ ways of emulating them with JS thanks to Proxy objects nowadays, but you're looking at a pretty big uphill battle I think.
- adsharma 5y agopython has a lot of features. But people use a small subset of it. So instead of supporting all of python, why not stick to the small subset that people actually want to use to solve a problem?
- ptx 5y agoDifferent Python programs use different subsets, so as you work towards supporting more programs the union of the subsets becomes essentially the full set, I would assume.
- adsharma 5y agoThe distribution of popularity of the features is not uniform. My intention is to carefully look at the intersection of relatively less used features and features that make python slow/inefficient and exclude them from the supported subset.
- mjburgess 5y agoare you sure? can you give an example? personally, i think pythons object model is extremely similar to js -- down to python essentially having prototypal inheritance
- chc 5y agoI don't think you have that quite right. Objects don't inherit from other objects in Python — you can't do `class Foo(object())`, for example. Python has class-based inheritance.
- 5y ago
- rthz 5y agoBecause a large part the Python ecosystem uses packages (and stdlib modules) with C extensions and you can't easily transpile those. The end goal of Pyodide is to be able run existing projects mostly without modification in the browser, and that is not possible if one doesn't support Python C extensions. (I'm one of the maintainers)
- saynotobloat 5y agoCan pyodide be run on in a server environment? If there is a wasm runtime on the server. Sorry my knowledge in this is limited.
- randomfool 5y agoTry it in JupyterLab using JupyterLite- https://jupyterlite.readthedocs.io/en/latest/_static/lab/index.html https://jupyterlite.readthedocs.io/en/latest/_static/lab/ind... (choose the pyolite option for Pyodide) This project is actually quite impressive, I believe they've even gotten some pip install paths working??
- rthz 5y agoYes, installing pure Python packages that have wheels from PyPi works. Assuming they don't use functionality that's not supported by the WebAssembly VM such as multiprocessing, threading (for now), and sockets.
- BiteCode_dev 5y agoAlso, Basthon: https://notebook.basthon.fr/ https://notebook.basthon.fr/
- mjburgess 5y agoI'm having trouble getting anything plotted using this -- either via plotly or matplotlib (they don't appear to have seaborn available). Have you (any one else) succeeded? Not being able to display plots seems a major limitation.
- charleover 5y agoThis works for me in Chrome 90 on Windows import matplotlib.pyplot as plt plt.plot([1,2,3,4,5], [1,4,3,2,5]) plt.show()
- mjburgess 5y agoThanks -- it looks like the issue occurs when the import is in a different cell; or when the plotting cell is being re-run. Try splitting cells on the first line and re-running the second. I quickly get Recursion-Depth-Max errors.
- 5y ago
- wiz21c 5y agohttps://brython.info/ https://brython.info/
- lacraig2 5y agoIt would be helpful if someone could clarify some differences between Brython and Pyodide.
- mkl 5y agoThat's totally different. Pyodide is CPython in WebAssembly, and can use existing Python libraries (including those written in or backed by C), like NumPy, Pandas, and MatPlotLib. Brython transpiles Python code to JavaScript, so can handle some pure-Python libraries only: https://stackoverflow.com/questions/57517734/how-to-import-numpy-or-the-other-library-using-brython https://stackoverflow.com/questions/57517734/how-to-import-n...
- BiteCode_dev 5y agoBrython => a JS Python implementation. It will not behave like cPython for somethings, does not have access to the whole stdlib nor the scientific stack. But it is quite small (700ko without their stdlib) and has native bindings to the DOM. Use it if you feel like writing your web page in python (I wouldn't, I love python but 700ko+ with no framework loaded yet is way too much). Pyodine => the cpython implementation, but compiled to wasm. It's very heavy (10Mo), but it brings the real, whole cpython implementation in the browser so it will behave the same and have the entire stdlib. It also comes with optional NumPy, Matplotlib, pandas, SciPy, and scikit-learn. Use it if you want people to write python in the browser, but don't want to expose your server to their code.
- TechBro8615 5y agoHas anyone gotten this to run in a wasm runtime on the server? At some point I saw that wasmer had a fork adding server support, but it was out of date. Main challenge seems to be assumptions about running in the browser? My motivation is that I’d like to run sandboxed Python scripts on the server, in a wasm runtime embedded in C.
- qbasic_forever 5y agoWhy not just use an OCI container and runtime (docker, containerd, crun, etc) or even systemd-nspawn?
- TechBro8615 5y agoBecause it’d be cool if it didn’t require OS level sandboxing. AFAIU sandboxed embedded Python has been a bit of a Holy Grail for a long time. So if wasm gives an easy answer for it, that would be sweet!
- rthz 5y agoWork on Node.js support is in progress in Pyodide. And as you mentioned there is also the Wasmer build that could be updated.
- TechBro8615 5y agoHow does Node play into this? Can I embed a v8 engine in my C program and use pyodide, or do I need to bring Node along too? Does the compatibility layer depend on any Node-specific concepts like the event loop or garbage collector? (I think maybe V8 does its own GC? This just shows how thin my experience is with this topic.)
- jokoon 5y agoI tried brython once and wrotesome video/image gallery with it, I was quite happy, but it ended up being quite slow. I'm super excited about pyodide, although: * I can't find a discord or IRC channel to ask questions, I guess the project is still young. Last time I've seen this project, it looked like a scientific software suite, which came with a lot of stuff I don't need. * I wish there was more examples or samples, especially with events and the DOM. * I've glanced at the doc and howtos, and it seems it requires a server, which I don't understand. Brython seems much more straightforward. * I've read that memory leaks can happen in multiple places * This example seems like misleading advertisement, in my view, at least for now: from js import document x = document.getElementById("myElement") (Or I might need to read up more doc to understand how to get to that point)
- swuecho 5y agono server is required. check https://pyodide.org/en/stable/usage/quickstart.html https://pyodide.org/en/stable/usage/quickstart.html
- rthz 5y agoA chat channel is mentioned in the readme: https://gitter.im/pyodide/community https://gitter.im/pyodide/community Yes, we are working on adding more examples. Well you need to be able to serve static files. So if you are building locally you need to start a webserver, otherwise you can use the JsDelivr CDN. Memory leaks can happen if you translate objects between Python and JS. So of it is unavoidable because it's difficult to know when to destroy Python objects from JS vice versa, but lots of work on it has been done in the 0.17 release with more to come in 0.18 Why do you find the example misleading?
- jokoon 5y ago> Well you need to be able to serve static files. So if you are building locally you need to start a webserver, otherwise you can use the JsDelivr CDN. I don't understand, can't I just use offline, static files ? I would still expect to use my own webserver, unrelated to pyodine, like flask or whatever. > Yes, we are working on adding more examples. Glad to hear this :) ! > Why do you find the example misleading? I haven't really seen that example in the doc or elsewhere, and I don't understand the steps required to make such example work offline or without a webserver. EDIT: Another cool thing of brython is that could "inline" a python script inside a html file, such as: <script type="script/python"> or <script src="thing.py"></script>
- timothycrosley 5y agoI use this for the isort in browser demo: https://pycqa.github.io/isort/docs/quick_start/0.-try/ https://pycqa.github.io/isort/docs/quick_start/0.-try/ it was really awesome to be able to directly use a Python package without any wrangling or modification as is usually required with other Python on the browser solutions.
- kumarvvr 5y agoThe site is about 7 MB download. I feel Blazor WASM is a better choice in every way. To elaborate : Blazor uses C#, is more performant, .NET 6 is bringing AOT for even better performance, has an ecosystem of tools to build sites with, integrated well into the .NET ecosystem, has a great set of libraries to choose from.
- BiteCode_dev 5y agoFor what use case? The main use case for pyiodine is running python without having to setup python. If you don't want python, there is not need for it. If you do want python, there is no replacement for it.
- up6w6 5y agoThis project is amazing, but I would love to see the same effort with Julia so I can show differential equations in browser.
- protoduction 5y agoIf you want to play with Pyodide in a web notebook you can try Starboard [1][2]. A sibling comment introduces JupyterLite and Brython, which are Jupyter-but-in-the-browser, whereas with Starboard I'm trying to create what Jupyter would have been if it were designed for the browser first. As it's all static and in-browser, you can embed a notebook (or multiple) in a blog post, for instance to power interactive examples. The bundle size is a lot smaller than JupyerLite for the initial load - it's more geared towards fitting into existing websites than being a complete IDE like JupyterLab. [1] https://github.com/gzuidhof/starboard-notebook https://github.com/gzuidhof/starboard-notebook [2] https://starboard.gg https://starboard.gg
- vogon_laureate 5y agoNeat: "The 0.17 release has a number of interesting features. Support for Python asyncio has been added, so that Python coroutines can run in the browser event loop; a JavaScript Promise can be awaited in Python and vice versa with Python awaitables. Error handling has also been upgraded so that exceptions generated by Python can be caught in JavaScript; that can be done in the other direction, as well."
- punnerud 5y agoGetting started: https://pyodide.org/en/stable/usage/quickstart.html https://pyodide.org/en/stable/usage/quickstart.html PyOdide on GitHub: https://github.com/pyodide/pyodide https://github.com/pyodide/pyodide
- KMnO4 5y agoI’m concerned with the performance. It’s up to 16x slower than native Python, which we already know is 100x(?) slower than native code. So roughly 1600x more clock cycles are consumed than is necessary. I’m already jaded with the extreme bloat of JS frameworks as it is (how many times does my computer need to ramp up fans because of your React blog?). This could potentially be an order of magnitude worse.
- protoduction 5y agoI think the main usecase for Python in the browser using Pyodide is to enable scientific computing and visualization in the browser, and also as a way to ship small Python applications to those that don't have Python installed (e.g. drag and drop a CSV file which gets processed with pandas). Lastly it's useful to power interactive coding environments without needing to spawn a pod per user (and fighting off people using them to mine bitcoin). Building a website or SPA entirely backed by Python seems like the wrong use of this technology. The same goes for crunching large amounts of data.
- musingsole 5y agoThe quality of an answer is often orders of magnitude more important than the speed at which it's generated. Speed is important, but it is always secondary. You wouldn't pull this in for a snappy, reactive UI driven by Python. You pull this in so that you can re-use a server-side module to perform some activity that isn't critical and so not worth re-writing in JavaScript. In another 5 years (assuming Python is still of interest and that this solution worked), it will have been refined and optimized where maybe you do reach for it by default.
- fernly 5y agoOK, great, but this is not a new idea; in 30 seconds I was able to find three "Python in the browser" sites. Executing import sys print(sys.version) in each produced, https://www.programiz.com/python-programming/online-compiler/ https://www.programiz.com/python-programming/online-compiler... "A module you have imported isn't available at the moment. It will be available soon." Really? sys? https://www.online-python.com/ https://www.online-python.com/ 3.8.5 (default, Jul 20 2020, 23:11:29) [GCC 9.3.0] https://replit.com/languages/python3 https://replit.com/languages/python3 3.8.9 (default, May 3 2021, 02:40:41) [GCC 7.5.0] The latter two, certainly, are adequate for class exercises and demonstrations. No idea if they are good enough for production of course, which Pyodide seems to be claiming.
- staticassertion 5y ago> Running the Python interpreter inside a Javascript virtual machine adds a performance penalty, but that penalty turns out to be surprisingly small — in our benchmarks, around 1x-12x slower than native on Firefox and 1x-16x slower on Chrome. Experience shows that this is very usable for interactive exploration. I seriously question these benchmarks.
- deleted 5y ago[deleted]
- azakai 5y agoThat data is from 2 years ago. Lower down in the post it has an update for the present: > performance since the 2019 announcement has improved greatly: "Performance ranges between near native to up to 3 to 5 times slower, depending on the benchmark."
- plimeriz 5y agoShould the new front-end framework based on this be named Plazor, Plyzor or Blythor?
- FlyMoreRockets 5y agoIs this substantially better than Pyjs? https://en.wikipedia.org/wiki/Pyjs https://en.wikipedia.org/wiki/Pyjs