scikit-hep / vegascope

View Vega/Vega-Lite plots in your web browser from local or remote Python processes.
BSD 3-Clause "New" or "Revised" License
35 stars 4 forks source link

ENH: add vegascope entrypoint for altair renderers #1

Closed jakevdp closed 6 years ago

jakevdp commented 6 years ago

This adds the mechanism necessary for Altair to use vegascope as a renderer.

Usage from altair looks like this, and works without any changes to the released version of altair as long as vegascope is installed:

In [1]: import altair as alt

In [2]: alt.renderers.enable('vegascope')
Out[2]: RendererRegistry.enable('vegascope')

In [3]: from vega_datasets import data
In [4]: cars = data.cars()
In [5]: alt.Chart(cars).mark_point().encode(
   ...:     x='Horsepower',
   ...:     y='Miles_per_Gallon',
   ...:     color='Origin',
   ...: ).interactive()
Out[5]: Rendered at http://localhost:56982

when the chart is evaluated, it launches a vegascope local canvas and displays the chart. Any additional chart is then displayed on the same canvas.

The one awkward thing is that the canvas never shuts itself down... I'd be curious if you have ideas on how to do that gracefully.

jpivarski commented 6 years ago

canvas.close() disconnects the server-side event stream. If you have a reference to the Canvas when you want it to go away, call close on it and the browser tab will go grey. Then you don't have the awkward situation in which the Python process doesn't close. Although the browser tab doesn't close, going grey is a good hint to the user that it's no longer useful.

I could perhaps register an atexit hook for every Canvas to close automatically at process shutdown. Would that be preferable?

jpivarski commented 6 years ago

(I didn't realize I was commenting on my own project here— I thought this was an Altair PR that you referenced me on.) Sure I'll accept it!

jpivarski commented 6 years ago

Actually, atexit wasn't necessary. I had overlooked a feature of ThreadingTCPServer that made all of the threads it spawned non-daemon. Turning that off (making them daemon), the Python process now ends cleanly even without explicitly calling close().

When the Python process exits, any browser tabs open to VegaScope will go gray.

jpivarski commented 6 years ago

Both updates are now on PyPI as vegascope>=1.0.7.

jakevdp commented 6 years ago

Awesome – thanks for the quick merge!