[HN Gopher] PyScript: Run Python in your HTML
       ___________________________________________________________________
        
       PyScript: Run Python in your HTML
        
       Author : gripfx
       Score  : 142 points
       Date   : 2022-04-30 16:52 UTC (6 hours ago)
        
 (HTM) web link (pyscript.net)
 (TXT) w3m dump (pyscript.net)
        
       | [deleted]
        
       | farmin wrote:
       | I suppose this is how I can get python into flutter app too?
        
         | anakaine wrote:
         | Now there's an Idea!
        
       | asimjalis wrote:
       | The GitHub repo has a more concrete overview.
       | 
       | 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.
        
         | obert wrote:
         | https://pyscript.net/examples/
        
       | ak391 wrote:
       | cool project, check out https://gradio.app/ as well, makes a web
       | demo with just python
        
       | stavros wrote:
       | I really really like this, but 4 MB zipped is a bit of a hard
       | sell :/ I've had a need to deploy small ad-hoc HTML tools at
       | work, though, and my Python is much better than my JS, so maybe
       | it's worth it.
        
         | kzrdude wrote:
         | For one of the examples, I saw it coming down with pyscript.js
         | - 1.1 MB, pyodide.asm data + wasm of 16 MB, and each python
         | package as a separate wheel download (cool!), matplotlib of 6MB
         | :)
        
         | qbasic_forever wrote:
         | Local intranet / work tools would actually be a good use case
         | for this as the load is small (tens, hundreds, maybe thousands
         | of employees at absolute most) and mostly the same repeat users
         | so browser caching would make it snappy for them on future
         | visits.
         | 
         | 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.
        
       | ericd wrote:
       | Pyodide is really cool, we got a Python library we were working
       | on working in JS in the browser with it, including Pandas iirc,
       | without too much trouble (though getting http requests working
       | required a little hackery).
        
       | grej wrote:
       | This is awesome and has so much potential!
       | 
       | 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
        
       | closed wrote:
       | For some reason, I can't see this linked anywhere on the site,
       | but there is an examples page:
       | 
       | https://pyscript.net/examples/
       | 
       | (This was presented today as a pycon keynote talk!)
        
         | tomatowurst wrote:
         | okay thought it wasn't working because it seems to take a long
         | time for the spinner to go away. but I'm very impressed with
         | the todo.py! This is what frontend should be and in 5 years or
         | so I wouldn't be surprised to see python taking over. If only
         | the initial payload for the python runner was smaller and if
         | only browsers started supporting webassembly/python stack out
         | of the box!!!                   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()
        
           | closed wrote:
           | I tried the clock example and it worked, but I needed to wait
           | maybe 30 seconds after page load..
        
           | mft_ wrote:
           | As a way of interacting with the DOM, this makes me very
           | uncomfortable:                   # 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?
        
             | saghm wrote:
             | I imagine that if Python were supported out of the box in
             | browsers like JavaScript is, frameworks would emerge for
             | this sort of thing. At this point though, I'm not sure I
             | think it's particularly likely that browsers will support
             | additional languages instead of just preferring people
             | compile to WASM and use some JavaScript over it.
        
         | edmcnulty101 wrote:
         | They don't seem to be working on Android Chrome.
        
           | nmstoker wrote:
           | Works fine for me on a Pixel 6XL. It does need to let the
           | pyodide files load, but they get cached, so for me, having
           | previously loaded a demo a month or so ago, it loaded the
           | Todo app almost instantly.
           | 
           | A few brief delays came with the matplotlib examples but
           | nothing too bad (seconds not minutes)
        
         | asimjalis wrote:
         | The js and css files should be hosted on a CDN otherwise people
         | copying/pasting these demos might chew up pyscript.net
         | resources.
        
           | aitoehigie wrote:
           | Or at least add the js and css files to the zip file.
           | 
           | Which curiously wasnt included.
        
       | als0 wrote:
       | I for one welcome our new WASM overlords
        
       | VWWHFSfQ wrote:
       | Is this similar at all to the old pyjs/pyjamas project? I had
       | some fun playing with that but there was no way I would have ever
       | actually made anything serious with it.
        
         | grej wrote:
         | That was a very cool project, though I believe they aren't
         | really related, even though they seem similar. Pyjs/Pyjamas was
         | a more of a Python to JavaScript transpiler.
         | 
         | 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.
        
         | prpl wrote:
         | I made a few internal UIs with it. It was easy for that and
         | worked well (and fun). This was 2010
        
       | hoten wrote:
       | Super cool.
       | 
       | Any reason to use a custom element `py-script` vs a script tag
       | with custom type?
        
       ___________________________________________________________________
       (page generated 2022-04-30 23:00 UTC)