Finding a file (xlsx), opening it and processing it (using pandas)

Hi All,

I want to make a website version of an app that I made with Tkinter.
The basic flow of the app is opening an xlsx and with pandas parse the data of the xlsx and start the quiz. Each question that were parsed will be randomized and tested on the user.

I hope to accomplish this with pyscript, so that everything is done in the client-side of things (browser) without the need of a server.
Is it possible?

filepath = fd.askopenfilename(filetypes=(("xlsx", "*.xlsx"),))
        if not filepath:
            return

Update 1
I think I will use JS to parse XLSX files

openpyxl module is supported by pyodide, so you can use it for parsing excel file inside py-script

2 Likes

For any future readers find the live examples below.

Load remote xlsx (excel)

If you’re file is remote use pyfetch for retrieval and if it’s binary like xlsx files:

from pyodide.http import pyfetch
import asyncio
import pandas as pd 
import openpyxl
from io import BytesIO

response = await pyfetch(url="YOUR_REMOTE_DIR/test.xlsx", method="GET")
bytes_response = await response.bytes()
df = pd.read_excel(BytesIO(bytes_response))
df

Example here.

Note that you need to load the respective libraries in py-env first like this:

  <py-env>
    - pandas
    - openpyxl
  </py-env>

Load remote csv or json

Else, if you’re loading non-binary files like csv’s or json’s it’s easier with open_url:

import pandas as pd 
from pyodide.http import open_url
df = pd.read_csv(open_url("YOUR_REMOTE_DIR/test.csv"))
df

Example here.