Hiding code cell on launch

Hello everyone,

Is there a way to have all code cells hidden when a notebook is launched/opened? I’m just looking for the same functionality you get when you click on the bar on the left that “collapses” a code cell.

It would be even better if this could be determined by tags set on the cells, similar to what jupyter-book does.

Thanks!

Hi @geirfreysson,
Currently, if you save the notebook after using View > Collapse All Code, it reopens with that same code collapsed. I just tested it in JupyterLab >= 1.0 launched from here and here by collapsing it in one session, saving the notebook and opening in another session. It should work for individual cells if you want that, too.
You can view the state in the meta data by right-clicking on the notebooks in the JupyterLab file browser and opening the notebooks with Open with > Editor and seeing "source_hidden": true for cells.

Or are you asking if you can enforce this for notebooks where this wasn’t previously set?
That probably can be done by reading in a notebook with nbformat and designating the output be adjusted to have that attribute by combining the process outlined here to open a notebook and step through cells with details here to adjust the meta-data in the output.

Hi @fomightez. That’s super helpful. I tested it as well and it works perfectly.

I wonder if it’s a 1.0 thing and I just didn’t noticed this functionality had been added - or maybe I just wasn’t paying attention at all!

Many thanks for your help! Problem solved!

1 Like

I think it may have been put in last year according to here and here.
I am finding the 1.0 milestone definitely allowed a lot of things that had been in flux to be sorted and some rough edges polished nicely. I think I am going to make sure I specify it in all my Binderized repos for the near future.

Just to confirm: yes, it’s something added in 1.0. (I implemented the final version of it in https://github.com/jupyterlab/jupyterlab/pull/5968 :).

1 Like

Thanks, @jasongrout
Oops. Yes, I was getting confused with the output persistence shoring up threads and changelogs.

Well done, it’s crucial for how we’re want to use Jupyter. And thanks for confirming, I was feeling a bit silly asking if it was there the whole time!

1 Like

Thanks for vindicating the days spent on designing and implementing this (and not just by me) :).

3 Likes

@jasongrout I am using JupyterLab >= 1.0 which hosted with JupyterHub but source_hidden metatag is not working. Collapsed true works but not source_hidden and outputs_hidden.

@arouneaj, below is what the json source for my cell looks like, and JupyterLab (v 1.0.2) displays it hidden when the notebook is loaded. Maybe you’re setting the metatag in the wrong place?

{
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
       "jupyter": {
           "source_hidden": true
        }
},
"outputs": []...

Thank you so much. That works. I havent included jupyter, It looked like below so it hasnt worked.
metadata": { “source_hidden”: true },

One more thing, Do you know how I can run all non empty cells upon opening a file. For instance, I open a ipynb file and i need all code cells to be executed

I’ve been looking for a clean way to do this myself, but haven’t found one. What we do is hide cells from the user and present them with a “run all cells” button by using an extension that adds run-all buttons to the toolbar.

Thanks for your sugestion. I am currently trying to achieve this with customJS. It is not fully functional yet.

define([
    'jquery',
    'base/js/namespace',
    'base/js/events'
], function (
    $,
    IPython,
    events
) {
    var run_all_cells = function(){
        var cells = IPython.notebook.get_cells();
        for (var i=0; i < cells.length; i++) {
            var cell = cells[i];
            cell.execute();
        }
    };

    var load_ipython_extension = function() {
        events.on('kernel_ready.Kernel', run_all_cells);
    };

    return {
        load_ipython_extension : load_ipython_extension
    };
});