ipyaggrid icon indicating copy to clipboard operation
ipyaggrid copied to clipboard

Grid Selections Not Being Captured

Open Lucid-Will opened this issue 1 year ago • 15 comments

Grid selections aren't being captured by event listener in a Jupyter notebook. I've tried in both Databricks and Microsoft Spark notebooks, neither of which seem to be working.

The grid itself is being populated. I've tested making selections on the grid by row as well as checkbox. I then tested the Export to CSV, Export Rows, Export Columns, and Export Range Data options. Export to CSV creates the CSV export but of the full grid, not the selections. The other export options do not appear to be functioning at all.

I've tried implementing with a button to call grid.get_selected_rows() and capture the selections into a dataframe but the dataframe is empty. I also tried creating a dataframe in a subsequent cell that calls grid.get_selected_rows() as well, also empty dataframe.

I've attached sample notebook code to recreate.

import pandas as pd
import ipywidgets as widgets
import pandas as pd
from ipyaggrid import Grid
from IPython.display import display

Sample data

data = {
    'ID': [1, 2, 3, 4, 5],
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'Age': [24, 30, 35, 22, 29],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],
    'Subscription': ['Yes', 'No', 'Yes', 'No', 'Yes']
}

pdf_subscription_load = pd.DataFrame(data)

Display the DataFrame

display(pdf_subscription_load)

Define the grid options with range selection enabled

grid_options = {
    'columnDefs': [{'headerName': col, 'field': col, 'checkboxSelection': (col == 'subscription_name'), 'sortable': True, 'filter': True} for col in pdf_subscription_load.columns],
    'enableSorting': True,
    'enableFilter': True,
    'enableColResize': True,
    'enableRangeSelection': True,
    'rowSelection': 'multiple',
}

Create the Grid widget with configured options

grid = Grid(grid_data=pdf_subscription_load,
            grid_options=grid_options,
            grid_options_multi=[],  # Additional grid options for multi-grid setups
            columns_fit='size_to_fit',
            index=False,
            keep_multiindex=False,
            compress_data=True,
            quick_filter=True,  # Enable or disable quick filtering
            export_csv=True,  # Enable or disable CSV export
            export_excel=True,  # Enable or disable Excel export
            show_toggle_delete=True,  # Show or hide delete toggle
            show_toggle_edit=True,  # Show or hide edit toggle
            sync_on_edit=True,  # Synchronize edits
            sync_grid=True,  # Synchronize the grid
            paste_from_excel=False,  # Allow pasting from Excel
            export_mode='buttons',  # Set export mode
            export_to_df=True,  # Export to DataFrame
            hide_grid=False,  # Hide or show the grid
            theme='ag-theme-balham')  # Theme

Define a button widget

button = widgets.Button(description="Confirm Selection")

Define an event handler for the button click event

def on_button_click(b):
    # Use the get_selected_rows method to capture selected rows
    selected_rows_df = grid.get_selected_rows()
    # Assuming you have a way to display or use the selected rows DataFrame
    print(selected_rows_df)  # For demonstration, this will print the DataFrame to the output

Attach the event handler to the button

button.on_click(on_button_click)

Display the Grid widget and the button

display(grid)
display(button)

selected_rows_df = grid.get_selected_rows()
display(selected_rows_df)`

Lucid-Will avatar Feb 18 '24 18:02 Lucid-Will