0

It would be really useful (and cool) if I were able to load a csv file to a pandas dataframe, in the browser, using pyscript, without starting a local server. It would allow me to create easily distributable tools.

Is it even possible?

The closest I've seen is this code. It doesn't really load the csv to a single dataframe object (that I can then manipulate). It does skip the need to start a local server and displays the csv file in the browser.

  • The browser has APIs to read files from the local file system. The same APIs that JavaScript developers use. Those APIs are your only option. Anything else is just a wrapper on top of the native browser interfaces. – John Hanley Aug 23 '22 at 04:38
  • *The browser has APIs to read files from the local file system.* Oh really? Unless you're developing an extension, that's just not so. Remember that pyscript is just a wrapper around the browser interfaces as well. Pyscript translates to Javascript, so you're still living in the browser sandbox. – Tim Roberts Aug 23 '22 at 04:54
  • @TimRoberts - Do you mean that there are no APIs to read a file without a user action to do so? The browser has several APIs to read files. Examples: `FileReader` and `showOpenFilePicker`. – John Hanley Aug 23 '22 at 04:59
  • That's exactly what I mean. It's a huge security risk, so it is forbidden. `FileReader` can only read files that the user selected with an `` tag. `showOpenFileReader` brings up a dialog that requires user action. – Tim Roberts Aug 23 '22 at 06:21
  • @TimRoberts - there is no security problem with pulling up a dialog via user action. In fact, you can launch the file dialog in code without user action by creating the Anchor tag and then `tag.click()`. I am not sure what you are referring to with **huge security risk**, since the browser supports selecting files to be read by the application. For a program to read a file, the user must select the file. – John Hanley Aug 23 '22 at 06:30
  • Yes, we're saying the same thing. – Tim Roberts Aug 23 '22 at 16:16
  • I'm not understanding the restriction to pyscript and then saying you cannot manipulate the dataframe made? Did you try adding code that manipulated the data in the example you pointed out? Also, while it isn't pyscript but related-tech JupyterLite that already exists allows you to use Pandas with a CSV without running Jupyter on a server. Go to [the main JuputerLite page](https://jupyterlite.readthedocs.io/en/latest/), start it up using the 'Lab' button and then open the 'python.ipynb' demo file. There's a 'Pandas DataFrame' section if you scroll down. Add the line .... – Wayne Aug 23 '22 at 20:01
  • `df = pd.read_csv("data/iris.csv")` and you'll see you can open the data from a CSV, too. You can drag your local files into the file browser pane on the left and use pandas to open them with `pd.read_csv`. (The file you dragged in is actually in your browser's sandboxed area deep in your machine.) You can then manipulate the dataframe with Pandas. And then save and 'download' the modified dataframe back to your local file system in any form you want. Just be careful that you aren't relying on the in-browser area for storage. Make sure you bring it back to your machine's local file system. – Wayne Aug 23 '22 at 20:11
  • 1
    This is a rapidly developing, experimental area, e.g.,opening a local folder, see [here](https://twitter.com/jonititan/status/1503388715695194112). On where the files are that you dragged in, you'll want to see [here](https://stackoverflow.com/a/72063769/8508004). People are losing files not realizing clearing caches or [browser glitches](https://stackoverflow.com/q/73297474/8508004) are going to cause issues because of the current file system for WASM-based stuff. Or thinking the notebooks produced in it are accessible as if you created a notebook in normal Jupyter installed on one's system. – Wayne Aug 23 '22 at 20:28
  • 1
    Oops, not only did I misspell JupyterLite while linking to the main site, I forgot to point to the location of the 'Lab' button to start. Here's that link again: [JupyterLite main site](https://jupyterlite.readthedocs.io/en/latest/). You can use the 'Lab' button in the upper left to start. Other buttons in the upper left corner next to 'Try' allow you to pick your flavor to start these interfaces that run entirely in the browser using web assembly. – Wayne Aug 23 '22 at 20:36

0 Answers0