Peeyush Kushwaha
Peeyush Kushwaha

Reputation: 3623

Can I show python code comments as markdown in jupyter?

Is there an extension which will allow you to convert this:

# A code *comment*
v = and_associated_code()
v

To be displayed in the next output cell as first the rendered markdown for A code *comment* and then the output of the second line?

Main use-case is for convenience when the markdown blocks are too small.

It'd be even better, though not required, if there is a way to only show some comments / comments in some cells like this and leave the others with their default behavior.

Upvotes: 4

Views: 986

Answers (2)

manisar
manisar

Reputation: 123

The following, not exactly doing what is asked, is a possible workaround. Until we get the requested extension, this may be used.

The code below will show markdown in IPython from within code cells, while ignoring it when run in normal Python (like usual comments). The constraint is to use strings instead of comments.

Note: I used forbiddenfruit just for aesthetics, that is not a requirement.

import sys
from IPython.display import display, Markdown # check link at the end for improved code
from forbiddenfruit import curse
def display_string(self):
    if 'ipykernel' in sys.modules: display(Markdown(self))
curse(str, "md", display_string)

I placed the above in a separate file, and then I import it anywhere I need it.
After this, the following code (e.g.), when run as a normal script ignores the string-comments:

import numpy as np
r = 5
h = 20
volume = np.pi * r**2 * h
"""Thus we have calculated the **volume** of the *cylinder* by using the formula
$$ V = \pi r^2 h $$""".md()
"Now we'll calcualte the area as per $A = \pi r^2 + 2 \pi r h$.".md()
A = np.pi * r**2 + 2 * np.pi * r * h

but when run in IPython, displays this (alongside executing the python code):

Thus we have calculated the volume of the cylinder by using the formula
                                V = πr2h
Now we'll calculate the area as per A = πr2 + 2πrh.

Considering that triple quoted strings are often used as multiline comments, this solution may be useful in many scenarios, and will not be a significant deviation from using normal comments.

On the plus side, there are two advantages:

  1. We get fine level control by having the option to decide exactly which comments (strings) are shown in the output (as was desired in the question).
  2. We can include dynamic values in the markdown, generated by code execution at run time by using f-strings (courtesy @psychemedia - the comment below) .

Update: I created a github package with improved code and extra options that can be installed with pip. Check github.
Or you may copy the code.

Upvotes: 0

psychemedia
psychemedia

Reputation: 5940

Inspired by @manisar's answer, I came up with a simple IPython magic that allows you to use an IPython / Juptyer notebook code cell to display markdown contained in the cell as a formatted string or f-string/string literal: fstring-magic

Install as pip install git+https://github.com/innovationOUtside/fstring-magic.git and then load as load_ext fstring_magic.

There are two block magics: %%stringformat to run a simple string formatter over the contents of the cell using the local scope and then display an HTML formatted markdown output, and %%fstring to treat the contents of the cell as an f-string in the local scope.

enter image description here

Upvotes: 1

Related Questions