jupyter-widgets / ipywidgets

Interactive Widgets for the Jupyter Notebook
https://ipywidgets.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.17k stars 949 forks source link

When loading from a file the .observe and .on_click functions don't work on a rerun of the cell #3959

Open FishingBehindTheNet opened 1 month ago

FishingBehindTheNet commented 1 month ago

Im using Ipywidgets in a jupytor notbook in VScode. When making a interface i use a .py file to code an load this into the notbook. This looks like the following

import Moduals.TestFile as File

display(File.button)

where the testfile looks as follows

import ipywidgets as widgets

button = widgets.Button(description="Click me")

def F_button(PlacHolder = None):
    print("Press")

button.on_click(F_button)

This works fine until I need to reload the cell in the notebook. After doing this, the .observe or .on_click don't register any input. After the kernel is reloaded, the functions work again until you reload the cell without reloading the kernel.

I expected the .observe to work the second time, but even after resetting all global variables, the problem still persists. I suspect that the button might be renamed because the same occurs when using the code from the test file in a notebook and display(button) is in a second cell. If the first cell is reloaded, the button is not recognized until I rerun the second cell. But then I don't understand why this is not resolved by resetting the global variables.