Skip to content

Commit

Permalink
Change to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
joelostblom committed Mar 13, 2024
1 parent b656dd6 commit 8fd2450
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
23 changes: 14 additions & 9 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,31 +1104,36 @@ def to_html(
**kwargs,
)

def to_url(self, *, open_browser: bool = True, fullscreen: bool = False) -> str:
def to_url(self, *, fullscreen: bool = False) -> str:
"""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():
url = vlc.vega_to_url(self.to_dict(format="vega"), fullscreen=fullscreen)
return vlc.vega_to_url(self.to_dict(format="vega"), fullscreen=fullscreen)
else:
url = vlc.vegalite_to_url(self.to_dict(), fullscreen=fullscreen)
if open_browser:
import webbrowser
return vlc.vegalite_to_url(self.to_dict(), fullscreen=fullscreen)

webbrowser.open(url)
return url
def open_editor(self, *, fullscreen: bool = False) -> None:
"""Opens the chart specification in the Vega chart editor using the default browser.
Parameters
----------
fullscreen : bool
If True, editor will open chart in fullscreen mode. Default False
"""
import webbrowser

webbrowser.open(self.to_url(fullscreen = fullscreen))

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(open_browser=False)
share_url = basic_chart.to_url()
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(open_browser=False, fullscreen=True)
fullscreen_share_url = basic_chart.to_url(fullscreen=True)
assert (
fullscreen_share_url
== f"https://vega.github.io/editor/#/url/vega-lite/{expected_vegalite_encoding}/view"
Expand Down

0 comments on commit 8fd2450

Please sign in to comment.