qgrid

Qgrid API Documentation

Qgrid is an Jupyter notebook widget which uses SlickGrid to render pandas DataFrames as interactive grid controls.

Other qgrid resources

This page hosts only the API docs for the project. You might also be interested in these other qgrid-related resources:

qgrid on GitHub
This is where you’ll find the source code and the rest of the documentation for the project, including the instructions for installing and running qgrid.
qgrid demo on binder

Click the badge below or the link above to try out qgrid in your browser. You’ll see a brief loading screen and then a notebook will appear:

https://beta.mybinder.org/badge.svg

qgrid Module

class qgrid.QgridWidget(df=None, grid_options=None, precision=None, show_toolbar=None)

The widget class which is instantiated by the ‘show_grid’ method, and can also be constructed directly. All of the parameters listed below can be read/updated after instantiation via attributes of the same name as the parameter (since they’re implemented as traitlets).

When new values are set for any of these options after instantiation (such as df, grid_options, etc), the change takes effect immediately by regenerating the SlickGrid control.

Parameters:
  • df (DataFrame) – The DataFrame that will be displayed by this instance of QgridWidget.
  • grid_options (dict) – Options to use when creating the SlickGrid control (i.e. the interactive grid). See the Notes section below for more information on the available options, as well as the default options that this widget uses.
  • precision (integer) – The number of digits of precision to display for floating-point values. If unset, we use the value of pandas.get_option(‘display.precision’).
  • show_toolbar (bool) – Whether to show a toolbar with options for adding/removing rows. Adding/removing rows is an experimental feature which only works with DataFrames that have an integer index.

Notes

The following dictionary is used for grid_options if none are provided explicitly:

{
    'fullWidthRows': True,
    'syncColumnCellResize': True,
    'forceFitColumns': True,
    'defaultColumnWidth': 150,
    'rowHeight': 28,
    'enableColumnReorder': False,
    'enableTextSelectionOnCells': True,
    'editable': True,
    'autoEdit': False,
    'explicitInitialization': True,
    'maxVisibleRows': 15,
    'minVisibleRows': 8,
    'sortable': True,
    'filterable': True,
    'highlightSelectedCell': False,
    'highlightSelectedRow': True
}

Most of these options are SlickGrid options which are described in the SlickGrid documentation. The exceptions are the last 6 options listed, which are options that were added specifically for Qgrid and therefore are not documented in the SlickGrid documentation.

The first two, maxVisibleRows and minVisibleRows, allow you to set an upper and lower bound on the height of your Qgrid widget in terms of number of rows that are visible.

The next two, sortable and filterable, control whether qgrid will allow the user to sort and filter, respectively. If you set sortable to False nothing will happen when the column headers are clicked. If you set filterable to False, the filter icons won’t be shown for any columns.

The last two, highlightSelectedCell and highlightSelectedRow, control how the styling of qgrid changes when a cell is selected. If you set highlightSelectedCell to True, the selected cell will be given a light blue border. If you set highlightSelectedRow to False, the light blue background that’s shown by default for selected rows will be hidden.

See also

set_defaults
Permanently set global defaults for the parameters of the QgridWidget constructor, with the exception of the df parameter.
set_grid_option
Permanently set global defaults for individual SlickGrid options. Does so by changing the default for the grid_options parameter of the QgridWidget constructor.
df

DataFrame – Get/set the DataFrame that’s being displayed by the current instance. This DataFrame will NOT reflect any sorting/filtering/editing changes that are made via the UI. To get a copy of the DataFrame that does reflect sorting/filtering/editing changes, use the get_changed_df() method.

grid_options

dict – Get/set the SlickGrid options being used by the current instance.

precision

integer – Get/set the precision options being used by the current instance.

show_toolbar

bool – Get/set the show_toolbar option being used by the current instance.

add_row()

Append a row at the end of the dataframe by duplicating the last row and incrementing it’s index by 1. The feature is only available for DataFrames that have an integer index.

get_changed_df()

Get a copy of the DataFrame that was used to create the current instance of QgridWidget which reflects the current state of the UI. This includes any sorting or filtering changes, as well as edits that have been made by double clicking cells.

Return type:DataFrame
get_selected_df()

