问题描述
在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 ?
推荐答案
您可以使用以清除一个单元格的输出。
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()
print("Hello World!")
At在这个循环结束时,你只能看到一个 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笔记本清除代码中的单元格输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!