blacken-docs icon indicating copy to clipboard operation
blacken-docs copied to clipboard

IPython shell examples

Open adamchainz opened this issue 4 years ago • 3 comments

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.

adamchainz avatar Nov 08 '21 10:11 adamchainz

if you want to make it work and maintain it sure

asottile avatar Nov 08 '21 13:11 asottile

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?)

MarcoGorelli avatar Nov 08 '21 13:11 MarcoGorelli

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.

adamchainz avatar Mar 30 '22 18:03 adamchainz