2

I would like to have one file with some magic commands (e.g. this one) that I can call from multiple (not all) Jupyter Notebooks.

How do I do this?

Toke Faurby
  • 5,788
  • 9
  • 41
  • 62

1 Answers1

5

There are two ways I can think of. First, use the get_ipython().run_cell_magic command. For example you can put this in a ipython_utils.py module

def load_runall():
    get_ipython().run_cell_magic('javascript', '',
        """
        Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', {
            help : 'run all cells',
            help_index : 'zz',
            handler : function (event) {
                IPython.notebook.execute_all_cells();
                return false;
            }}
        );
        """)

load_runall()

And then simply run import ipython_utils in the cell of your notebook. A different approach would be to save to complete cell into a file and load it using the %load magic command and then run the cell normaly.

dseuss
  • 971
  • 6
  • 8
  • Option 1: Would you put all this in a file e.g. `jupyter_utils.py` file, and then in jupyter write `from jupyter_utils import *`, or how do you mean? – Toke Faurby May 25 '17 at 08:58
  • Yes, this is valid python code (when run inside ipython). I have adapted my answer. – dseuss May 25 '17 at 09:10