4

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.

  • when I put add .show() like this pn.Column(some_button, text_output_widget).show() this will open the browser tab and you can actually see the printout in your jupyter notebook when the button is pushed. – Sander van den Oord Jan 21 '20 at 19:38
  • Is your print function only for checking if your function is finished, so more like logging? Because the callbackfn IS working and changing your text_output_widget.value and this result gets displayed. – Sander van den Oord Jan 21 '20 at 20:36
  • The print function is more of a stand-in for other functions folks in my team have written. It's actually going to be more like: `def callbackfn(WatchedEvents): someone_elses_fn()` and i need to be able to catch that output and pipe it to the correct part of the Panel layout. – Nirjhor Chakraborty Jan 21 '20 at 21:58
  • I'm not seeing that when I do .show(). Instead I'm only getting `` printed on my notebook. I think that's because I'm not running Jupyter on my local machine but on a remote server. Anyway, not sure how to retrieve the output with .show(). Appreciate the tip though. – Nirjhor Chakraborty Jan 21 '20 at 22:03
  • Just wondering: why don't you create another text_output_widget where your write the result to as a variable. Or use something like param: https://panel.holoviz.org/user_guide/Param.html – Sander van den Oord Jan 22 '20 at 08:31
  • Sure. Anything works. But I've just started using Panel and Param. Not sure how I would do that (write the result to a variable). I updated my question to describe my use case more specifically. I'd be grateful if you could give some code example. – Nirjhor Chakraborty Jan 22 '20 at 16:51

1 Answers1

1

You can add print statements in your code and see the printed statement in your browser console. Console output You can access the console by right Ctrl+Shift+I in Firefox or Chrome.

I was also really frustrated that I could not print anything out to the cell output while using panel. This really limited the debugging ability. Thanks to a user in the Panel Discourse platform for pointing this out to me.

Rithwik
  • 1,128
  • 1
  • 9
  • 28