Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set default tags for new cells in a jupyter notebook?

I need to add the tag 'nocell' to most of my cells and it is a bit annoying. Is there a way to setup a notebook so that tags are added by default when creating a new cell? Thanks!

like image 645
Manu Avatar asked Dec 07 '25 09:12

Manu


1 Answers

As far as I know, there isn't a built in way to do this. However, you can look into creating a jupyter extension which does what you need. Using this, you could build either an extension that marks any newly added cells with the tag, or you can create a new button bar action that, when clicked, would insert a cell with the tag.

Update

Here is some more detail on how exactly to do this with a button added by a custom Jupyter extension. From this tutorial, it gives the instructions on adding a custom extension. You will need to tweak the example slightly to make it do what you want. Specifically, you should tweak the main.js file. It needs to be something along these lines:

define([
    'base/js/namespace',
    'base/js/events'
], function (Jupyter, events) {

    var add_cell_tagged_nocell = function () {
        Jupyter.notebook.insert_cell_below();
        Jupyter.notebook.select_next();
        var cell = Jupyter.notebook.get_selected_cell();
        cell.metadata.tags = ["nocell"];
    };

    function load_ipython_extension() {
        // Button to add tagged cell
        Jupyter.toolbar.add_buttons_group([
            Jupyter.keyboard_manager.actions.register({
                'help': 'Add cell tagged nocell',
                'icon': 'fa-play-circle',
                'handler': add_cell_tagged_nocell
            }, 'add-tagged-cell', 'Tagged cell')
        ])
    }
    return {
        load_ipython_extension: load_ipython_extension
    };
});

Then when the extension is loaded, you will get an additional button in the button bar to click. When clicked, a new cell will be inserted below the current cell and have the tag "nocell".

enter image description here

enter image description here

There is a known issue where the cell toolbar does not refresh if you have the tags showing, but the metadata is there and the tags toolbar will be correct if you hide and show it again.

Cheers!

like image 104
cwalvoort Avatar answered Dec 10 '25 04:12

cwalvoort



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!