Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue formatting cells with custom IPython magic commands #275

Closed
TimeTravelPenguin opened this issue Jul 15, 2023 · 1 comment
Closed
Assignees
Labels
triage-needed Issue is not triaged.

Comments

@TimeTravelPenguin
Copy link

TimeTravelPenguin commented Jul 15, 2023

Using the VSCode extensions, I am working with Manim, which has a custom magic command %%manim. Note that I am working within an Anaconda Environment. The extension is set to use the bundled import strategy.

I have added to the args the required parameter to add the custom command. In my VSCode settings.json:

"black-formatter.args": [
    "--python-cell-magics",
    "manim"
  ]

Formatting an .ipynb notebook with the following at the top of my cell gives me an error

%%manim -v WARNING --disable_caching TestAnimation

The error I get is:

Syntax error in code: Traceback (most recent call last):
  File "c:\Users\<user>\.vscode\extensions\ms-python.black-formatter-2023.4.1\bundled\tool\lsp_server.py", line 87, in is_python
    ast.parse(code)
  File "c:\Users\<user>\AppData\Local\Programs\Python\Python311\Lib\ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 1
    %%manim -v WARNING --disable_caching ODE
    ^
SyntaxError: invalid syntax

I can see in the Output tab:

2023-07-15 21:05:38.740 [info] [Trace - 9:05:38 PM] Sending request 'textDocument/formatting - (1)'.
2023-07-15 21:05:38.740 [info] [Trace - 9:05:38 PM] Sending request 'textDocument/formatting - (2)'.
2023-07-15 21:05:38.740 [info] [Trace - 9:05:38 PM] Sending request 'textDocument/formatting - (3)'.
2023-07-15 21:05:38.744 [info] [Trace - 9:05:38 PM] Received notification 'window/logMessage'.
2023-07-15 21:05:38.744 [info] c:\Users\<user>\anaconda3\envs\manim\python.exe -m black --python-cell-magics manim --stdin-filename d:\VideoProjects\functional_mathematics\notebook.py -
2023-07-15 21:05:38.744 [info] [Trace - 9:05:38 PM] Received notification 'window/logMessage'.
2023-07-15 21:05:38.744 [info] CWD formatter: d:\VideoProjects\functional_mathematics
2023-07-15 21:05:38.760 [info] [Trace - 9:05:38 PM] Received notification 'window/logMessage'.
2023-07-15 21:05:38.760 [info] reformatted d:\VideoProjects\functional_mathematics\notebook.py

All done! ✨ 🍰 ✨
1 file reformatted.

2023-07-15 21:05:38.760 [info] [Trace - 9:05:38 PM] Received response 'textDocument/formatting - (1)' in 20ms.
2023-07-15 21:05:38.763 [info] [Trace - 9:05:38 PM] Received notification 'window/logMessage'.
2023-07-15 21:05:38.763 [info] [Error - 9:05:38 PM] Syntax error in code: Traceback (most recent call last):
  File "c:\Users\<user>\.vscode\extensions\ms-python.black-formatter-2023.4.1\bundled\tool\lsp_server.py", line 87, in is_python
    ast.parse(code)
  File "c:\Users\<user>\anaconda3\envs\manim\Lib\ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 1
    %%manim -v WARNING --disable_caching ODE
    ^
SyntaxError: invalid syntax

2023-07-15 21:05:38.764 [info] [Trace - 9:05:38 PM] Received notification 'window/showMessage'.
2023-07-15 21:05:38.764 [info] [Trace - 9:05:38 PM] Received notification 'window/logMessage'.
2023-07-15 21:05:38.764 [info] [Warn  - 9:05:38 PM] Skipping non python code: d:\VideoProjects\functional_mathematics\notebook.ipynb
2023-07-15 21:05:38.765 [info] [Trace - 9:05:38 PM] Received notification 'window/showMessage'.
2023-07-15 21:05:38.765 [info] [Trace - 9:05:38 PM] Received response 'textDocument/formatting - (2)' in 26ms.
2023-07-15 21:05:38.766 [info] [Trace - 9:05:38 PM] Received notification 'window/logMessage'.
2023-07-15 21:05:38.766 [info] c:\Users\<user>\anaconda3\envs\manim\python.exe -m black --python-cell-magics manim --stdin-filename d:\VideoProjects\functional_mathematics\notebook.py -
2023-07-15 21:05:38.766 [info] [Trace - 9:05:38 PM] Received notification 'window/logMessage'.
2023-07-15 21:05:38.766 [info] CWD formatter: d:\VideoProjects\functional_mathematics
2023-07-15 21:05:38.777 [info] [Trace - 9:05:38 PM] Received notification 'window/logMessage'.
2023-07-15 21:05:38.777 [info] reformatted d:\VideoProjects\functional_mathematics\notebook.py

All done! ✨ 🍰 ✨
1 file reformatted.

2023-07-15 21:05:38.777 [info] [Trace - 9:05:38 PM] Received response 'textDocument/formatting - (3)' in 37ms.

I am not sure why it is printing, for example:

2023-07-15 21:05:38.766 [info] c:\Users\<user>\anaconda3\envs\manim\python.exe -m black --python-cell-magics manim --stdin-filename d:\VideoProjects\functional_mathematics\notebook.py -

as the file is notebook.ipynb, not notebook.py. But that may be irrelevant.

Now, when I ran black from my command line, outside of VSCode, using my system's terminal, I got the following response:

> black .\notebook.ipynb

Skipping .ipynb files as Jupyter dependencies are not installed.
You can fix this by running ``pip install "black[jupyter]"``
No Python files are present to be formatted. Nothing to do 😴

I ran the required command, as prompted, and then formatting successfully occurred when I specified the custom magic command, as expected:

> black --python-cell-magics manim .\notebook.ipynb

reformatted notebook.ipynb

All done! ✨ 🍰 ✨
1 file reformatted.

I reattempted to run Black within vscode, but it still gives the same errors as previously listed. Is this feature not supported within the vscode extension?

@github-actions github-actions bot added the triage-needed Issue is not triaged. label Jul 15, 2023
@karthiknadig karthiknadig self-assigned this Jul 17, 2023
@karthiknadig
Copy link
Member

Duplicate of #87

We do some cell formatting by tricking black into thinking the cells are actually python files. This is why you see .ipynb switched to .py. We do have plans to support notebook formatting.

@karthiknadig karthiknadig closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage-needed Issue is not triaged.
Projects
None yet
Development

No branches or pull requests

2 participants