Calysto / metakernel

Jupyter/IPython Kernel Tools

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible to run code (including magics) on startup?

thomasjm opened this issue · comments

For a kernel based on Metakernel, is there some way to run code on startup? I'm thinking about how with IPython you can put code in ~/.ipython/profile_default/ipython_config.py.

My goal at the moment is with the Octave kernel; I'd like to run %plot -f svg at the beginning of every session because the SVG output works better.

I saw there was some discussion about this in #14. Since the Jupyter/IPython split that has happened since then, it seems like the solution would need to be more IPython-independent than the idea proposed there, especially since Jupyter doesn't support profiles like IPython does.

FWIW I tried putting the following in ~/.jupyter/jupyter_config.py but it didn't work:

c = get_config()
c.InteractiveShellApp.exec_lines = ['%plot -f svg']

Is there a solution here I'm missing or does Metakernel need more work to support this? Thanks!

@blink1073 any chance you know the answer to this? Thanks!

Here is an example of running some setup code the first time code is executed in the scilab kernel. You could also call out to do_execute_direct in your MetaKernel subclass constructor.

If you add an exec_lines trait, you can allow users to specify the lines to run in config. The base class we are using is a trait-based class. Then it is up to you whether to run them at startup or the first time the user calls do_execute_direct.

Thanks @blink1073! FYI it looks like your recent commit 2f88045 added some stuff to README.rst that isn't rendering properly on Github (for me at least). The sentence The base MetaKernel class offers plot_settings as a configurable trait. As an example: trails off and then there's nothing under it.

Thanks for the heads up @thomasjm, I made a pass through the README in 8ff7b0e.

Also @blink1073, thanks for adding that plot_settings option, it's exactly what I needed!

Unfortunately it doesn't seem to work at the moment though. I'm using the recently released Octave kernel 0.30.2, and I created the file ~/.jupyter/metakernel_config.py with the contents

c.OctaveKernel.plot_settings = dict(format='svg')

but when I start a kernel and make plots they still appear in the default PNG format. (I can get SVG if I do %plot -f svg.) Is it possible I'm missing a step or it doesn't work in that release yet?

Drat, you're right. The plot_settings aren't being propagated. I'll have to dig into it.

Everything's working now with plot_settings config, thanks again. Closing...