Passing data from JS to pyscript

Hi, I have this little website for a school assignment where I have to make a form and when submitted, there will be a table at the bottom of the page displaying all the data from the submissions. Since just storing the data temporarily would be too simple, I wanted to challenge myself, and that’s when I got the idea to use pyscript and somehow get the form submission data and store it in a CSV file. But the problem is, I don’t really have a clue on how to do it other than figure out how to:

  1. do some error handling on submit on JS then pass the data to python, or
  2. add an onclick event listener from pyscript, and when its triggered, it gets all the submission data from which I can use to handle errors then finally write it to the CSV file

…so does anyone know how to do it?

I wrote an article on how to setup JavaScript Event Handlers in Pyscript. The process is very simple. The key is to use the pyodide.create_proxy to create a JsProxy for the Python callback. Then you can process form submit events in Python.

Link to my article:

@farzanakhtarfakhir The saving part is obviously tricky. The browser sandbox prevents access to the filesystem.

Except…when it doesn’t! There’s a File System Access API that could do what you want. It’s just barely supported but perhaps very-recent-Chrome is enough for you?