quantopian / qgrid

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

Filter not working #388

Closed HariniMadhi closed 1 year ago

HariniMadhi commented 1 year ago

Hi, consider the below code. I have 2 types of column in my dataframe. I want to call the show_highlighted and show_included button from inside my code - For simplicity, I have linked them to button clicks.

The aim is to show only those rows that contain True (in case of Highlighted) or '1' (in case of Included)

import ipywidgets as widgets
import pandas as pd
import qgrid

from IPython.display import display

def show_highlighted(button1):
    grid._handle_qgrid_msg_helper({'type':'show_filter_dropdown', 'field':'Highlighted', 'search_val': True})
    grid._handle_qgrid_msg_helper({'field':'Highlighted', 'filter_info':{'field':'Highlighted', 'selected':[0], 'type':'bool', 'excluded':[]}, 'type':'change_filter'})
    button1.description = 'Highlight Done'

def show_included(button2):
    grid._handle_qgrid_msg_helper({'type':'show_filter_dropdown', 'field':'Included', 'search_val':'1'})
    grid._handle_qgrid_msg_helper({'field':'Included', 'filter_info':{'field':'Included', 'selected':[0], 'type':'text', 'excluded':[]}, 'type':'change_filter'})
    button2.description = 'Include Done'

sample_dict = {'Highlighted': [True, False, True, True, False], 'Included': ['1', '0', '0', '1', '1']}
grid = qgrid.show_grid(pd.DataFrame(sample_dict))

button1 = widgets.Button(description = 'Highlight')
button1.on_click(show_highlighted)

button2 = widgets.Button(description = 'Include')
button2.on_click(show_included)

HBox = widgets.HBox([button1, button2])
display(grid, HBox)

The show_included works as expected, but show_highlight is not working. I've changed 'type':'text' to 'type':'bool', but it still does not work. Can anyone point me the issue? Thanks

HariniMadhi commented 1 year ago

Hi, I've resolved the issue. If any one is interested, check the below code. A more appropriate title would be 'Filtering the columns of Qgrid, without GUI'

def show_highlighted(button1):
    grid._handle_qgrid_msg_helper({'field':'Highlighted', 'filter_info':{'field':'Highlighted', 'selected':True, 'type':'boolean', 'excluded':[]}, 'type':'change_filter'})
    button1.description = 'Highlight Done'