0

The Seaborn code does not work. I use jupyterlite to execute seaborn python code. first, i import seaborn in the following way --

import piplite
await piplite.install('seaborn')
import matplotlib.pyplot as plt
import seaborn as sn
%matplotlib inline

But when I insert seaborn code like the following one then it shows many errors that i do not understand yet -- link of the code

the problem that I face

But I insert this code in the google colab it works nicely google colab

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • Please provide enough code so others can better understand or reproduce the problem. – Community Dec 19 '22 at 15:19
  • It's not so much that "Seaborn code Anscombe’s quartet" does not work. It is the way the `sns.load_dataset()` retrieves the sample dataset that isn't working. You can clearly see that is the error line in your image and so that is what your title should address. Do you know much about JupyterLite? If you are just learning, it is not ideal for efforts like this because it cannot use a lot of the normal ways to fetch code and data. There's only certain ways that work within JupyterLite for several reasons related to security and how WASM works inside your browser. ... – Wayne Dec 19 '22 at 17:09
  • Go [here](https://github.com/binder-examples/requirements) and press `launch binder`. When the session starts up several seconds later, paste in the Seaborn example code you linked to and before running it, change `sns.set_theme(style="ticks")` to `sns.set(style="ticks")`. When you run it, it will work. If you make anything useful, save it back to your local computer ASAP as the session your are working in is running on a remote, **temporary** machine. However, it is using a full, typical Python kernel and not pyodide **and so it is much less limited than Jupyterlite!** – Wayne Dec 19 '22 at 17:17
  • Alternatively, if you want to use a more current version of Seaborn where you don't need to change the `sns.set_theme()` syntax, go to [here](https://github.com/binder-examples/conda) and press `launch binder`. In the session that comes up, you need to install seaborn, and so you can run in a cell `%conda install seaborn` first. Restart the kernel when it completes and then the Seaborn code you are trying to run will work with no changes necessary. – Wayne Dec 19 '22 at 17:24
  • If you are just learning things, please pay close attention to notifications. On the [JupyterLite](https://jupyterlite.readthedocs.io/en/latest/) main page there's lightning bolts highlighting the experimental nature of JupyterLite. The text reads, "Although JupyterLite is currently being developed by core Jupyter developers, the project is still unofficial. Not all the usual features available in JupyterLab and the Classic Notebook will work with JupyterLite ..." **So usually if example code isn't working in a well-developed package, a JupyterLite limitation being the cause is a good guess.** – Wayne Dec 19 '22 at 17:30
  • Also in troubleshooting in general you'll commonly encounter issues where the syntax has slightly changed over time and you may be trying to use an old version. You could see an example of that with the `sns.set_theme()` syntax and looking around you can see a solution [here](https://stackoverflow.com/a/65409962/8508004). That solution came up near the top by just searching 'seaborn set_theme'. Hopefully, taking you through how one can sort things out will help you if you want to continue to use JupyterLite or as you try to use code that others say works or worked at the time. – Wayne Dec 19 '22 at 17:39
  • Note that it worked in Google Colab because that is a typical, full Python kernel underlying things. It's running on Google's machines and not the way JupyterLite runs in web-assembly inside your browser inside your local machine. – Wayne Dec 19 '22 at 18:52

1 Answers1

0

The issue is getting the example dataset as I point out in my comments.

The problem step is associated with:

# Load the example dataset for Anscombe's quartet
df = sns.load_dataset("anscombe")

You need to replace the line df = sns.load_dataset("anscombe") with the following:

url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/anscombe.csv' # based on [Data repository for seaborn examples](https://github.com/mwaskom/seaborn-data)
from pyodide.http import open_url
import pandas
df = pandas.read_csv(open_url(url))

That's based on use of open_url() from pyodide.http, see here for more examples.


Alternative with pyfetch and assigning the string obtained

If you've seen pyfetch around, this also works as a replacement of the sns.load_dataset() line based on John Hanley's post, that uses pyfetch to get the CSV data. The code is commented further:

# GET text at URL via pyfetch based on John Hanley's https://www.jhanley.com/blog/pyscript-loading-python-code-in-the-browser/
url = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/anscombe.csv' # based on [Data repository for seaborn examples](https://github.com/mwaskom/seaborn-data)
from pyodide.http import pyfetch
response = await pyfetch(url)
content = (await response.bytes()).decode('utf-8')
# READ in string to dataframe based on [farmOS + JupyterLite: Import a CSV of Animals](https://gist.github.com/symbioquine/7641a2ab258726347ec937e8ea02a167)
import io
import pandas
df = pandas.read_csv(io.StringIO(content))
Wayne
  • 6,607
  • 8
  • 36
  • 93