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

Conversation

jonmmease
Copy link
Contributor

Closes #3188

This PR adds a Chart.to_url method that uses VlConvert to generate a Vega editor URL that opens the chart's specification in the online Vega editor.

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'
)

print(chart.to_url())

https://vega.github.io/editor/#/url/vega-lite/N4Igxg9gdgZglgcxALlANzgUwO4tJKAFzigFcJSBnAdTgBNCALFAZgAY2AacaYsiygAlMiRoVYcAvpO50AhoTl4QpAE4AbFCDGEADpWQB6Q2DpQAdACtKdTOrhpV5qJkKGougLaG0mBHIBaeUVKV0oAATQARnMAJgBOczZDYLkTOVVKK0poEBkQTwyAa2VCAE9dTC1dCBJxfMwoSDoSJFQedQhVZXg7Oi0AeVVEEhBucsqtKAhPEjlNfIAPHqx1fuQQQS7QmuxMbvGKqo2AR1I5IjhFYl887jKVvq0AWTh1TEoAfUrVT4BxeadKBjEATY4gM4XYjXBxVaTcAAklDAjEwhS0On0Rh8fjk5gQV0YpAARuY4BBDMjUYUcf4AvZCJgfABWcxRVkxay5SRAA

cc @NickCrews

Copy link
Contributor

@binste binste left a comment

Choose a reason for hiding this comment

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

Looks great @jonmmease. Not sure if that was the intention but it helps with #2875 which is nice.

Only added two minor comments.

altair/vegalite/v5/api.py Outdated Show resolved Hide resolved
altair/vegalite/v5/api.py Outdated Show resolved Hide resolved
jonmmease and others added 2 commits November 5, 2023 08:28
Co-authored-by: Stefan Binder <binder_stefan@outlook.com>
Co-authored-by: Stefan Binder <binder_stefan@outlook.com>
@jonmmease
Copy link
Contributor Author

Thanks for the review and suggestions @binste, I've accepted them.

Not sure if that was the intention but it helps with #2875 which is nice.

I hadn't come across that issue, but yeah this will provide a reasonable workaround for Visual Studio Code users.

Copy link
Contributor

@NickCrews NickCrews left a comment

Choose a reason for hiding this comment

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

Looks great! Thank you!

Could consider adding auto copy to clipboard support, but I lean towards that being out of scope

@@ -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

@jonmmease
Copy link
Contributor Author

Could consider adding auto copy to clipboard support, but I lean towards that being out of scope

Yeah, this is a possible future extension. Another idea is to optionally use the Python webbrowser module to open the URL in the editor.

@mattijn
Copy link
Contributor

mattijn commented Nov 6, 2023

Thanks for the PR @jonmmease and reviews @NickCrews, @binste! This is great and valuable (especially in VSCode, like Stefan said.)

@mattijn mattijn merged commit b03d071 into main Nov 6, 2023
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Shareable link to eg https://vega.github.io/editor
4 participants