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

how does nb_black locate the cell that is being formatted? #15

Closed yuuuxt closed 4 years ago

yuuuxt commented 4 years ago

I encountered a problem that is presumably caused by nb_black (maybe together with scrathpad in jupyter_contrib_nbextensions)

After hitting ctrl-enter, the current cell isn't formatted, and later I found another cell is replaced by the formatted code. This causes a long notebook to be in a mess and I almost lost lots of work.

using anaconda 2019.10, jupyter_contrib_nbextensions and latest version of black & nb_black.

dnanhkhoa commented 4 years ago

Hi @yuuuxt

I am really sorry to hear about that and hope that you could recover your work

The script is basically using the last running cell in the history log of the Jupyter Notebook to place the formatted code. I am suspecting that maybe another library tried to create/delete a cell or something that changes the history log while my script is being triggered so that caused your problem. Currently, this issue could be fixed completely on JupyterLab but not JupyterNotebook, so please stop using until your problem be resolved. Thanks!

yuuuxt commented 4 years ago

Hi @dnanhkhoa ,

Thank you for your explanation. Luckily I found a backup and recovered the notebook.

I tried but I couldn't reproduce it in a new notebook, maybe it's a rare condition. If it's reproducible, a warning about this is necessary.

hanfried commented 4 years ago

I think I had the some problem some months ago and submitted https://github.com/dnanhkhoa/nb_black/pull/12 as a possible solution to at least protect the integrity of the notebook. (If there are any changes in a cell, while it is executed, don't format and don't change the contents)

Maybe it helps you, I just use my forked branch since that time :snail:

yuuuxt commented 4 years ago

Hi @hanfried ,

I saw your pull request before but wasn't thinking of that kind of situation.

Thank you for your suggestion, but currently I just disable it. In fact I'm not in favor of black's suggested style when I'm mainly using pandas in notebook.

dnanhkhoa commented 4 years ago

Hi @hanfried,

Sorry about that and thank you so much for the pull requests. I just have a look and see that it's clear to me so I will merge it to the master branch.