quantopian / qgrid

An interactive grid for sorting, filtering, and editing DataFrames in Jupyter notebooks
Apache License 2.0
3.04k stars 421 forks source link

On event not working #322

Open frankghe opened 4 years ago

frankghe commented 4 years ago

Environment

Uninstalled core extensions: @8080labs/qgrid

Description of Issue

Methods qgrid.on() and QgridWidget.on() have no effect, ie. handler is registered, but never called.

Reproduction Steps

Code below can be used to reproduce problem:

import pandas as pd import qgrid df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) grid = qgrid.show_grid(df, show_toolbar=True) grid count=0 def record_change(event, widget): count +=1 print('handler fired') grid.on(names='cell_edited',handler=record_change)

Note: other events also do not fire the event, nor

I also tried to use the example provided in the following page, with same result (graph not updated in this case): https://github.com/quantopian/qgrid-notebooks/blob/master/events.ipynb

What steps have you taken to resolve this already?

...

Anything else?

...

fdobad commented 4 years ago

You forgot the global statement before modifying a local variable

count=0
def record_change(event, widget):
    global count
    count +=1
    print('handler fired')

grid.on(names='cell_edited',handler=record_change)
grid

Avoid global like the plague:

class QgridCounter():

    def record_change(self, event, grid_widget):
        self.count +=1
        print('handler fired %d times'%self.count)

    def __init__(self):
        self.df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
        self.grid_widget = qgrid.show_grid(self.df, show_toolbar=True)
        self.grid_widget.on(names='cell_edited',handler=self.record_change)
        self.count = 0

    def disp(self):
        display(self.grid_widget)

QgridCounter().disp()