From js import fetch - error

I am trying to run the getting started example and the command
from js import fetch
and I get the error
ModuleNotFoundError: No module named ‘js’

What do I need to do to have the module?

Thanks.

I have the same issue.
-Using: Anaconda for windows 2021.11
-link to example page: Anaconda Nucleus
Looking forward to reply.
Thanks

Tried workaround:
Imported the whole js module and referenced the fetch function within js

import js
res = await js.fetch(‘https://jupyterlite.anaconda.cloud/b0df9a1c-3954-4c78-96e6-07ab473bea1a/files/bikeshare.csv’)

Highlights an issue in the editor
“Cannot find reference ‘fetch’ in ‘init.py’”

Ran it anyway…
Error:
res = await js.fetch(‘https://jupyterlite.anaconda.cloud/b0df9a1c-3954-4c78-96e6-07ab473bea1a/files/bikeshare.csv’)
^
SyntaxError: ‘await’ outside function

The Nucleus example runs on a completely different version of python - one that is compiled for use within Node.js. This specific python compilation includes the js module. Therefore, you cannot import this from ‘normal’ python.

See here: Pyodide — Version 0.19.0

1 Like

Not a great getting started example if we can not replicate it on our machines. Does anyone have any direction on how to get this example to work on our machines?

For those looking to run this in their own installation of Jupyter. It is easy to remove the reference to from js import fetch being reliant on python with javascript built-in. Simply delete or skip the second cell with the reference to js and the fetch from the URL, then in the third cell change it to:

df = pd.read_csv(‘https://jupyterlite.anaconda.cloud/b0df9a1c-3954-4c78-96e6-07ab473bea1a/files/bikeshare.csv’)
df.head()

Let pandas read_csv function read directly from the url to get the csv data and then load it into a data frame. No need to even use fetch from javascript as Pandas can handle this all on it’s own. If I am not clear and you are still having issues drop me a message, glad to help out.

Hi! Not sure if I understood well what you mean. I just tried:

df = pd.read_csv('https://jupyterlite.anaconda.cloud/b0df9a1c-3954-4c78-96e6-07ab473bea1a/files/bikeshare.csv 7')
df.head()

but it seems that the URL is not valid. I get some error messages ending with:

df = pd.read_csv('https://jupyterlite.anaconda.cloud/b0df9a1c-3954-4c78-96e6-07ab473bea1a/files/bikeshare.csv 7')
df.head()```

Here is snippet, if that helps you.

#import js <=== Comment this line
#from js import fetch <== Comment this line
import io

URL = ‘df = pd.read_csv(‘https://jupyterlite.anaconda.cloud/b0df9a1c-3954-4c78-96e6-07ab473bea1a/files/bikeshare.csv 7’)
df.head()’
#resp = await fetch(URL) <== Comment this line
#text = io.BytesIO((await resp.arrayBuffer()).to_py()) <== Comment this line

df = pd.read_excel(
URL,
sheet_name=‘Your sheet name’,
skiprows=range(20),
skipfooter=2)
print(‘Data downloaded and read into a dataframe!’)

df = pd.read_csv(“https://jupyterlite.anaconda.cloud/b0df9a1c-3954-4c78-96e6-07ab473bea1a/files/bikeshare.csv”)
df.head()

wondeful…commented out everything related to fetch and js and it worked.