How do I change the autoindent to 2 space in IPython notebook

Ipython NotebookAuto Indent

Ipython Notebook Problem Overview


I find that developing functions in IPython notebook allows me to work quickly. When I'm happy with the results I copy-paste to a file. The autoindent is 4 spaces, but the coding style for indentation at my company is 2 spaces. How do I change the autoindent to 2 spaces?

Ipython Notebook Solutions


Solution 1 - Ipython Notebook

The official documentation has an [example answering this specific question] 1. This worked for me with IPython 4.

Summary: Paste the following into your browser's javascript console

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

The setting is persisted. You can roll back by exchanging : 2 for : null.

Solution 2 - Ipython Notebook

From the official documentation for CodeMirror Code Cells:

  1. Open an Ipython Notebook
  2. Create a Code Cell e.g. by pressing b
  3. Open your browser’s JavaScript console and run the following

snippet:

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

4. Reload the notebook page in the browser e.g. by pressing F5

This will fix it permanently. I assume this works only on recent versions, not sure though!

Solution 3 - Ipython Notebook

AdamAL's answer is correct. It worked for me.

However it only changes the indentation in the Jupyter Notebook and leaves the indentation in the Jupyter Editor unaffected.

A more direct way to change the indentation is to directly edit the Jupyter config files in the .jupyter/nbconfig directory. This directory contains 2 files:

edit.json
notebook.json

The option you must set in either one is indentUnit. Here is the content of my Jupyter config files:

edit.json:

{
  "Editor": {
    "codemirror_options": {
      "indentUnit": 2,
      "vimMode": false, 
      "keyMap": "default"
    }
  }
}

notebook.json:

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

With this approach I've set the default indentation to 2 in both the Jupyter Notebook and the Jupyter Editor.

Solution 4 - Ipython Notebook

Based on this question and the options found here:
In your custom.js file (location depends on your OS) put

IPython.Cell.options_default.cm_config.indentUnit = 2;

On my machine the file is located in ~/.ipython/profile_default/static/custom

Update:

In IPython 3 the plain call does not work any more, thus it is required to place the setting within an appropriate event handler. A possible solution could look like

define([
    'base/js/namespace',
    'base/js/events'
    ],
    function(IPython, events) {
        events.on("app_initialized.NotebookApp",
            function () {
                IPython.Cell.options_default.cm_config.indentUnit = 2;
            }
        );
    }
);

Solution 5 - Ipython Notebook

If you use jupyterlab, there seems to be an easier way:

  1. Click jupyterlab menu Settings > Advanced Setting Editor

  2. Click "Notebook" on the left hand pane, make sure you are on "Raw View"

  3. On the right pane, under "User Overrides", enter this:

    { "codeCellConfig": { "tabSize": 2 } }

If you look at the System Defaults, that will give you hint on whats overridable and you can repeat this for other settings.

I tried this on Google Platform AI Notebook which uses Jupyterlab.

Solution 6 - Ipython Notebook

I believe this is now best wrapped in a event handler to load once per notebook load:

$([IPython.events]).on('app_initialized.NotebookApp', function(){
  IPython.CodeCell.options_default['cm_config']['indentUnit'] = 2;
});

Solution 7 - Ipython Notebook

In addition to adding

IPython.Cell.options_default.cm_config.indentUnit = 2;

to your custom.js file as suggested by Jakob, be sure to clear your browser cache as well before restarting ipython notebook!

Also, you may have to first create the ~/.config/ipython/profile_default/static/custom/ directory (use echo $(ipython locate default) to find your default directory) before adding the custom.js file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2826610View Question on Stackoverflow
Solution 1 - Ipython NotebookAdamALView Answer on Stackoverflow
Solution 2 - Ipython NotebookThoranView Answer on Stackoverflow
Solution 3 - Ipython NotebookbpirvuView Answer on Stackoverflow
Solution 4 - Ipython NotebookJakobView Answer on Stackoverflow
Solution 5 - Ipython NotebookkawingkelvinView Answer on Stackoverflow
Solution 6 - Ipython NotebookTim DierksView Answer on Stackoverflow
Solution 7 - Ipython NotebookBobbyView Answer on Stackoverflow