calysto_hy icon indicating copy to clipboard operation
calysto_hy copied to clipboard

%matplotlib inline does not exist

Open samzhang111 opened this issue 7 years ago • 11 comments

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.

samzhang111 avatar Aug 11 '17 01:08 samzhang111

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.

dsblank avatar Aug 11 '17 02:08 dsblank

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

samzhang111 avatar Aug 11 '17 02:08 samzhang111

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.

dsblank avatar Aug 11 '17 02:08 dsblank

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

samzhang111 avatar Aug 11 '17 03:08 samzhang111

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.

dsblank avatar Aug 11 '17 04:08 dsblank

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

dsblank avatar Aug 15 '17 13:08 dsblank

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

schuster-rainer avatar Feb 03 '18 15:02 schuster-rainer

Sure! Is it possible for you to make a PR?

dsblank avatar Feb 04 '18 05:02 dsblank

Yes ... and be aware of syntax issues. defsharp has been some intermediate name.

from defreader > defsharp > and now deftag in the current master.

schuster-rainer avatar Feb 06 '18 22:02 schuster-rainer

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.

dsblank avatar Jan 31 '19 15:01 dsblank

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.

CFiggers avatar Mar 17 '23 00:03 CFiggers