I am trying to read a csv file using pyscript. There is an error message kept showing.
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 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: 'salaries.csv' )
the code for reading csv, pandas is imported.
<py-script>
df = pd.read_csv("salaries.csv")
</py-script>`
<py-env>
- matplotlib
- numpy
- seaborn
- pandas
</py-env>
<py-script>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import os
</py-script>
I expect someone could help me solve this issue.
Pyscript is rapidly evolving. For now ...
You'll note for example py-config
has replaced py-env
, see here.
paths
, that had been highlighted in section about local file access here, is now used differently. paths
is now a list to specify local Python modules for installation, see here. open_url
seems the suggested way to get items, see here and current pyscript demo example in action here.
If you are serving your HTML file from the same folder where your .csv
file is (which I gather is the case from "I have literally put the csv file in the same folder as my html file"), then you can use relative URLs to point to it.
For example, hosting on GitHub along side the .csv
file works in this example below at present:
<html>
<head>
<title>Panndas Example with reading from relative url in the same location where HTML resides</title>
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<py-config>
packages = [
"pandas",
"jinja2"
]
</py-config>
<body>
<h1>Hello World!</h1>
<p>This is a test To see how my html code is.</p>
<p>This space below will become blank as a few seconds is taken to render the dataframe. Be patient ...</p>
<div id="pane"></div>
<py-script>
import pandas as pd
from pyodide.http import open_url
url = 'penguins.csv'
df = pd.read_csv(open_url(url))
df.head(39).style.format(precision=2)
</py-script>
</body>
</html>
You can see the HTML running here.
Note that it may break a few hours or days from now. As this older route similar to OP's approach is now outdated.
If you aren't trying to just display the Pandas dataframe... Alternatively, if you want to use Pyodide/WASM to work with data from .csv
actively, you can use the file browser in JupyterLite to place your local .csv
into the virtual system and interactively use the data contained in it with Pandas. See my suggestion here. If you make any useful changes, be sure to save the new data and download the modified version back out of the virtual file system inside your browser, to your local machine's file handling system. Otherwise, you take chances that a change to your browser or clearing the cache could wipe what you coded or made from the internal storage.