Reputation: 11
In a Jupyter notebook, I want to use a python code to generate some markdown cells if some conditions are met.
I use Ipython.display.Mardown. It works fine if no conditions are given, but fails to display anything if a condition is given.
Here is a minimal example :
In cell 1, a code that generates the expected Markdown:
from IPython.display import display, Markdown
Markdown("""
# First test
Here, Markdown is used outside a condition test \n
It works as I expect
""")
In cell 2, a code which generates no output cell:
SHOW=True
if SHOW:
Markdown("""
# Second test
Here, Markdown is used inside a condition test \n
It won't show
""")
Using the Ipython.display.display function, the string shows in the output but in a raw form.
In cell 3, a code which generates an output cell but the string is not interpreted as a Markdown:
SHOW=True
if SHOW:
display(Markdown("""
# Third test
Here, I also use the display function. \n
It kind of helps but won't show as I expect
"""))
Upvotes: 1
Views: 3293
Reputation: 21
In your example, Cell 2 doesn't work because the Markdown
is not like print
command, so it only displays when it's the last execution of the cell block.
Cell 3 doesn't work, because you have a new line at the start of your block quote. The following (corrected version of Cell 3) works for me:
SHOW=True
if SHOW:
display(Markdown("""# Third test
Here, I also use the display function. \n
It kind of helps but won't show as I expect
"""))
Upvotes: 2