Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 788
user2826610 Avatar asked Sep 28 '13 15:09

user2826610


People also ask

How do you change the indentation on a Jupyter notebook?

Implement Auto-Indent Lines ( Ctrl + Alt + I ) and Reformat Code ( Ctrl + Alt + L ) for Python code cells in Jupyter notebook source code representation.

How do you write a multiline in Jupyter notebook?

ctrl+o. If you type ctrl+o, you should be able to add additional lines to the input to run them simultaneously.


2 Answers

The official documentation has an example answering this specific question. 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.

like image 109
AdamAL Avatar answered Sep 27 '22 22:09

AdamAL


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) 
  1. 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!

like image 22
Thoran Avatar answered Sep 27 '22 23:09

Thoran