Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure indentation size in my Jupyter notebooks?

I'd like to have a default indentation size of 2 spaces instead of 4 spaces in my Jupyter notebooks. How can I do this?

Note: this is not a duplicate of How do I change the autoindent to 2 space in IPython notebook, as that question is for (deprecated) IPython notebooks and not (current) Jupyter notebooks.

like image 414
rd11 Avatar asked Nov 25 '16 08:11

rd11


People also ask

How do you indent in Jupyter Notebook markdown?

Use the greater than sign (>) followed by a space, for example: > Text that will be indented when the Markdown is rendered. Any subsequent text is indented until the next carriage return.

What is indentation error in Jupyter Notebook?

The indentation error can occur when the spaces or tabs are not placed properly. There will not be an issue if the interpreter does not find any issues with the spaces or tabs. If there is an error due to indentation, it will come in between the execution and can be a show stopper.


1 Answers

The correct way to do this is in bpirvu's buried answer to How do I change the autoindent to 2 space in IPython notebook:

Just edit ~/.jupyter/nbconfig/notebook.json. Here is a complete notebook.json which includes the relevant setting:

{
  "CodeCell": {
    "cm_config": {
      "indentUnit": 2
    }
  }
}

There's also a more hacky solution that's more prevalent around the web, probably because the Jupyter documentation is lacking on this topic and indentUnit is only mentioned here: http://jupyter-notebook.readthedocs.io/en/latest/frontend_config.html. This solution is to open your browser's JavaScript console and enter

var cell = Jupyter.notebook.get_selected_cell();
var config = cell.config;
var patch = {
      CodeCell:{
        cm_config:{indentUnit:2}
      }
    }
config.update(patch)

which ends up editing ~/.jupyter/nbconfig/notebook.json for you.

like image 91
rd11 Avatar answered Sep 21 '22 06:09

rd11