Faur
Faur

Reputation: 5968

Jupyter: How to execute cell magic placed in separate file

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?

Upvotes: 2

Views: 2168

Answers (1)

dseuss
dseuss

Reputation: 1141

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.

Upvotes: 5

Related Questions