Notebook: Block Comment Indentation

Created on 14 Jan 2016  路  13Comments  路  Source: jupyter/notebook

When trying to comment a line within a block, it seems to make more sense to indent the comment symbol as well.

For example:

if 10 + 5 == 15:
    print("Yay")
    print("Woohoo")

When commenting the first line inside the block, I expect the code to look like this:

if 10 + 5 == 15:
    # print("Yay")
    print("Woohoo")

but notebook defaults to this:

if 10 + 5 == 15:
#    print("Yay")
    print("Woohoo")

Here is a gif describing the above:

This is the default way of commenting in Sublime Text.

Is this possible to configure in jupyter notebook? Thanks!

Most helpful comment

Hi @minrk , just to follow up. I can see that CodeMirror devs is saying there is no bug in CodeMirror, and

It's simply a matter of calling toggleComment with indent: true

So, how would I go about changing my notebook's behaviour such that toggleComment is called with indent=true? Is there a configuration setting, or would I have to implement it myself by altering the Jupyter Notebook source code?

All 13 comments

The notebook just triggers CodeMirror's toggleComment action. How that's performed is entirely up to CodeMirror, you may want to ask over there if there's a way to influence this.

Hi @minrk , just to follow up. I can see that CodeMirror devs is saying there is no bug in CodeMirror, and

It's simply a matter of calling toggleComment with indent: true

So, how would I go about changing my notebook's behaviour such that toggleComment is called with indent=true? Is there a configuration setting, or would I have to implement it myself by altering the Jupyter Notebook source code?

Even if the issue is as simple as providing a configurable option that is then passed to CodeMirror, it still seems like this is a relevant "issue" that should be reopened, no @minrk?
@scholer was there an issue on CodeMirror (that was then closed) about this, or just a private (e.g. e-mail conversation)?

Hi @lzkelley , I looked at it briefly until I realized it isn't possible to configure Jupyter Notebook to pass arguments when calling CodeMirror functions from the keymap options (or at least, it wasn't obvious to me how to do so).

My understanding from a cursory review:

  • Keys are bound by a simple "Ctrl-/" : "toggleComment", entry in options.cm_config.extra_keys. The value, e.g. toggleComment is the name of a function.
  • The named function is referenced in lookupKey(...) where it is passed, as found to handle(found).

The current Notebook code doesn't seem to anticipate having to pass any arguments/options when calling lookupKey(...) or handle(found).

I see two obvious paths to solving this:

  1. Either Add support for defining arguments to be passed (e.g. pass options object with {indent: true}, in addition to the function name (toggleComment).
  2. Or create a new function that simply calls toggleComment with {indent: true}.

However, I don't have the insight into Jupyter Notebook development to know which approach is preferable.

Edit: Just saw that @juhasch has updated jupyter_contrib_nbextensions, updating the toggle_comment function to call cm.toggleComment with {indent: true/false} in a user-configurable manner.

@scholer: It looked like a simple enough addition. Glad if it helps.

@juhasch thanks for this! Can you clarify how to enable this?

I tried doing:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable comment-uncomment/comment_uncomment_indent  # Fails
jupyter nbextension enable comment-uncomment/indent  # Fails
jupyter nbextension enable comment-uncomment # Fails

The errors I get look like:

Enabling notebook extension comment-uncomment/comment_uncomment_indent...
      - Validating: problems found:
        - require?  X comment-uncomment/comment_uncomment_indent

You need to do

jupyter nbextension enable comment-uncomment/main

or activate the extension using the configurator.

The installation docs can be found here.

Cannot get this to work.

Installed and enable those cryptic nbextension (my love for jupyter is crumbling fast), but no cigar. Block comments still start at beginning of line.
nbconfig/notebook.json has this

{
  "load_extensions": {
    "nbextensions_configurator/config_menu/main": true,
    "contrib_nbextensions_help_item/main": true,
    "comment-uncomment/main": true
  },
  "comment_uncomment_indent": true
}

Which... to me means the proper indentation should work.

This may be related to you keyboard shortcut not working with the extension. Did you use Alt-C to toggle the comment ?

@juhasch Rather, Ctrl /.

Ctrl / is a standard notebook hotkey. This is not what the extension uses. I can't say if it is possible to override the standard hotkey and use Ctrl / with the extension, as I have no US keyboard and therefore can't type Ctrl /.

Try using Alt c or changing the hotkey in the extension to Ctrl /.

Hi, does this fix also works with JupyterLab?

Is there any update to this?

I am using Mac with US keyboard.
I tried with these and it does not work
Ctrl + /
Cmd + /

What works is this:
Alt + /

Seems like the either the default setting of standard hotkey can not be overridden or comment-uncomment extension only works with Alt.

I think it would be more intuitive to be able to use OS specific key with this extension to do commenting just like what common editor does.

Was this page helpful?
0 / 5 - 0 ratings