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

ignore jupyter magics #36

Closed
basnijholt opened this issue Jan 13, 2020 · 3 comments
Closed

ignore jupyter magics #36

basnijholt opened this issue Jan 13, 2020 · 3 comments
Labels
question Further information is requested

Comments

@basnijholt
Copy link
Contributor

basnijholt commented Jan 13, 2020

With jupytext one can keep notebooks as Markdown files.

Often one uses Jupyter magics (e.g., %%time), when running blacken-docs on the .md files I get many warnings like:

FILENAME_HERE.md:30: code block parse error Cannot parse: 1:0: %load_ext autoreload

Would it be possible to easily ignore those magics?

@asottile
Copy link
Contributor

blacken-docs is just a thin wrapper around black -- if black can't parse it I don't think we can either

and even if we did put some weird substitution in place, I'm not sure how it could survive a round of formatting

so I think the answer here is "probably not" -- thanks for the issue though!

@asottile asottile added the question Further information is requested label Jan 13, 2020
@grst
Copy link

grst commented Apr 6, 2020

I would also be interested in this.
Since the cell magics can only occur at the first line of a code chunk, I think it should be rather easy to exclude them.

In fact, adding a line to your regex parser that skips a line starting with % should already solve the problem.

@MarcoGorelli
Copy link

In fact, adding a line to your regex parser that skips a line starting with % should already solve the problem.

Unfortunately, this would break:

foo = (
'%s'
% 'bar'
)

if black can't parse it I don't think we can either
and even if we did put some weird substitution in place, I'm not sure how it could survive a round of formatting

Just FYI, this might soon be happening (psf/black#2357)

In the meantime, there's my little project nbQA which, like the above PR to black, transform cells using IPython's TransformerManager and ast.parse to preserve/round-trip magics

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants