这似乎是一个基本问题,但我尝试了一段时间,但没有找到Panel的解决方案:
我试图通过单击按钮触发函数,捕捉函数的输出,并将它们打印到所需位置的屏幕上。
ipywidgets中的with output功能是我想要实现的那种东西的一个很好的例子:
import ipywidgets as widgets
from IPython.display import display
button = widgets.Button(description="Click Me!")
output = widgets.Output()
display(button, output)
def on_button_clicked(b):
with output:
print("Button clicked.")
button.on_click(on_button_clicked)但是,我想不出如何用Panel来完成这个任务。下面是我在Pyviz Panel中尝试的一个简单例子。
import panel as pn
pn.extension()
import panel.widgets as pnw
text_output_widget = pnw.StaticText(value='orginal output')
some_button = pnw.Button(name='test',button_type = 'primary')
def callbackfn(WatchedEvents):
text_output_widget.value = 'new output'
print('Function successfully run')
third_party_function() #prints a bunch of stuff
some_button.on_click(callbackfn)
pn.Column(some_button,text_output_widget)但是,print('Function successfully run')语句在callbackfn()函数中的输出会在这个过程中丢失。如何捕捉这个文本输出?
编辑:在callbackfn函数中添加了一些更详细的内容,以更具体地表示我的用例。我必须运行函数(third_party_function()),它是由我以外的人编写的。这些函数打印东西,我不能(不允许)更改这些函数。
发布于 2020-08-23 20:45:16
您可以在代码中添加print语句,并在浏览器控制台中看到打印的语句。

您可以在火狐或Chrome中通过正确的Ctrl+Shift+I访问控制台。
在使用面板时,无法将任何输出输出到单元格输出,这也使我感到非常沮丧。这确实限制了调试能力。感谢面板讨论平台中的一位用户向我指出了这一点。
https://stackoverflow.com/questions/59847714
复制相似问题