Skip to content

fix for chromium renderer on linux systems (issue #2348) #3278

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/python/plotly/plotly/io/_base_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,11 @@ def open_html_in_browser(html, using=None, new=0, autoraise=True):
if isinstance(html, six.string_types):
html = html.encode("utf8")

if isinstance(using, tuple):
using = [i for i in webbrowser._browsers.keys() if any(j in i for j in using)][
0
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use if any(j in i for j in using) here instead of if i in using here? I think the way it is now, you would get partial string matches, which might be confusing.

Also, I think this will cause an index-out-of-bounds error if none of the browsers in the tuple are available. I don't recall what error is currently raised, but it would be nice to report something a little more informative (e.g. A ValueError that explains that none of the elements in using were found in list(if any(j in i for j in using))).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @jonmmease using if i in using makes more sense than if any(j in i for j in using), will get that change done.

Regarding the second point I could do something like

if isinstance(using, tuple):
    try:
        using = [i for i in webbrowser._browsers.keys() if i in using][0]
    except TypeError:
         raise TypeError("""
Unable to detect any browsers in webbrowser._browsers.keys().
Check that the desired browser is installed on your system.
If problem still persists, try registering the browser using the python webbrowser module """)

Let me know if the TypeError is easy to understand or any suggestions are welcome

ValueError for invalid names like chromee is already handled and throws and error as follows ValueError: Invalid named renderer(s) received: ['chromee']

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sticking with raising a ValueError makes sense here (to match to existing behavior). And I think you'll need to trap an IndexError rather than a TypeError to handle the case where no matches are found (this is the exception raised when you evaluate [][0]).

For the error message, I pictured including the actual list of supported browsers (the result of evaluating list(webbrowser._browsers.keys())) rather than "webbrowser._browsers.keys()") rather than itself.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jonmmease can you please help me out with the content to put in the value error. Does this content seem ok? Any suggestions are welcome.

if isinstance(using, tuple):
    try:
        using = [i for i in webbrowser._browsers.keys() if i in using)][0]
    except IndexError:
        raise ValueError("""
Unable to detect the given browser.
Try one among the following "chrome", "chromium", "firefox" or "default" to use your default browser.
""")


class OneShotRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
Expand Down
8 changes: 5 additions & 3 deletions packages/python/plotly/plotly/io/_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,11 @@ def show(fig, renderer=None, validate=True, **kwargs):

# External
renderers["browser"] = BrowserRenderer(config=config)
renderers["firefox"] = BrowserRenderer(config=config, using="firefox")
renderers["chrome"] = BrowserRenderer(config=config, using="chrome")
renderers["chromium"] = BrowserRenderer(config=config, using="chromium")
renderers["firefox"] = BrowserRenderer(config=config, using=("firefox"))
renderers["chrome"] = BrowserRenderer(config=config, using=("chrome", "google-chrome"))
renderers["chromium"] = BrowserRenderer(
config=config, using=("chromium", "chromium-browser")
)
renderers["iframe"] = IFrameRenderer(config=config, include_plotlyjs=True)
renderers["iframe_connected"] = IFrameRenderer(config=config, include_plotlyjs="cdn")
renderers["sphinx_gallery"] = SphinxGalleryHtmlRenderer()
Expand Down
3 changes: 3 additions & 0 deletions packages/python/plotly/plotly/tests/test_io/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
import requests
import numpy as np
import webbrowser

import plotly.graph_objs as go
import plotly.io as pio
Expand Down Expand Up @@ -224,6 +225,8 @@ def test_notebook_connected_show(fig1, name, connected):
def test_browser_renderer_show(fig1, renderer):
pio.renderers.default = renderer
renderer_obj = pio.renderers[renderer]
# scan through webbrowser._browsers.keys() and assign the browser name registered with os
renderer_obj.using = [i for i in webbrowser._browsers.keys() if renderer in i][0]

# Setup mocks
mock_get = MagicMock(name="test get")
Expand Down