scikit-hep / histbook

Versatile, high-performance histogram toolkit for Numpy.
BSD 3-Clause "New" or "Revised" License
109 stars 9 forks source link

Support JupyterLab & other frontends #6

Closed jakevdp closed 6 years ago

jakevdp commented 6 years ago

Hello – very cool project.

I notice that for the notebook plotting you depend on vega. Unfortunately, this does not work in JupyterLab because of its different frontend and backend architecture.

Have you thought displaying via altair instead? If you do, you could take advantage of the work we've done on rendering backends, and you'd get automatic support for notebook, jupyterlab, nteract, colab, hydrogen, etc.

You wouldn't have to use Altair's API to build the charts – if you have a valid vega-lite spec in the form of a Python dictionary, alt.Chart.from_dict(spec) will return an Altair object that's connected to all the display architecture.

In the meantime, I want to think about adopting your vegascope project in altair: https://github.com/altair-viz/altair/issues/919

Let me know if you have any questions, and thanks for this great package!

jpivarski commented 6 years ago

I definitely considered Altair when scouting out for graphics handling. Originally, I wanted a simple Vega AST, but Vincent seems to be abandoned/obsolete. Altair is not a simple AST, but a plotting tool, which I was afraid I'd find myself reverse engineering to make it so what I want. (I remember digging into GitHub issues about difficulties with overlaying...) Since what I want to do is a bit low-level, it make more sense to write Vega (Lite) directly. It's just too bad there's no Python-side correctness check.

I also checked the vega library's compatibility with Jupyter Lab. It didn't work in the early days of Jupyter Lab, but when I updated everything a few weeks ago, it did.

I didn't know about alt.Chart.from_dict(spec). This answers my low levelness question— I can keep my JSON-generating code and make Altair objects, which is great because users can mix them with other plots made in Altair (right?). But there's another consequence: if it works the way I think it does, it's already supported:

h.step().to(alt.Chart.from_dict)

should turn histogram h into an Altair object. The to function calls whatever function you give it with the JSON to plot.

histbook doesn't explicitly depend on any graphics library, not even vega (I use vegascope because I work outside of Jupyter). If this works as I think, histbook supports Altair as much as the others.

(I need to do a test and make an example, then publicize it with a few examples, particularly if I can mix histogram plots with other kinds of plots from Altair.)

jakevdp commented 6 years ago

Fair enough – I just wanted to help you anticipate answers to questions from users who are unable to use the histbook's visualization tools in JupyterLab and other frontends :smile:

jakevdp commented 6 years ago

i.e. you can provide a hook to Altair and make use of all the effort we've already put into various rendering frontends, or you can reinvent those wheels yourself, or you can tell users that those frontends are not supported by histbook.

jpivarski commented 6 years ago

Thanks for telling me about that— in case it didn't come through on the way I wrote it, I'm excited that this is an option and want to make some examples. I definitely would like to be able to say, "to work with Altair, do this..."

Access to additional frontends is good (unlikely to interest the users I have in mind, but there could be others), but I'm particularly excited about the prospect of mixing histbook histograms with non-histograms from Altair. Altair looks like a great project for general plotting (i.e. what I used to use gnuplot for), and a data analysis isn't all histograms.

jpivarski commented 6 years ago

I just got a chance to test it, and indeed h.step().to(alt.Chart.from_dict) works! (That is, after its validator corrected some errors in my Vega-Lite rendering: undef vs null.)

In a version I'm about to push, users will be able to select front-ends by

Indeed, ipyvega works for Jupyter Notebook and not for Jupyter Lab (even after I updated both). I could have sworn I tested that, but I must be misremembering. I don't see any activity about it on their site, so I'll post an issue. For now, though, Altair remains the only frontend that works in Altair.

Test results in Jupyter Notebook

Test results in Jupyter Lab

jakevdp commented 6 years ago

Its a known and essentially unfixable issue that ipyvega doesn't work on Jupyterlab. We chose to address compatibility across frontends within Altair, and leave ipyvega as the compatibility layer for the classic notebook only.