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

Make altair_viewer and altair_saver work with Altair 5 #2866

Closed
mattijn opened this issue Feb 1, 2023 · 6 comments
Closed

Make altair_viewer and altair_saver work with Altair 5 #2866

mattijn opened this issue Feb 1, 2023 · 6 comments

Comments

@mattijn
Copy link
Contributor

mattijn commented Feb 1, 2023

As was pointed out in #2686 (comment). Once Altair 5 RC is released altair_viewer and altair_saver won't work anymore as they are using the entrypoints of altair vegalite 4.

Observe current behaviour using altair from GitHub main branch:

import altair as alt
from vega_datasets import data
import altair_viewer

cars = data.cars.url

op_var = alt.param(value=0.1)

chart = alt.Chart(cars).mark_circle(opacity=op_var).encode(
    x='Miles_per_Gallon:Q',
    y='Horsepower:Q',
    color='Origin:N'
).add_params(
    op_var
)

altair_viewer.display(chart)
---------------------------------------------------------------------------
NoMatchingVersions: No matches for version='5.2.0' among ['4.0.2', '4.8.1', '4.17.0'].
Often this can be fixed by updating altair_viewer:
    pip install -U altair_viewer

Originally posted by @mattijn in #2599 (comment)

@joelostblom
Copy link
Contributor

joelostblom commented Jan 31, 2024

Is there any functionality from altair_viewer or altair_saver that we still would like to be usable from altair 5? For altair_saver, I can't think of anything that isn't already covered by vl_convert. Unless I'm missing something, maybe we should add a note to the altair_saver readme that we encourage users to use vl_convert for altair 5 instead?

For altair_viewer, we don't yet have a way to support the display/show functionality in altair 5 (and some requests to add it in altair-viz/altair_viewer#59). I wonder if we can lift out the browser opening logic from altair_viewer directly into altair and rely on vl_convert to display the chart in a browser tab either online or offline mode. @jonmmease Do you think this would be possible?

@jonmmease
Copy link
Contributor

As I recall, altair_viewer does a bunch of work involving websockets to make repeated chart displays automatically show up in the same browser tab. I'd personally prefer to have each call do chart.show() open a new browser tab. If we use this approach, then I think we can rely on vl-convert to produce the HTML.

I wrote some logic for plotly.py a while back that takes an HTML string and opens it in a new browser tab without writing to a temp file. See https://github.com/plotly/plotly.py/blob/a6662ab48bf0a71f703fac1833a4d7e6c805b805/packages/python/plotly/plotly/io/_base_renderers.py#L655-L709. If this is the behavior we want, then I think it would be pretty straightforward to combine this logic with vl-convert.

@joelostblom
Copy link
Contributor

From my side of things, those solutions sounds great! I think the most important part of this feature is to enable an altair workflow from the terminal; it doesn't have to mimic altair saver exactly. I'm in favor of making the behavior easy to implement/maintain and I don't see a noticeable advantage of reusing the same tab (and if someone really wants to, they could write a chart to file and have that filepath open in a single tab that they refresh). You plotly solution sounds like a good approach to me as well.

@stefaneidelloth
Copy link

stefaneidelloth commented Feb 15, 2024

If you include save/export options in altair, you might want to support *.pdf as well.
#2612
In matplotlib a corresponding command would be chart.savefig(file_path, ...)
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html
In plotly its
https://plotly.github.io/plotly.py-docs/generated/plotly.io.write_image.html

@jonmmease
Copy link
Contributor

Hi @stefaneidelloth, PDF export is supported using chart.save("fig.pdf"). Starting in Altair 5.2.0, this is now based on [vl-convert] rather than altair_saver. vl-convert has no external dependencies on selenium, node.js, or a web browser, so it's a lot less complex to use.

@joelostblom
Copy link
Contributor

I'm closing this since Altair 5.2 brought in PDF export and mentioned above, and since Altair 5.3 the functionality of Altair Viewer is now available in Altair via the vl-convert package instead. See the docs on how to use the browser renderer to display charts via .show(), and the PR #3379 for more details. I believe that means all the functionality from altair_viewer and altair_saver are now completely covered by altair + vl-convert.

Immense thanks to @jonmmease for all your work on vl-convert making it simpler to both save and display altair charts!

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

No branches or pull requests

4 participants