Get a DataFrame which reflects the current state of the UI and only includes the currently selected row(s). Internally it calls get_changed_df() and then filters down to the selected rows using iloc.

Return type:DataFrame
get_selected_rows()

Get the currently selected rows.

Return type:List of integers
off(names, handler)

Remove a qgrid event handler that was registered with the current instance’s on method.

Parameters:
  • names (list, str, All (default: All)) – The names of the events for which the specified handler should be uninstalled. If names is All, the specified handler is uninstalled from the list of notifiers corresponding to all events.
  • handler (callable) – A callable that was previously registered with the current instance’s on method.

See also

QgridWidget.on()
The method for hooking up instance-level handlers that this off method can remove.
on(names, handler)

Setup a handler to be called when a user interacts with the current instance.

Parameters:
  • names (list, str, All) – If names is All, the handler will apply to all events. If a list of str, handler will apply to all events named in the list. If a str, the handler will apply just the event with that name.
  • handler (callable) – A callable that is called when the event occurs. Its signature should be handler(event, qgrid_widget), where event is a dictionary and qgrid_widget is the QgridWidget instance that fired the event. The event dictionary at least holds a name key which specifies the name of the event that occurred.

Notes

Here’s the list of events that you can listen to on QgridWidget instances via the on method:

[
    'cell_edited',
    'selection_changed',
    'viewport_changed',
    'row_added',
    'row_removed',
    'filter_dropdown_shown',
    'filter_changed',
    'sort_changed',
    'text_filter_viewport_changed',
    'json_updated'
]

The following bullet points describe the events listed above in more detail. Each event bullet point is followed by sub-bullets which describe the keys that will be included in the event dictionary for each event.

  • cell_edited The user changed the value of a cell in the grid.

    • index The index of the row that contains the edited cell.
    • column The name of the column that contains the edited cell.
    • old The previous value of the cell.
    • new The new value of the cell.
  • filter_changed The user changed the filter setting for a column.

    • column The name of the column for which the filter setting was changed.
  • filter_dropdown_shown The user showed the filter control for a column by clicking the filter icon in the column’s header.

    • column The name of the column for which the filter control was shown.
  • json_updated A user action causes QgridWidget to send rows of data (in json format) down to the browser. This happens as a side effect of certain actions such as scrolling, sorting, and filtering.

    • triggered_by The name of the event that resulted in rows of data being sent down to the browser. Possible values are viewport_changed, filter_changed, sort_changed, add_row, and remove_row.
    • range A tuple specifying the range of rows that have been sent down to the browser.
  • row_added The user added a new row using the “Add Row” button in the grid toolbar.

    • index The index of the newly added row.
  • row_removed The user added removed one or more rows using the “Remove Row” button in the grid toolbar.

    • indices The indices of the removed rows, specified as an array of integers.
  • selection_changed The user changed which rows were highlighted in the grid.

    • old An array specifying the indices of the previously selected rows.
    • new The indices of the rows that are now selected, again specified as an array.
  • sort_changed The user changed the sort setting for the grid.

    • old The previous sort setting for the grid, specified as a dict with the following keys:

      • column The name of the column that the grid was sorted by
      • ascending Boolean indicating ascending/descending order
    • new The new sort setting for the grid, specified as a dict with the following keys:

      • column The name of the column that the grid is currently sorted by
      • ascending Boolean indicating ascending/descending order
  • text_filter_viewport_changed The user scrolled the new rows into view in the filter dropdown for a text field.

    • column The name of the column whose filter dropdown is visible
    • old A tuple specifying the previous range of visible rows in the filter dropdown.
    • new A tuple specifying the range of rows that are now visible in the filter dropdown.
  • viewport_changed The user scrolled the new rows into view in the grid.

    • old A tuple specifying the previous range of visible rows.
    • new A tuple specifying the range of rows that are now visible.

The event dictionary for every type of event will contain a name key specifying the name of the event that occurred. That key is excluded from the lists of keys above to avoid redundacy.

See also

on()
Same as the instance-level on method except it listens for events on all instances rather than on an individual QgridWidget instance.
QgridWidget.off()
Unhook a handler that was hooked up using the instance-level on method.
remove_row()

Remove the currently selected row (or rows) from the table.

qgrid.enable(dataframe=True, series=True)

