Open samzhang111 opened 7 years ago
I've thought about this problem a bit, and I do think the solution should go into metakernel so that all derived kernels could set matplotlib up. I would look to see exactly what the %matplotlib
magic does, and then we can add hooks to do similar things in metakernel.
It does look like there is a %plot magic, which has an inline
attribute. I tried to run it with Hy but it opened a Tk backend. Is there something we need to implement in the Hy-specific code after all to get this to work?
https://github.com/Calysto/metakernel/blob/master/metakernel/magics/plot_magic.py https://github.com/Calysto/metakernel/issues/10 (which you're a part of :)
Yes, that is the API of how we could configure, say matplotlib in Hy. Currently, I think it is only used to configure plotting in Octave. All we need to do it have Hy "do the right thing" when the plot settings are made.
Do you think we'd be able to re-use some code, like the InlineBackend, from ipykernel? After spelunking in the ipython code for 20 mins I'm still not quite sure how matplotlib is made to not open a backend, and instead make the images accessible to the kernel.
I was looking at the octave kernel and it looks like there the images get written to disk, then read again.
It also seems like there's a bit of detail involved in making sure the plot settings get set correctly with the metakernel base class. Is this a high enough priority for you that you could give me more instructions, or if you want, just take it yourself :) ?
Edit: I've found that https://github.com/bollwyvl/hy_kernel is able to plot inline, so this isn't an urgent request
Yes, I think reuse or a variation of ipykernel is the way to go. I got lost in there too. The other way to do this is to just figure out how to to it directly, and then make %plot do that as simply as possible. I'll have to look at @bollwyvl 's code... I didn't realize he had that working. That should be a good hint at what metakernel needs to do in general.
BTW, here is an easy way to hook into IPython's magics:
get_ipython().magic(u'matplotlib inline')
More at: https://stackoverflow.com/questions/35145509/why-is-ipython-display-image-not-showing-in-output
I created a reader macro (this is for hy 0.13 and newer)
(import [IPython [get_ipython]]
[IPython.terminal.interactiveshell [TerminalInteractiveShell]])
(cond
[(not (TerminalInteractiveShell.initialized))
(TerminalInteractiveShell.instance)])
(defsharp % [expr]
`(.magic (get_ipython) '~expr))
with this you can execute your magics like this:
#% matplotlib
@dsblank maybe you can add it to the initialisation of calysto
Sure! Is it possible for you to make a PR?
Yes ... and be aware of syntax issues. defsharp has been some intermediate name.
from defreader > defsharp > and now deftag in the current master.
Because Calysto Hy is based on Metakernel, I think it has a mechanism to run start up code (in the kernel's language) when the kernel starts. See https://github.com/Calysto/metakernel/tree/master/metakernel for more information.
For whomever might still be concerned, I ran into this issue while trying to get Calysto Hy working today. I wasn't able to get a workaround going—I'm using the Jupyter extension for VS Code.
I realize this might be a
metakernel
issue, so feel free to redirect me if that is the case. I am trying to run a scientific Python stack with pandas, matplotlib, etc. with this notebook, but it seems like I can't plot inline.Any directions on where to start looking? I'd be happy to help contribute a patch.