Select to view content in your preferred language

Issue with clear_output() Method in ipywidgets within ArcPro Notebooks

140
0
06-03-2024 06:42 AM
SteveF
by
New Contributor III

Dear Esri Support,

I am a federal government user running ArcPro notebooks in a completely disconnected environment. We use ipywidgets in our notebooks to build UI interfaces that allow analysts to run tools repeatedly without needing coding skills.

I am currently working on a tool that as part of its outputs is a Pandas DataFrame. This tool enables users to quickly modify variables using dropdowns and other widgets. Since the tool is run repeatedly, the previous output needs to be cleared and replaced with the new output, but persist the ui created using ipywidgets, each time it is executed.

To achieve this, I load the output into an ipywidget output widget and use the clear_output() method. According to the documentation, multiple defined outputs can be targeted individually.

In my use case, I use these output widgets to build the UI and to display the output to the user running the tool. However, I have observed that in ArcPro notebooks, running clear_output on a single output widget clears all output widgets. I have tested this in a notebook running in a notebook server, where clear_output works as documented and only clears the specified output.

Here is a code snippet that demonstrates this issue:

 

import ipywidgets as widgets

from IPython.display import display

import pandas as pd

import time

 

# Create Output widgets

df_output = widgets.Output()

text_output = widgets.Output()

 

# Display the Output widgets

display(text_output)

display(df_output)

 

# Display the persistent text in the text_output widget

with text_output:

    print("This text persists across DataFrame changes")

 

# Create and display the initial DataFrame

df = pd.DataFrame({

    'A': [1, 2, 3],

    'B': [4, 5, 6]

})

 

# Function to display a DataFrame in the df_output widget

def display_dataframe(dataframe):

    with df_output:

        display(dataframe)

 

# Function to update the DataFrame and display in the df_output widget

def update_display():

    # Clear the output widget

    df_output.clear_output(wait=True)

    # Create and display the updated DataFrame

    display_dataframe(df)

 

# Display the initial DataFrame

update_display()

 

# Wait for 2 seconds to visualize the initial DataFrame

time.sleep(2)

 

# Update the DataFrame

df = pd.DataFrame({

    'A': [7, 8, 9],

    'B': [10, 11, 12]

})

 

# Update the display

update_display()

 

# Wait for 2 seconds to visualize the updated DataFrame

time.sleep(2)

 

# Update the DataFrame again

df = pd.DataFrame({

    'A': [13, 14, 15],

    'B': [16, 17, 18]

})

 

# Update the display

update_display()

 

In the code, I define an output that contains a text header (simulating a UI element that needs to persist across tool runs) and another output that loads several Pandas DataFrames. In an ArcPro notebook, the text header never displays because calling clear_output on the widget containing the DataFrames clears all output. In a notebook server notebook, the text header persists while the DataFrames change as expected.

 

I would appreciate your assistance in resolving this issue.

UPDATE:  Using Arc Pro 3.1

 

0 Kudos
0 Replies