Python 根据带有条件的代码生成降价

Python 根据带有条件的代码生成降价,python,jupyter-notebook,Python,Jupyter Notebook,在Jupyter笔记本中,如果满足某些条件,我想使用python代码生成一些标记单元格 我使用Ipython.display.Mardown。如果没有给定条件,它可以正常工作,但如果给定条件,它将无法显示任何内容 这是一个 : 在单元格1中,生成预期降价的代码: from IPython.display import display, Markdown Markdown(""" # First test Here, Markdown is used outside a condition te

在Jupyter笔记本中,如果满足某些条件,我想使用python代码生成一些标记单元格

我使用Ipython.display.Mardown。如果没有给定条件,它可以正常工作,但如果给定条件,它将无法显示任何内容

这是一个 :

在单元格1中,生成预期降价的代码:

from IPython.display import display, Markdown

Markdown("""
# First test
Here, Markdown is used outside a condition test \n
It works as I expect
""")
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
    """))
在单元格2中,一种不生成输出单元格的代码:

SHOW=True

if SHOW:
    Markdown("""
    # Second test
    Here, Markdown is used inside a condition test \n
    It won't show
    """)
使用Ipython.display.display函数,字符串以原始形式显示在输出中

在单元格3中,一种生成输出单元格但字符串不被解释为标记的代码:

from IPython.display import display, Markdown

Markdown("""
# First test
Here, Markdown is used outside a condition test \n
It works as I expect
""")
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
    """))

在您的示例中,单元格2不起作用,因为
标记
打印
命令不同,因此它仅在最后一次执行单元格块时显示

单元格3不起作用,因为您在块报价的开头有一个新行。以下(单元格3的更正版本)适用于我:

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
    """))
很酷,如果我删除所有新行,即单个字符串中的所有内容:display(标记(“#第三次测试\n这里,我还使用display函数。\n\n这有点帮助,但不会像我预期的那样显示”)