10 ms·
PyScript: An open source platform for Python in the browser
- Loeffelmann 2y agoHow is this different from pyodide[0]? [0]: https://pyodide.org/en/stable/ https://pyodide.org/en/stable/
- santiagobasulto 2y agoThis uses pyodide. It's more like a Python to JS transpiler (even if it's not). It's to create browser apps using Python. So it's Pyodide + a browser API.
- azakai 2y agoThey also support MicroPython as an option now and not just Pyodide, https://pyscript.net/tech-preview/micropython/about.html https://pyscript.net/tech-preview/micropython/about.html In fact the main PyScript site uses MicroPython now, I see in the dev console (micropython.mjs and micropython.wasm).
- qsort 2y agoPyodide is a port of CPython to the WebAssembly platform. PyScript is a framework that uses Pyodide to provide a framework for developing Python applications in the browser. Its elevator pitch would be "python inside script tags via pyodide".
- v3ss0n 2y agoIts a wrapper.
- bouncing 2y agoPyodide is a runtime. PyScript can use Pyodide or MicroPython. MicroPython is actually quite small. On top of the runtime, PyScript gives you some pleasantries and a lot of quality-of-life improvements for actually using Python for web programming, not just being able to run Python code.
- mg 2y agoThe homepage left me confused, and also after reading the documentation, I'm not sure what this is. I tinkered a bit to try it myself. Turns out you can throw this into your website and it will display "Hello World": <script type="module" src="https://pyscript.net/releases/2024.8.2/core.js"></script> <script type="py"> import js js.document.querySelector("body").innerText = "Hello World" </script> So it seems to be a script that looks for scripts of type "py" and transpiles them from Python to JavaScript via Pyodide? On the other hand, the demo on the homepage is a repl. And the title says "platform". So I'm still confused.
- pansa2 2y ago> transpiles them from Python to JavaScript There are projects that do that, but it looks like PyScript instead runs Python code directly. It uses a Python interpreter compiled to WebAssembly - either CPython (from Pyodide) or MicroPython.
- deleted 2y ago[deleted]
- deisteve 2y agodoes this mean we can install FastAPI in the browser?
- bouncing 2y agoI mean, maybe, but you'd have trouble with sandboxing when you try to open a socket.
- deisteve 2y agohow would i go about doing that? open up a socket via webrtc to the internet and translate incoming webrtc calls to HTTP for fastapi
- nine_k 2y agoWhat would be the point? You can't have a listening socket anyway. And if the browser has to imitate the connection, there are numerous less oblique ways than running fastAPI.
- amanzi 2y agoI used this to create a Markdown renderer a while back. You can see the very simple source code here: https://pyscript.com/@stuartmaxwell/markdown-previewer-v2/latest https://pyscript.com/@stuartmaxwell/markdown-previewer-v2/la... The app won't run well on mobile but should be fine on desktop. I used this code in a blog application I was writing in Django.
- IshKebab 2y agoThe examples were about as slow as I imagined they would be. 20 seconds to draw a simple graph.
- nmstoker 2y agoThe majority of that is the upfront one-off loading time for pyodide to get going along with required packages. Subsequent runs for things will be substantially faster.
- IshKebab 2y agoThat is a subsequent run (because I also wondered if caching etc would improve hot loads, but it doesn't).
- bArray 2y agoI've been looking for something like this, but I'm still unsure it quite hits the spot. I think that this would be very interesting if it allows a person to run a blog like a Jupyter notebook. I have a lightweight hack [1] that runs Python in markdown documents and can output basic images/graphs - which is embedded into static pages. The point was being able to write an article that somebody else could see the code behind it and test themselves, e.g. [2]. One of the browsers I test in is Netsurf with JS disabled. Unfortunately it doesn't look great in Lynx. [1] https://gitlab.com/danbarry16/pandoc-highlight-filter https://gitlab.com/danbarry16/pandoc-highlight-filter [2] https://coffeespace.org.uk/projects/langtons-ant-universe.html https://coffeespace.org.uk/projects/langtons-ant-universe.ht...
- DerThorsten 2y agoThere is jupyerlite which does exactly that https://github.com/jupyterlite/jupyterlite https://github.com/jupyterlite/jupyterlite Kernels are compiled to wasm and can therefore run in the browser. Try it out at: https://jupyterlite.readthedocs.io/en/stable/_static/lab/index.html https://jupyterlite.readthedocs.io/en/stable/_static/lab/ind... Its easy to create your own deployments, there is a template repo for that https://github.com/jupyterlite/demo https://github.com/jupyterlite/demo
- bArray 2y ago> There is jupyerlite which does exactly that https://github.com/jupyterlite/jupyterlite https://github.com/jupyterlite/jupyterlite Had a quick look, this seems quite cool! Is there alternative output is the WASM is not run? It would be good if it defaulted to a pre-run static output, otherwise users could be left with nothing?
- agumonkey 2y agoWhat other language would people choose in a browser? Lua ?
- thesuperbigfrog 2y ago>> What other language would people choose in a browser? Lua? Brendan Eich originally wanted to use Scheme. There are some Scheme in Javascript implementations: https://www.biwascheme.org/ https://www.biwascheme.org/ https://lips.js.org/ https://lips.js.org/ https://www.wescheme.org/ https://www.wescheme.org/ https://web.scheme.org/ https://web.scheme.org/
- agumonkey 2y agoYeah but I was mostly wondering about mainstream programmers. The chance of people liking a lisp are still low(to my own sadness)
- zygentoma 2y agoIsn't this the same concept as brython? (BRowser pYTHON) https://brython.info/ https://brython.info/ https://github.com/brython-dev/brython https://github.com/brython-dev/brython
- pansa2 2y agoBrython seems to be a Python => JS translator, rather than an in-browser Python implementation. Similar end result, but works in a completely different way. https://yasoob.me/2019/05/22/running-python-in-the-browser/ https://yasoob.me/2019/05/22/running-python-in-the-browser/
- miohtama 2y agoAFAIK Brython cannot translate some of popular Python libraries. Python libraries, esp. Data science ones, are often the reason why developers pick Python.
- nmstoker 2y agoYes, this is an advantage of Pyodide, and thus PyScript too, they have a fair number of packages that work directly: https://pyodide.org/en/stable/usage/packages-in-pyodide.html https://pyodide.org/en/stable/usage/packages-in-pyodide.html
- bschwindHN 2y agoOur phones are getting faster every year, we must find a way to slow things down even more.
- poikroequ 2y agoThe dawn of the PyScript web frameworks is upon us...
- amy-petrik-214 2y agoWe must think of the lessons of docker, the lessons of conda. Why, what with the web environments, so many, is there not a way to unify an environment? Of course! A virtual environment of python, on a virtual environment of the docker machine, on a virtual web browser, and there can be JIT throughout to make it only painfully slow and not excruciatingly slow! Abstraction is a Good Thing (TM) and the more abstraction, the better. There really ought to be an object oriented abstraction layer factory generator factory using Java OOP best practices, also a good thing (TM). If all goes well we may even be able to reach abstraction tier 17, which is two more layers of abstraction than the industry standard 15. See: https://www.corporate-rebels.com/blog/cia-field-manual https://www.corporate-rebels.com/blog/cia-field-manual
- bouncing 2y agoI wrote one. https://puepy.dev https://puepy.dev
- poikroequ 2y agoYou, sir, are a mad man.
- v3ss0n 2y agoI can't find any real world use case there. If you want to run ML Models you can build them to Onnx https://onnxruntime.ai/docs/tutorials/web/ https://onnxruntime.ai/docs/tutorials/web/ . Will this efficient to build browser based app in place of JS/TS ?
- h8hawk 2y agoPerhaps some people don't want to touch JS/TS
- mapcars 2y agoTS is actually has better typing than Python (mypy). And the whole React (Vue, etc) infrastructure around it. Without these two things I wouldn't even bother writing frontend code.
- pphysch 2y agoThe browser doesn't support TS though. The browser does support Python via PyScript/pyodide/etc. You can't run TS in the browser unless you jump through the same hoops that PyScript is.
- wiseowise 2y ago‘npx vite build’ is what you call hoops?
- pphysch 2y agoYou can offline transpile anything to JS to later run in the browser, TS isn't special in this way. With PyScript you can see and modify the actual code in your browser, just like vanilla JS.
- mapcars 2y agoDo you actually see and modify code in your browser today? I mean for like 5 years all you see in the browser is minified and obfuscated to the point its almost impossible to make any practical sense out of it.
- apwheele 2y agoFor folks interested in seeing a usecase, I have a panel dashboard of Dallas crime statistics at: https://crimede-coder.com/graphs/Dallas_Dashboard https://crimede-coder.com/graphs/Dallas_Dashboard Startup takes around a minute (which includes installing various python libraries + loading an external 5mb dataset). But once up and running it is quite responsive. (And works fine on my iphone.) Many businesses I work with use Tableau free versions, and just manually update the data (since the free doesn't allow direct connections to a datastore). This is a free alternative (I use github actions to build a zipped up csv file that is pulled into the environment).
- miohtama 2y agoAwesome example of using in production! Nice work. I wish we get over with the loading hickups soon. Do you know if there is any work going on to make it start faster/smaller/less janky?
- solarexplorer 2y agoMaybe some kind of snapshot feature, where you can just load an snapshot of the already initialized state?
- stackskipton 2y agoMemory usage seems quite high. According to Browser Task Manager, the tab is consuming 657MB compared to 25-70MB most other tabs are using.
- apwheele 2y agoYes this is part of the price of running it client side instead of having a server do all the data manipulation. It is pretty much a wash for RAM for me vs running locally (local is 200-300 for python + 100-200 for chrome for just that web-page).
- bbminner 2y agoWoah, what browser/page combination can fit into ~50Mb these days? I am simply used to the fact that most pages take at least 200Mb RAM in task manager.
- jerpint 2y agoI wonder if this could be combined with fastHTML to have a full python html rendered client side
- dataguy_ 2y agoyep. would be nice as well to have LLMs trained on these things for fast prototyping
- maartenbreddels 2y agoWe plan to support fastHTML at https://py.cafe/ https://py.cafe/ This also uses pyodide, but supports more frameworks out of the box.
- SillyUsername 2y agoSomeone just invented a whole new level of hell for devs who maintain code. First it was untrained consultants and VB or Delphi (Pascal) Then came the JS monkeys mixing up plain JS with jQuery and SQL injections. Now it's time for data scientists and pi/sketch users to feel the pain of an uncharted domain...
- micahdeath 2y agoIf you could write VB (VB6) in WASM, that would make some things so much simpler!!! (I work on a legacy codebase)
- v3ss0n 2y agoThats mean you want to embed VB code into Browser Like in ActiveX days?
- pjmlp 2y agoActually I think Blazor might support class libraries written in VB.NET.
- micahdeath 2y agoThey do and it appears to work really well (I have only experimented with it.)
- micahdeath 2y agoIsn't it the same thing as a SPA or any other WASM? The only real difference now is that it is now accepted and the browsers assist with cross platform APIs (and some security) instead of pure Windows APIs. (Ok, so I made it a bit more simple than it is, but that is how I see it.)
- nazgulsenpai 2y agoOh man that would be so cool. Kinda like the Ruffle Flash player!
- bdcravens 2y agoReminds me of my first "tech" job (computer store/ISP/web dev shop, circa 1998) where the owner wrote some VBScript for the browser and wondered why it didn't work in Netscape.
- pitah1 2y agoI recently went down the rabbit hole of using PyScript for running a Python CLI app in the browser. It felt hacky the whole time, especially when dependencies were involved. I had to create wrapper classes to work around Pydantic 2.x not being available to use. I tried to put all logic into the Python files but found some things missing that I had to put in JavaScript. I think it could be good in use cases where you want some simple UI with custom UI logic on top of your Python code but maybe Streamlit or Gradio could be more suitable. GitHub repo: https://github.com/data-catering/data-contract-playground https://github.com/data-catering/data-contract-playground Website: https://data-catering.github.io/data-contract-playground/ https://data-catering.github.io/data-contract-playground/
- skeledrew 2y agoThe Rustification of a lot of Python projects is making it more difficult that necessary to use Python everywhere.
- raybb 2y agoThe big limitation that's very annoying with any WASM setup is that at the end of the day you still can't call many APIs or scrape sites because of CORS. Ofcourse, CORS is important to avoid XSS and the like but I wish there was a way to deal with this other than setting up a proxy to strip CORS, which then leaves you with a non residential IP that's more likely to get blocked.
- dsp_person 2y agoIs this only an issue with cross-origin isolation enabled with COOP and COEP?
- deknos 2y agoWhat i would like, if i could write scripts in firefox, which act the browser, like doing the evaluation at any stage i like. At GET the page, after ALL GETs. After javascript evaluation or before. Is there something which can do this? and with full power of python also accessible to the host?
- grej 2y agoAlso check out pyscript.com, to create and host PyScript apps.
- Falimonda 2y agoDoes something built off this or Pyodide exist that's effectively a replacement to using React?
- bityard 2y agoWhy does it require signing in just to try it out? Someone else below mentioned that it's just adding a <script> tag to your HTML.
- jedberg 2y agoFirst thing I tried was a small lambda function and it worked! Then I tried some Fibonacci with tail recursion and surprisingly that worked too. I'm impressed, because often when people reimplement the Python interpreter they get those wrong.
- tusharsadhwani 2y agothat's because it's not a reimplementation, it is cpython recompiled to wasm.
- pxc 2y ago> Then I tried some Fibonacci with tail recursion and surprisingly that worked too. I'm impressed, because often when people reimplement the Python interpreter they get those wrong. Huh? I thought Python didn't have TCO anyway. Guido had a whole blog post about why it supposedly doesn't belong in Python: https://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html https://neopythonic.blogspot.com/2009/04/tail-recursion-elim...
- nickpsecurity 2y agoFor those exploring, here was one of the best “Python in the browser” projects I found: https://brython.info/ https://brython.info/
- n8henrie 2y agoI tried to write a POC Firefox extension in pyscript but ended up having to fall back to pyodide. Was a fun experiment anyway. https://n8henrie.com/2023/06/write-a-firefox-extension-in-python/ https://n8henrie.com/2023/06/write-a-firefox-extension-in-py...
- skeledrew 2y agoI've been seeking to do this for a very long time!
- apples2apples 2y agoCareful Anaconda will change the license on you and then sue...
- pwang 2y agoFun joke but Anaconda has a track record of creating OSS and then turning it over to community governance. This includes the conda tool itself, libraries like bokeh, dask, numba, jupyterlab, and many more. And while PyScript project governance isn't in NumFOCUS, all of the code is permissively-licensed BSD/MIT. The commercial licenses for the products and commercial repository is what supports all of this OSS development work.
- bouncing 2y agoIf I may toot my own horn, I wrote a somewhat more opinionated, reactive, frontend framework using PyScript. It's inspired by Vue.js, a bit: https://puepy.dev https://puepy.dev
- bilsbie 2y agoHow’s it work? Webassembly? Webile?
- laffra 2y agoAlso, check out PySheets (https://pysheets.app https://pysheets.app), which leverages PyScript to run Python logic in a spreadsheet UI, which is written entirely in Python. The PySheets UI uses LTK (https://github.com/pyscript/ltk https://github.com/pyscript/ltk), a fully client-side UI rendering library for PyScript.
- a10sshg 2y agoThis is from Anaconda and built on Pyodide: PyScript is currently built on Pyodide, which is a “port of CPython to WebAssembly/Emscripten.” PyScript supports writing and running Python code in a browser. Like conda I find the whole thing very confusing and slow, but I suppose it will be marketed to academics as "the thing to do web development with". It will also be soft-abandoned. Just use Javascript, PHP or Scala like lichess.org, which is a professional website.
- maartenbreddels 2y agoAlso check out https://py.cafe/ https://py.cafe/ (I work on this project) It supports streamlit, dash, virzo and solara. Frameworks like shiny, gradio and panel are planned (maybe even fasthtml). It's also really nice to integrate this into your documentation: https://mkdocs.py.cafe/ https://mkdocs.py.cafe/