Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

px.bar fails in Python #94

Open
ryan-williams opened this issue Jan 23, 2022 · 2 comments
Open

px.bar fails in Python #94

ryan-williams opened this issue Jan 23, 2022 · 2 comments

Comments

@ryan-williams
Copy link
Sponsor

Sample notebook: https://starboard.gg/nb/nf9YixM

Just one cell:

import pandas as pd
import micropip
await micropip.install(['plotly==5.1.0'])

import plotly.express as px

px.bar(x=[1,2,3], y=[4,5,6])

It seems to get through micropip.install and import, but then calling px.bar fails with:

Python exception:
Traceback (most recent call last):
  File "/lib/python3.9/site-packages/plotly/io/_renderers.py", line 530, in <module>
    webbrowser.get()
AttributeError: module 'webbrowser' has no attribute 'get'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/lib/python3.9/site-packages/plotly/basedatatypes.py", line 810, in _repr_html_
    bundle = self._repr_mimebundle_()
  File "/lib/python3.9/site-packages/plotly/basedatatypes.py", line 822, in _repr_mimebundle_
    renderer_str = pio.renderers.default
  File "/lib/python3.9/site-packages/_plotly_utils/importers.py", line 36, in __getattr__
    class_module = importlib.import_module(rel_module, parent_name)
  File "/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/lib/python3.9/site-packages/plotly/io/_renderers.py", line 532, in <module>
    except webbrowser.Error:
AttributeError: module 'webbrowser' has no attribute 'Error'

Here's the same notebook in jupyterlite; the plot renders successfully: https://runsascoded.com/jupyter/lite/retro/notebooks/?path=plotly-test.ipynb

image

I see a couple mentions of plotly in the "federated_extensions" and "piplite_urls" sections of jupyterlite/examples/jupyter_lite_config.json, I guess there's some configuration that lets plotly work "out of the box" in the jupyterlite demos, that could be adapted here?

@gzuidhof
Copy link
Owner

It looks like there was some work into Jupyterlite to support Plotly, I would like to also support it in Starboard of course :). They seem to load a specific extension like you mentioned (this seems to be the PR: jupyterlite/jupyterlite#129). I am sure we can support it without needing to have the ability to support these kinds of extensions.

This is where it currently falls over: https://github.com/plotly/plotly.py/blob/master/packages/python/plotly/plotly/io/_renderers.py#L526-L536. We should probably have a mock version of ipython, I expect a lot of packages will depend on that

From what I understand this browser renderer actually opens a webbrowser normally. It creates a webserver that serves a page once.

I think the next step here is to mock out ipython, the alternative would be to request a patch into plotly.js itself to support custom pyodide environments better, or monkeypatch it ourselves (not a fan!).

@lakshyapriyadarshi
Copy link

Is there a way I can use Plotly in the notebook? @ryan-williams @gzuidhof

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants