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

Support converting a Chart to a Vega editor URL #3252

Merged
merged 9 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 21 additions & 0 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,27 @@ def to_html(
requirejs=requirejs,
)

def to_url(self, fullscreen: bool = False):
jonmmease marked this conversation as resolved.
Show resolved Hide resolved
"""Convert a chart to a URL that opens the chart specification in the Vega chart editor

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

This method requires the option vl-convert-python dependency
jonmmease marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
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)
else:
return vlc.vegalite_to_url(self.to_dict(), fullscreen=fullscreen)

def save(
self,
fp: Union[str, IO],
Expand Down
2 changes: 2 additions & 0 deletions doc/releases/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Version 5.2.0 (unreleased month date, year)

Enhancements
~~~~~~~~~~~~
- Support saving charts as PDF files using the vl-convert export engine (#3244)
- Support converting charts to sharable Vega editor URLs with ``chart.to_url()`` (#3252)

Bug Fixes
~~~~~~~~~
Expand Down
20 changes: 20 additions & 0 deletions doc/user_guide/saving_charts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ size at the default resolution of 72 ppi::

chart.save('chart.png', scale_factor=2)

Sharable URL
~~~~~~~~~~~~
The :meth:`Chart.to_url` method can be used to build a sharable URL that opens the chart
specification in the online Vega editor_.

.. altair-plot::
:output: repr

import altair as alt
from vega_datasets import data

chart = alt.Chart(data.cars.url).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N'
)

chart.to_url()

.. _vl-convert: https://github.com/vega/vl-convert
.. _altair_saver: http://github.com/altair-viz/altair_saver/
.. _vegaEmbed: https://github.com/vega/vega-embed
.. _editor: https://vega.github.io/editor/
24 changes: 24 additions & 0 deletions tests/vegalite/v5/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,30 @@ def test_save_html(basic_chart, inline):
assert 'src="https://cdn.jsdelivr.net/npm/vega-embed@6' in content


def test_to_url(basic_chart):
share_url = basic_chart.to_url()
expected_vegalite_encoding = (
"N4Igxg9gdgZglgcxALlANzgUwO4tJKAFzigFcJSBnAd"
"TgBNCALFAZgAY2AacaYsiygAlMiRoVYcAvpO50AhoTl4QUOQFtMKEPMUBaMACY5LTAA4AnACM55ugFY6ARgBspgOz"
"2zh03Wfs5bCwsIDIganIATgDWyoQAngAOmsgg1hEh3JhQkHQkSKggAB7K8JgANnRaStzxSVpQEGokcmUZIHElWBVa"
"liA1ickgAI6kckRwisRomtLcACSUYIyY4VpihAmUyAD029MIcgB0CBOMpJaHcBDbi8vhe5gHumUTmHt2h44ALJ+HA"
"FaUaB9bQKOSUTCESjKHRyfRGEwWay2BwudyeUzeXz+QLBZAAbVAShSAEFgb1kAZTDJCVoAEJklB2OzUkBEkAAYQZy"
"C+LBZbIAIlzzI4+VoAKJc0wizg0lIAMS5dl5MtZWgA4lzHOZRSlBJK3DqQABJRUGSQAXWkQA"
)

assert (
share_url
== f"https://vega.github.io/editor/#/url/vega-lite/{expected_vegalite_encoding}"
)

# Check fullscreen
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"
)


def test_facet_basic():
# wrapped facet
chart1 = (
Expand Down
Loading