ByRequest
ByRequest

Reputation: 311

Partial refresh or clear of Jupyter cell display after Python function runs

I am using Python 3.7.1 and Jupyter Notebook 6.0.1 via Anaconda.

I am attempting to run some basic analysis in Python using a function initialized by an ipywidget dropdown list to filter data. If the value in the dropdown changes, my desired state is to have the dropdown list remain on the screen, but refresh any data as filtered by the function. I have constructed the following example in an attempt to further test this out:

import ipywidgets as widgets
from IPython.display import clear_output
def mytest(x):
    outs = widgets.Output()
    outs.clear_output()
    with outs:
        lookhere = mytestfilter.value
        if lookhere==1:
            print("hello")
        if lookhere==0:
            print("goodbye")
    display(outs)

mytestfilter = widgets.Dropdown(options={'night': 0, 'morning': 1}, description="FILTER")
display(mytestfilter)
outs=mytestfilter.observe(mytest, names='value')

Basically, in this example, the desired state would be to erase "hello" or "goodbye" and replace it with the proper term when "FILTER" is changed. Ultimately, this will be adapted to filter a pandas grid. I have tried using Ipython.display and assembling the function to an output to be cleared, however, depending on what I do, it is either clearing everything or clearing nothing.

Things I have tried: -I attempted moving the clear_output to the beginning of the function to clear anything existing. Since on the first run the "outs" variable does not exist, I also attempted housing this in an if statement as well as a try/except. It did not appear to do anything. -I have attempted different orders of assigning the variable, displaying the variable and clearing the output with no success.

My suspicion is the problem may partially be in the way the function is running and the variable exists inside the function. It seems as though it kicks off a new output and once the function is escaped, the output remains in the notebook regardless of what I attempt to clear the next time the function runs.

I referenced the following example when attempting to set up my test: https://github.com/jupyter-widgets/ipywidgets/issues/1744

Upvotes: 2

Views: 5084

Answers (2)

ac24
ac24

Reputation: 5565

Try using a separate output widget (outs = widgets.Output()), which is created outside of your interact function.

import ipywidgets as widgets
from IPython.display import clear_output
outs = widgets.Output()

def mytest(x):
    with outs:
        clear_output()
        lookhere = mytestfilter.value
        if lookhere==1:
            print("hello")
        if lookhere==0:
            print("goodbye")

mytestfilter = widgets.Dropdown(options={'night': 0, 'morning': 1}, description="FILTER")
display(mytestfilter)
display(outs)
mytestfilter.observe(mytest, names='value')

Upvotes: 1

ByRequest
ByRequest

Reputation: 311

I was able to solve it with help from this post: Clear widget area of a cell in a Jupyter notebook from within notebook

It appears the display and output functions must exist outside of my defined function. That being said, I am still stuck on one piece - If someone could explain how to pass multiple variables into this function when called from the button it would be greatly appreciated as once I introduce multiple variables back in I receive errors regarding the positions of the arguments.

Upvotes: 0

Related Questions