ipython notebook 在代码中清除单元格输出 [英] ipython notebook clear cell output in code

查看:40
本文介绍了ipython notebook 在代码中清除单元格输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iPython 笔记本中,我有一个 while 循环,它侦听串行端口并实时print接收到的数据.

In a iPython notebook, I have a while loop that listens to a Serial port and print the received data in real time.

我想要实现的仅显示最新接收到的数据(即仅一行显示最新数据.在单元格输出区域中不滚动)

What I want to achieve to only show the latest received data (i.e only one line showing the most recent data. no scrolling in the cell output area)

我需要(我认为)是在收到新数据时清除旧的单元格输出,然后打印新数据.我想知道如何以编程方式清除旧数据?

What I need(i think) is to clear the old cell output when I receives new data, and then prints the new data. I am wondering how can I clear old data programmatically ?

推荐答案

您可以使用 IPython.display.clear_output 清除单元格的输出.

You can use IPython.display.clear_output to clear the output of a cell.

from IPython.display import clear_output

for i in range(10):
    clear_output(wait=True)
    print("Hello World!")

在此循环结束时,您只会看到一个 Hello World!.

At the end of this loop you will only see one Hello World!.

如果没有代码示例,就不容易为您提供工作代码.可能缓冲最近的 n 个事件是一个很好的策略.每当缓冲区发生变化时,您都可以清除单元格的输出并再次打印缓冲区.

Without a code example it's not easy to give you working code. Probably buffering the latest n events is a good strategy. Whenever the buffer changes you can clear the cell's output and print the buffer again.

这篇关于ipython notebook 在代码中清除单元格输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