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

`line_number` no longer valid in latest version of black #2

Closed xoolive closed 5 years ago

xoolive commented 5 years ago

The line_number parameter seems no longer valid in latest version of black which makes lab_black fail (silently).

The following quick fix seems to work ok for now, though I am not sure it is enough for addressing the issue properly.

if sys.version_info >= (3, 6, 0):
    from black import format_str, FileMode

    def _format_code(code):
        return format_str(src_contents=code, mode=FileMode())

Just for the record, since lab_black failed silently, I would recommend to change

except (ValueError, TypeError):
    pass

into

except (ValueError, TypeError) as e:
    logging.warn(e)
dnanhkhoa commented 5 years ago

Thank you for raising up this issue. I think that your solution is fine, I will check and update it as soon as possible. Thanks.

MarvinT commented 5 years ago

https://gist.github.com/MarvinT/a072aa992e977496974aaf492287b08c and https://marvint.github.io/Black-Jupyter/

Have examples for fixing this problem or an alternative method using code-prettify.

dnanhkhoa commented 5 years ago

Hi @xoolive, I have fixed this issue using the code you suggested, also tested on both Jupyter Lab and Jupyter Notebook and it works fine, so please update to the latest version. Thank you.

dnanhkhoa commented 5 years ago

Hi @MarvinT, Thank you for sharing, I have checked your repo out and see that it's really helpful and convenient. :D

xoolive commented 5 years ago

Excellent! Works like a charm on my side.