Hi all,
Can I read a csv from disk into pandas? When I hard-link to the file, it says it can’t find the file. If I do os.getcwd(), I get /home/pyodide, which does not exist. All the examples I can find on the website either simulate data or grab it from the web.
/home/pyodide
JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 5, in File "/lib/python3.10/site-packages/pandas/util/_decorators.py", line 311, in wrapper return func(*args, **kwargs) File "/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 680, in read_csv return _read(filepath_or_buffer, kwds) File "/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 575, in _read parser = TextFileReader(filepath_or_buffer, **kwds) File "/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 933, in __init__ self._engine = self._make_engine(f, self.engine) File "/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1217, in _make_engine self.handles = get_handle( # type: ignore[call-overload] File "/lib/python3.10/site-packages/pandas/io/common.py", line 789, in get_handle handle = open( FileNotFoundError: [Errno 44] No such file or directory: '/home/david/pyscript-main/pyscriptjs/public/voice_data.csv' )
Same issue with me. The CSV file is placed in the same directory as that of the HTML file and it cannot be handled by PyScript? Is there any particular place we need to place the CSV file in? I get the File Not Found error as well.
The python code (and all browser runtime) runs in a sandbox with a virtual or “fake” filesystem, not the real local filesystem. This is a safety feature of browsers, to stop downloaded scripts from being able to read your local data. None of the os module’s features will behave as you expect them to.
If you are hosting your own CSV file in a directory with the HTML, you can probably do an HTTP fetch, as in the examples, but with a filepath like “./voice_data.csv” - i.e., relative to the location of the HTML. It the CSV file is remote, it needs to be on a server set up to allow CORS from any location.
I am getting error now
JsException(PythonError: Traceback (most recent call last): File “/lib/python3.10/site-packages/_pyodide/_base.py”, line 429, in eval_code .run(globals, locals) File “/lib/python3.10/site-packages/_pyodide/_base.py”, line 300, in run coroutine = eval(self.code, globals, locals) File “”, line 1, in ModuleNotFoundError: No module named ‘altair’ )
If I remove path line and df lines from the script code is working… Basically, I took working example of altair.html and introducing reading data from local csv file…
When you specify the file name “filename.ext” while read file, you are providing the open() function with a relative path. This means that the file you want to read is located in the current working directory.
file = open('filename.ext') //relative path
In the above code, you are not giving the full path to a file to the open() function, just its name - a relative path. The error “FileNotFoundError: [Errno 2] No such file or directory” is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path.
In the above code, all of the information needed to locate the file is contained in the path string - absolute path.
If the full path to the file is not provided, the python file path is interpreted relative to the current working directory. This is the directory from which the program was started. For this to work, the directory containing the python executable must be in the PATH environment variable, which contains directories automatically searched for executables when a command is entered. To access a file in a different directory, you must either specify a relative path between the files or use an absolute path for one of them.
Hi @carlhyde - while your answer is correct (and excellent!) for general desktop Python, it is not accurate for PyScript, which does not have an internal concept of a PATH variable or Python Executable in same way that “normal” python does. Files from the desktop/server environment need to be moved into the Emscripten virtual filesystem in the browser and accessed from there - using a local file directly like the original poster did will not work.
Since this is quite an old thread, I should note that the syntax for brining files from a remote URL into the virtual filesystem changed in PyScript 2022.11.1 to use [[fetch]] configurations in the <py-config> tag, as opposed to paths in <py-env>.
Once relative import is used (in this case importing .csv from the root folder), is it then no longer possible to just share the .html so that the receiver can just open it and see the result ? (And hence, a static web server is required ? )
If so, is there a dirty way of embedding the data into html ?