adamchainz / blacken-docs

Run `black` on python code blocks in documentation files
MIT License
646 stars 43 forks source link

IPython shell examples #127

Open adamchainz opened 3 years ago

adamchainz commented 3 years ago

I tend to use IPython shell examples rather than the plain Python console, and I'd like blacken-docs to format them, e.g.

 .. code-block:: ipython

-    In [1]: print('Hello World')
+    In [1]: print("Hello World")
     Hello World

     In [2]: print(
-       ...: 'Hello World'
+       ...: "Hello World"
        ...: )
     Hello World

Would this be an accepted feature? Happy to give it a shot if so.

asottile commented 3 years ago

if you want to make it work and maintain it sure

MarcoGorelli commented 3 years ago

The tricky part here is dealing with IPython magics, such as %%timeit, which aren't valid Python syntax

Just bringing up black's IPython magic handler, in case it's useful https://github.com/psf/black/blob/main/src/black/handle_ipynb_magics.py (perhaps it's even possible to do this reusing black.format_cell?)

adamchainz commented 2 years ago

This may be somewhat redundant, since IPython has the ability to run Black on input lines during execution. It doesn't help with reformatting existing shell sessions but it covers creating them.