3

Requirement : I want to read a excel file from my local directory by using <py-script>

Problem Statement : py-script runs under their own environment. Hence, It is not able to locate the current working directory and when I trying to see the current working directory by using os.cwd() command. It is returning /home/pyodide instead of the local directory files.

<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
  </head>
  <body>
    <py-script>
      import os

      print(os.listdir())
      print(os.getcwd())
    </py-script>

  </body>
</html>

Hence, It is giving the below error.

FileNotFoundError: [Errno 44] No such file or directory.

Is there any way to achieve the requirement using py-script ?

Debug Diva
  • 26,058
  • 13
  • 70
  • 123

1 Answers1

0

You'll want to add one or more fetch configurations to your <py-config> tag. This is a convenience feature for fetching files from the network, and moving them into the Virtual Filesystem that Python interacts with (inside Pyodide/PyScript).

For example, if your html file is located at /index.html and you want to make use of a file located at /data/example.txt, you could do:

<py-config>
    [[fetch]]
        files = ['example.txt']
        from = 'data'
        to_folder = 'data_for_python'
</py-config>

<py-script>
    with open("data_for_python/example.txt", "r") as fp:
        print(fp.read())
</py-script>

The above fetch configuration fetches the content at the URL data/example.txt and stores it in the virtual filesystem at data_for_python/example.txt.

It's important to emphasize that this is fetching data from the network, not from your local filesystem directly. You will probably want to run some kind of simple local server to make all the required files available to the network; this could be a live server built into an IDE, or as simple as running python -m http.server 8000 --bind 127.0.0.1.

See the fetch configurations section of the 2022.12.1 release blog post for more discussion.

Jeff Glass
  • 622
  • 1
  • 8
  • Thanks for the answer! But actually it is giving me the error that I need some live server to run this but at the end what I am looking for is that end user should just open a HTML file in the browser and will be able to see the excel file data without running any web server in their machine. – Debug Diva Mar 02 '23 at 13:53
  • What you spell out in your last comment is an entirely different 'ask'.@RohìtJíndal . However, the eventuality of that is being discussed in a related way in the Jupyter community where you could package minimal JupyterLite into an Electron app and then place it alongside your data and not need an internet connection. I think that was the general idea. Maybe the data even gets packaged. Point is this is still a ways off. If your data is [small enough](https://stackoverflow.com/q/75616047/8508004) you could write up a step-by-step guide inside a pyscript page or Jupyter markdown `.ipynb` ... – Wayne Mar 02 '23 at 17:53
  • file and host that online in 'static' serving site such as github pages and point your end user at it. The guide would then describe how to 'upload' the data from their local computer into the web assembly instance running inside their machine. And guide them through triggering the processing of the data as you want. That way your local data stays local and on the end user's machine. The only connection stuff is bringing in the framework. It's an option that can work now sites that allow you to serve static web pages for free like Github pages & others (Netlify is one, I think?). – Wayne Mar 02 '23 at 17:59
  • @Wayne The end user will have the internet connection. The only thing is that end user should not be installed any live server or IDE in their systems. They have to just run the html file in the browser. That's it. – Debug Diva Mar 03 '23 at 06:50
  • Given that, then pyscript (or JupyterLite) is fine. You can easily make free static sites that use Jeff's code (or for JupyterLite you'd use different code.) The end user doesn't set up where the files are hosted. You do. So I'm not sure why you commented below Jeff's code, "But actually it is giving me the error that I need some live server to run this"? [This paragraph](https://github.com/fomightez/pyscript_test#pandas-dataframe-input-and-display) has links how to access the examples as webpages (via GitHub Pages). That repo there has the code above. Missing why Jeff's code isn't solution? – Wayne Mar 03 '23 at 13:29
  • To expand on what Wayne said, if you host the Excel file (in your case) on the web, you can can similarly use a Fetch configuration to get the file from there. Simply use a `files` value with a fully qualified URL, as in: `files = ['https://www.example.com/your/site/data.xlsx']`, and optionally use to the `to_folder` attribute if you want the file in a different folder in the virtual filesystem. – Jeff Glass Mar 03 '23 at 16:04