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

Bash to python is not supported #8

Closed manycoding closed 5 years ago

manycoding commented 5 years ago

keys = !echo 1

ERROR
Cannot parse: 1:6: keys = !echo 1 Traceback (most recent call last): File "/opt/conda/lib/python3.7/site-packages/lab_black.py", line 67, in format cell = _format_code(cell) File "/opt/conda/lib/python3.7/site-packages/lab_black.py", line 20, in _format_code return format_str(src_contents=code, mode=FileMode()) File "/opt/conda/lib/python3.7/site-packages/black.py", line 669, in format_str src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions) File "/opt/conda/lib/python3.7/site-packages/black.py", line 758, in lib2to3_parse raise exc from None black.InvalidInput: Cannot parse: 1:6: keys = !echo 1

dnanhkhoa commented 5 years ago

Thank you for the issue report. Currently, I haven't found a good solution for this, because Black formatter doesn't support bash inside python code. Maybe we could ignore the line which contains bash code before calling black and then put the line back, it means that the line containing bash code will not be formatted. But I am not sure it will work or not. Do you have any idea?

manycoding commented 5 years ago

Since black is for Python, let's just don't fail (ignore bash\non python cells, I reckon there are more types)

lukedyer-peak commented 5 years ago

Similar error it fails on:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline

with error

ERROR:root:Cannot parse: 5:0: %matplotlib inline
Traceback (most recent call last):
  File "/home/xx/.local/lib/python3.6/site-packages/lab_black.py", line 67, in format
    cell = _format_code(cell)
  File "/home/xx/.local/lib/python3.6/site-packages/lab_black.py", line 20, in _format_code
    return format_str(src_contents=code, mode=FileMode())
  File "/home/xx/.local/lib/python3.6/site-packages/black.py", line 669, in format_str
    src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
  File "/home/xx/.local/lib/python3.6/site-packages/black.py", line 758, in lib2to3_parse
    raise exc from None
black.InvalidInput: Cannot parse: 5:0: %matplotlib inline

and it also fails on other cells that have magic in them like

%%sql df_test <<
select top 10 * from database

with error:

ERROR:root:Cannot parse: 2:7: select top 10 * from stage.taxonomy
Traceback (most recent call last):
  ...
black.InvalidInput: Cannot parse: 2:7: select top 10 * from database

I agree with @manycoding that we should just suppress the errors and not fail on them.

dnanhkhoa commented 5 years ago

Thank you for your feedback and suggestion, Currently, I am a bit busy these days, I will try to sit on it this weekend and get back to you with a fix.

dnanhkhoa commented 5 years ago

Hi, @manycoding and @lukedyer-peak,

Currently, I have updated the extension to tackle your issue, It took more time than I thought :( . Now the extension supports almost IPython's magic commands. Please upgrade to the latest version. Thanks.