And Brython [3] is a completely different option without long load time: a Python interpreter implemented in JavaScript.
If load time is important, Brython is pretty nice. If feature completeness is important, Pyodide and Coldbrew are probably best.
[0] https://github.com/pyscript/pyscript/blob/main/pyscriptjs/sr...
[1] https://github.com/pyodide/pyodide
https://anvil.works/blog/python-in-the-browser-talk
(We chose one of the Python-to-JS compilers, https://skulpt.org, for the Anvil web-app platform, because it's much lighter weight than this. It's a couple of hundred kb on the wire, and you can call to the server for any heavy lifting.)
https://pyscript.net/examples/
(This was presented today as a pycon keynote talk!)
from datetime import datetime as dt
from utils import add_class, remove_class
from js import console
tasks = []
# define the task template that will be use to render new templates to the page
task_template = Element("task-template").select('.task', from_content=True)
task_list = Element("list-tasks-container")
new_task_content = Element("new-task-content")
def add_task(*ags, **kws):
# create task
task_id = f"task-{len(tasks)}"
task = {"id": task_id, "content": new_task_content.element.value, "done": False, "created_at": dt.now()}
tasks.append(task)
# add the task element to the page as new node in the list by cloning
taskHtml = task_template.clone(task_id, to=task_list)
taskHtmlContent = taskHtml.select('p')
taskHtmlContent.element.innerText = task['content']
taskHtmlCheck = taskHtml.select('input')
task_list.element.appendChild(taskHtml.element)
def check_task(evt=None):
task['done'] = not task['done']
if task['done']:
add_class(taskHtmlContent, "line-through")
else:
remove_class(taskHtmlContent, "line-through")
new_task_content.clear()
taskHtmlCheck.element.onclick = check_task
def add_task_event(e):
if (e.key == "Enter"):
add_task()This seems highly unlikely. Each browser maker would have to have two teams now (one for JavaScript and one for Python). An additional language would add a new security/hacking vector. They would need to keep both languages at parity which would probably slow development and make testing much more complicated. Would JS and Python be able to both work on the same web page at the same time? What if both tried to interact with the same element at the same time? This is just thoughts off the top of my head. I’m sure the real issues would be much more complicated.
# add the task element to the page as new node in the list by cloning
taskHtml = task_template.clone(task_id, to=task_list)
taskHtmlContent = taskHtml.select('p')
taskHtmlContent.element.innerText = task['content']
taskHtmlCheck = taskHtml.select('input')
task_list.element.appendChild(taskHtml.element)
We can surely make such things easier in 2022?A few brief delays came with the matplotlib examples but nothing too bad (seconds not minutes)
Which curiously wasnt included.
Does Python have some libraries that JS is missing? (I could think of AI/ML libs) The examples [1] seem so slow in my browser (Firefox on an Intel-based MBP) that they are borderline unusable for me (and some of them just straight out don't load within 60 seconds, like the Bokeh, at which point I give up).
If you are already using Python on the backend, not having to hire/train a completely different skill set/stack is appealing. Sure, the browser and CSS/HTML specialism is going to remain, but not having to split your team by JS vs <backend-language> would make it much easier for a team to collaborate and share work.
I think Python in the browser only has to be “almost as good” in a direct comparison for it to actually win out as the best choice organizationally for many teams.
Also the idea is you CAN now access those libraries like numpy, even though Python speed in general is usually overstated.
This isn't good for most web use cases but there are some major new use cases that it enables.
Pyodide is in Python documentation all over the place, for instance on https://numpy.org/
Python has many libraries that JS is missing. Particularly the scientific computing ecosystem has a lot of very well supported Python packages but fewer JavaScript packages. They also can benefit a lot from connection to a UI.
Two key use cases for this stuff are Python education and scientific computing. Students have a hard time installing Python locally. Generally scientists want to share their results broadly, but they probably did their analysis in Python.
Here's another example of a good use case: https://www.socialfinance.org.uk/blogs/analysing-sensitive-d...
PyScript is a Pythonic alternative to Scratch, JSFiddle or other "easy to use" programming frameworks, making the web a friendly, hackable, place where anyone can author interesting and interactive applications.
Regarding the size of the download, I know there is ongoing work in the Pyodide project to create a much smaller python runtime that will help tremendously. Also, the script will be cached once you download it once.
EDIT: Also, you don't need NPM or anything locally to run these
But some other low hanging fruit include unvendoring the special encodings for Asian languages (hopefully everyone uses utf8), the decimal library, and the xml library which are all quite large and only occasionally used.
https://github.com/pyscript/pyscript
Would be nice if there was a demo on the page that didn’t require installing NPM and other stuff on my machine.
A public marketing page where it's the random internet of totally new/anonymous users who want to instantly find out info about your product would be the worst use case for this IMHO.
Any reason to use a custom element `py-script` vs a script tag with custom type?
This is the actual CPython runtime with full standard library and many of the Python mathematical and plotting libraries compiled to WASM, running in the browser. It can also be included with other HTML and JavaScript on a web page just by wrapping the Python in a tag.
Of course it would be better if Chrome shipped with Python support, but this is a good effort. Does Wasm generally support other languages ?
This looks like a very interesting world to be apart of , I'll look at this tomorrow.
Asking these web devs turned desktop app developers to learn python is probably a bridge too far…