35

I have defined a function in an IPython notebook and would like to be able to block comment a section of it. Intuitively, I'd expect to be able to highlight a section of code, right click and have an option to comment out the selection but this has not been implemented.

Is there a way to do this?

WalkingRandomly
  • 4,537
  • 5
  • 34
  • 34
  • Thank you for posting this question. It made me realize things I did not know about Jupyter myself. If you select your code block as per how you normally select content, and then just start typing the ''' that start a comment, they will appear around your selected code block. You only type them once and the block is enclosed. This works with quotes for strings, and parentheses used in function coding as well. And probably works for anything that Jupyter knows you normally enclose around a block of content. This was tested on Win7 American keyboard using Anaconda 4.2 distribution of Python – TMWP Mar 14 '17 at 14:09

4 Answers4

55

Default solution

In IPython 2.x and 3.x (cmd|ctrl)-/ works but requires an english (american) keyboard layout, see https://github.com/ipython/ipython/pull/3673.

Other keyboard layouts

In case you have a non-english keyboard layout, you can define a custom keybinding for the codemirror editor via your custom.js. To this end add e.g. the following lines

define([
    'base/js/namespace',
    'base/js/events'
    ],
    function(IPython, events) {
        events.on("app_initialized.NotebookApp",
            function () {
                IPython.Cell.options_default.cm_config.extraKeys = {"Ctrl-," : "toggleComment"};
            }
        );
    }
);

to use Ctrl+, to toggle (block) comments. I use this with a german keyboard layout and IPython 3.0. The previous solution (see edits) worked fine with chrome, but not with firefox.

Old solution (IPython 1.x)

If you are using IPython 1.x you can try the comment-uncomment.js from https://github.com/ipython-contrib/IPython-notebook-extensions - I haven't tried this yet, but I guess its a good start.

Jakob
  • 19,815
  • 6
  • 75
  • 94
  • 1
    @I am not sure how to use your german keyboard solution. I did `ipython3 profile create` and created a file in `~/.config/ipython/profile_default/static/custom/custom.js` with the content above. But it didn't work. – student May 26 '15 at 20:22
  • Which IPython (3.x?) version and which keyboard layout are you using? – Jakob May 27 '15 at 05:13
  • Do you use IPython 2.x or 3.x? – Jakob May 27 '15 at 10:52
  • @student the solution i showed seems to work only with chrome but not with firefox (at least on ubuntu), hence I've updated the code to work with firefox as well. – Jakob May 27 '15 at 20:40
  • Thanks, but it doesn't do anything. Is the path of my custom.js correct? – student May 28 '15 at 19:08
  • 1
    @student you can get to your current custom.js via this address: http://localhost:8888/static/custom/custom.js (adapt your port as necessary). – Jakob May 28 '15 at 19:59
  • Using `Python 2.7.9 |Anaconda 2.2.0 (64-bit) | IPython 3.0.0 ` in IE11 and `Ctrl+#` works for block comments on German keyboard. – feinmann Jul 23 '15 at 13:20
  • @feinmann thanks for the hint, unfortunately this works only in IE and not in FF – Jakob Jul 23 '15 at 13:52
  • @Jakob That's weird. But trust me, this feature isn't worth using IE ;) – feinmann Jul 24 '15 at 08:30
  • 2
    `find . -name "custom.js` gave me multiple results, so to get the path to my custom.js, in a notebook I did `import notebook` then `notebook.__path__`, and added to that path static/custom/custom.js. – P. Camilleri Nov 15 '16 at 09:40
  • for Mac users of anaconda/miniconda, the direct path to the custom.js file is `.//envs//lib/ /site-packages/notebook/static/custom/custom.js` – Agile Bean Oct 22 '18 at 11:05
  • @Jakob does the current iPython version (7.1.1) still support your solution on Mac? I have created the custom.js with your solution (replacing `Ctrl` with `Cmd`) but the shortcut doesn't work. And I verified the custom.js at the server link you provided. – Agile Bean Dec 02 '18 at 05:24
  • @AgileBean I've never used a Mac, thus I cannot answer you question, sorry. Anyway, in the mean time there are other answers to a similar question, maybe you find a solution there (https://stackoverflow.com/q/29885371/2870069). – Jakob Dec 02 '18 at 16:40
  • @Jakob thanks for your notice - I tried them all :) but to no success :,( The only way that works is the cumbersome fiddling with `Alt` and cursor moving described here (https://stackoverflow.com/questions/29885371/how-do-i-comment-out-multiple-lines-in-jupyter-ipython-notebook#) – Agile Bean Dec 03 '18 at 09:54
3

i have a german Keyboard and tried out some keys. The following worked: [strg] + [#]

nomara
  • 31
  • 1
3

Solution that should work for any keyboard layout:

Following this blogpost: https://towardsdatascience.com/jupyter-notebook-extensions-517fa69d2231, you can install some plugins for jupyter notebook with the command:

pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install 

Now launch jupyter and go the new Nbextensions tab. There is a plugin called Comment/Uncomment Hotkey. Activate it and choose your hotkey. For example Alt + C. Now you can comment/uncomment a line or a block by selecting it and using your new hotkey.

  • worked..! Is there any way to comment like putting text to comment within stars? For ex /* text to comment is here.......... line 1 line 2 line 3.....line n */ instead of having hash (#) infront of all lines? – DOT Jan 22 '21 at 12:50
0

For me Ctrl + ^/~. I'm using windows 10 and Jupyter Notebook.

Numbermind
  • 93
  • 1
  • 12