【问题标题】:How to clear the output in Google Colab via code?如何通过代码清除 Google Colab 中的输出?
【发布时间】:2020-05-24 10:39:24
【问题描述】:

我想定期清除 Google Colab 中某个单元格的输出,该单元格运行带有 !python file.py 的本地 python 文件

我已经在 stackoverflow 上尝试了这个问题的答案:

from google.colab import output output.clear()

from IPython.display import clear_output clear_output()

如果我直接在单元格中而不是通过本地文件运行它们,那么这两个都可以工作。

【问题讨论】:

  • 你知道这是否可能吗?

标签: python jupyter-notebook ipython google-colaboratory


【解决方案1】:
from IPython.display import clear_output 



clear_output()

【讨论】:

    【解决方案2】:

    您可以在 javascript 控制台中使用它来每 1 分钟清除一次输出:

    function ClearOutput(){
        console.log("Cleared Output"); 
        document.querySelector("iron-icon[command = 'clear-focused-or-selected-outputs']").click()
    }
    setInterval(ClearOutput,60000)
    

    请注意,您希望清除输出的代码块必须处于焦点位置。为此,您可以在所需的单元格运行时单击以下图标(如下图链接所示):

    The three dots focus on the currently running cell

    【讨论】:

      【解决方案3】:

      而不是运行

      !python file.py
      

      你可以用这个。

      %run file.py
      

      它会按预期工作。

      【讨论】:

        【解决方案4】:

        IPython 主要用于 Jupyter,如果实现正确,它应该可以在 Colab 中用于本地文件。不要忘记在主要 python 代码中导入库。

        【讨论】:

          猜你喜欢
          • 2019-11-26
          • 2011-06-14
          • 1970-01-01
          • 2020-06-02
          • 1970-01-01
          • 1970-01-01
          • 2020-02-15
          • 2019-07-08
          • 2019-02-11
          相关资源
          最近更新 更多