This seems like a basic question but I've tried for a while and not found a solution with Pyviz Panel:
I'm trying to trigger functions with a click of a button, catch the outputs of functions and print them on the screen at desired locations.
The with output
functionality in ipywidgets is a good example of the kind of thing I'm looking to achieve:
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)
However, I can't figure out how to do this with Panel. Here's a simple example of what I'm trying in 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)
However, the output of the print('Function successfully run')
statement in the callbackfn()
function gets lost in the process. How do I catch this text output?
EDIT:
Added in a little more detail within the callbackfn function to represent my use case more specifically. I have to run functions (third_party_function()
) written by people other than me. These functions print stuff, and I can't (not allowed to) change these functions.