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 8 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
19 changes: 19 additions & 0 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,25 @@ def to_html(
requirejs=requirejs,
)

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

Choose a reason for hiding this comment

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

Consider: forcing all args to be keyword-only so that in the future, if we add more params to this function, they are not order dependent and we can re-order them as needed without breaking anyone. Not sure if this is overkill.

to_url(False) is also not very obvious what False does. Requiring a kwarg improves that.

On the other hand, requiring a kwarg is more typing.

I think I fall 60% in favor of requiring kwarg but am fine with keeping it as is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! Done in 5d440f9

"""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
----------
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 = (
"N4Igxg9gdgZglgcxALlANzgUwO4tJKAFzigFcJSBnAdTgBNCALFAZgAY2AacaYsiygAlMiRoVYcAvpO50AhoTl4QU"
"OQFtMKEPMUBaMACY5LTAA4AnACM55ugFY6ARgBspgOz2zh03Wfs5bCwsIDIganIATgDWyoQAngAOmsgg1hEh3JhQk"
"HQkSKggAB7K8JgANnRaStzxSVpQEGokcmUZIHElWBValiA1ickgAI6kckRwisRomtLcACSUYIyY4VpihAmUyAD029"
"MIcgB0CBOMpJaHcBDbi8vhe5gHumUTmHt2h44fjocAVpTQPraBRySiYQiUZQ6OT6IwmCzWWwOFzuTymby+fyBYLIA"
"DaoCUKQAgkDesgDKYZAStAAhUkoOx2KkgQkgADC9OQABYWMzWQARTnmRx8rQAUU5phFnGpKQAYpy7LyZSytABxTmO"
"cyilKCSVuHUgACSioMkgAutIgA"
)

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