Automatically use qgrid to display all DataFrames and/or Series instances in the notebook.

Parameters:
  • dataframe (bool) – Whether to automatically use qgrid to display DataFrames instances.
  • series (bool) – Whether to automatically use qgrid to display Series instances.
qgrid.disable()

Stop using qgrid to display DataFrames and Series instances in the notebook. This has the same effect as calling enable with both kwargs set to False (and in fact, that’s what this function does internally).

qgrid.set_defaults(show_toolbar=None, precision=None, grid_options=None)

Set the default qgrid options. The options that you can set here are the same ones that you can pass into QgridWidget constructor, with the exception of the df option, for which a default value wouldn’t be particularly useful (since the purpose of qgrid is to display a DataFrame).

See the documentation for QgridWidget for more information.

Notes

This function will be useful to you if you find yourself setting the same options every time you create a QgridWidget. Calling this set_defaults function once sets the options for the lifetime of the kernel, so you won’t have to include the same options every time you instantiate a QgridWidget.

See also

QgridWidget()
The widget whose default behavior is changed by set_defaults.
qgrid.on(names, handler)

Setup a handler to be called when a user interacts with any qgrid instance.

Parameters:
  • names (list, str, All) – If names is All, the handler will apply to all events. If a list of str, handler will apply to all events named in the list. If a str, the handler will apply just the event with that name.
  • handler (callable) – A callable that is called when the event occurs. Its signature should be handler(event, qgrid_widget), where event is a dictionary and qgrid_widget is the QgridWidget instance that fired the event. The event dictionary at least holds a name key which specifies the name of the event that occurred.

Notes

There is also an on method on each individual QgridWidget instance, which works exactly like this one except it only listens for events on an individual instance (whereas this module-level method listens for events on all instances).

See that instance-level method (linked below) for a list of all events that can be listened to via this module-level on method. Both methods support the same events with one exception: the instance_create event. This event is only available at the module-level and not on individual QgridWidget instances.

The reason it’s not available on individual qgrid instances is because the only time it fires is when a new instance is created. This means it’s already done firing by the time a user has a chance to hook up any event listeners.

Here’s the full list of events that can be listened for via this module-level on method:

[
    'instance_created',
    'cell_edited',
    'selection_changed',
    'viewport_changed',
    'row_added',
    'row_removed',
    'filter_dropdown_shown',
    'filter_changed',
    'sort_changed',
    'text_filter_viewport_changed',
    'json_updated'
]

See also

QgridWidget.on()
Same as this on method except it listens for events on an individual QgridWidget instance rather than on all instances. See this method for a list of all the types of events that can be listened for via either on method.
off()
Unhook a handler that was hooked up using this on method.
qgrid.off(names, handler)

Remove a qgrid event handler that was registered with the on method.

Parameters:
  • names (list, str, All (default: All)) – The names of the events for which the specified handler should be uninstalled. If names is All, the specified handler is uninstalled from the list of notifiers corresponding to all events.
  • handler (callable) – A callable that was previously registered with the ‘on’ method.

See also

on()
The method for hooking up handlers that this off method can remove.
qgrid.set_grid_option(optname, optvalue)

Set the default value for one of the options that gets passed into the SlickGrid constructor.

Parameters:
  • optname (str) – The name of the option to set.
  • optvalue (object) – The new value to set.

Notes

The options you can set here are the same ones that you can set via the grid_options parameter of the set_defaults or show_grid functions. See the SlickGrid documentation for the full list of available options.

qgrid.show_grid(data_frame, show_toolbar=None, precision=None, grid_options=None)

Renders a DataFrame or Series as an interactive qgrid, represented by an instance of the QgridWidget class. The QgridWidget instance is constructed using the options passed in to this function. The data_frame argument to this function is used as the df kwarg in call to the QgridWidget constructor, and the rest of the parameters are passed through as is.

If the data_frame argument is a Series, it will be converted to a DataFrame before being passed in to the QgridWidget constructor as the df kwarg.

See the QgridWidget documentation for descriptions of all of the options that can be set via it’s constructor.

Return type:QgridWidget

See also

QgridWidget()
The widget class that is instantiated and returned by this function.
qgrid.QGridWidget

alias of qgrid.grid.QgridWidget