dnanhkhoa / nb_black

A simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically using black.
MIT License
367 stars 41 forks source link

Ignore trailing whitespace in cell comparison #25

Open Iain-S opened 4 years ago

Iain-S commented 4 years ago

Issue If you have trailing whitespace in a Jupyter notebook cell, nb-black won't format it. For example (note the newline):

a=1

 

I have tested it with newlines and spaces.

Fix It happens because nbb_unformatted_code (which comes from self.shell.user_ns["_i" + str(cell_id)]) is stripped of whitespace (not sure why but it is out of our control). Therefore, we should strip whitespace from nbb_cells[i].get_text() before we compare it to nbb_unformatted_code.

dnanhkhoa commented 4 years ago

Hi @Iain-S , thank you for the PR

Actually I haven't tested this yet. Do you have any problem so far with your fix?

Iain-S commented 4 years ago

Hi @dnanhkhoa , I haven't noticed any issues thus far.

A minor problem is that if the user has started to add new blank lines after submission but before completion of the cell, those new lines will be deleted. I suspect that this will be only a minor annoyance but an alternative solution would be to work out why the self.shell.user_ns[] code is stripped of whitespace.