【问题标题】:How to run IPython functions within a normal Python script如何在普通 Python 脚本中运行 IPython 函数
【发布时间】:2020-10-20 10:08:57
【问题描述】:

我知道有很多人支持使用由 IPython 库提供支持的 Jupyter Notebook。然而,我更喜欢在 PyCharm 中工作并编写 Python 脚本。

当我想复制用 Jupyter Notebook 编写的代码时,我发现有时它们会使用 IPython 中的函数。比如这个例子:

    from fastai.vision.widgets import *

    cleaner = ImageClassifierCleaner(learn)
    cleaner

这里的cleaner 是一个具有可以在 Juypter Notebook 中显示的小部件的类。如果我在 Python 脚本中运行该函数,则不会显示小部件。我尝试使用以下代码修复它:

    from fastai.vision.widgets import *

    cleaner = ImageClassifierCleaner(learn)
    from IPython.display import display
    display(cleaner.widget)
    plt.show()

它不起作用。有任何想法吗?谢谢。

ps1:使用print(cleaner)

<PIL.Image.Image image mode=RGBA size=227x128 at 0x7F99C4F97470>
<PIL.Image.Image image mode=RGBA size=226x128 at 0x7F99C3679898>
<fastai.vision.widgets.ImageClassifierCleaner object at 0x7f99f803ec88>

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    Jupyter 小部件(和 display 函数)使用 JavaScript 将输出呈现为笔记本页面中的 HTML 元素,因此您不能在常规 Python 脚本中使用它们。如果您希望能够使用 PyCharm,请here 了解如何在 IDE 中运行 Jupyter 笔记本。

    【讨论】:

      猜你喜欢
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2020-02-25
      • 2011-03-20
      • 2017-09-15
      相关资源
      最近更新 更多