FishingBehindTheNet
FishingBehindTheNet

Reputation: 41

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

I'm using IPyWidgets in a Jupyter Notebook in VS Code. When making an interface, I use a .py file to code and load this into the notebook. 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 and .on_click methods 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 .observe to work the second time, but even after resetting all global variables, the problem 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.

Upvotes: 1

Views: 27

Answers (0)

Related Questions