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

Open charts in the default browser with open_editor method #3358

Merged
merged 4 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 10 additions & 3 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,24 +1104,31 @@ def to_html(
**kwargs,
)

def to_url(self, *, fullscreen: bool = False) -> str:
def to_url(self, *, open_browser: bool = True, fullscreen: bool = False) -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This technically breaks if someone used to rely on fullscreen as a positional argument via to_url(True). I don't think it's a huge deal, but also have nothing against reversing the order.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good thing is that thanks to the *, fullscreen has always been a keyword-only argument so this should be fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is why we should lean towards kwarg-only APIs when we have the choice :) yay thanks past us

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is cool, I haven't come across this use of * to block additional positional args. Thanks for linking the pep!

"""Convert a chart to a URL that opens the chart specification in the Vega chart editor
The chart specification (including any inline data) is encoded in the URL.

This method requires that the vl-convert-python package is installed.

Parameters
----------
open_browser : bool
If True, the default browser will be launched to open the url. Default True
fullscreen : bool
If True, editor will open chart in fullscreen mode. Default False
"""
from ...utils._importers import import_vl_convert

vlc = import_vl_convert()
if _using_vegafusion():
return vlc.vega_to_url(self.to_dict(format="vega"), fullscreen=fullscreen)
url = vlc.vega_to_url(self.to_dict(format="vega"), fullscreen=fullscreen)
else:
return vlc.vegalite_to_url(self.to_dict(), fullscreen=fullscreen)
url = vlc.vegalite_to_url(self.to_dict(), fullscreen=fullscreen)
if open_browser:
import webbrowser

webbrowser.open(url)
return url

def save(
self,
Expand Down
4 changes: 2 additions & 2 deletions tests/vegalite/v5/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def test_save_html(basic_chart, inline):


def test_to_url(basic_chart):
share_url = basic_chart.to_url()
share_url = basic_chart.to_url(open_browser=False)
expected_vegalite_encoding = "N4Igxg9gdgZglgcxALlANzgUwO4tJKAFzigFcJSBnAdTgBNCALFAZgAY2AacaYsiygAlMiRoVYcAvpO50AhoTl4QUOQFtMKEPMUBaAOwA2ABwAWFi1NyTcgEb7TtuabAswc-XTZhMczLdNDAEYQGRA1OQAnAGtlQgBPAAdNZBAnSNDuTChIOhIkVBAAD2V4TAAbOi0lbgTkrSgINRI5csyQeNKsSq1bEFqklJAAR1I5IjhFYjRNaW4AEkowRkwIrTFCRMpkAHodmYQ5ADoEScZSWyO4CB2llYj9zEPdcsnMfYBWI6DDI5YjgBWlGg-W0CjklEwhEoyh0cgMJnMlmsxjsDicLjcHi8Pj8AWCKAA2qAlKkAIKgvrIABMxhkJK0ACFKSgPh96SBSSAAMIs5DmDlcgAifIAnEFBVoAKJ84wSzgM1IAMT5HxYktSAHE+UFRRqQIJZfp9QBJVXUyQAXWkQA"

assert (
Expand All @@ -390,7 +390,7 @@ def test_to_url(basic_chart):
)

# Check fullscreen
fullscreen_share_url = basic_chart.to_url(fullscreen=True)
fullscreen_share_url = basic_chart.to_url(open_browser=False, fullscreen=True)
assert (
fullscreen_share_url
== f"https://vega.github.io/editor/#/url/vega-lite/{expected_vegalite_encoding}/view"
Expand Down
Loading