🔍 Code Extractor

class custom_tabulator

Maturity: 40

A custom extension of Panel's Tabulator widget that overrides the _patch method to handle data updates with special logic for filtering, sorting, and remote pagination scenarios.

File:
/tf/active/vicechatdev/panel_edits/custom_tabulator.py
Lines:
13 - 39
Complexity:
moderate

Purpose

This class extends pn.widgets.Tabulator to provide custom patching behavior when table data is updated. It intercepts patch operations and applies special handling based on the widget's state (filters, sorters, pagination mode). The class ensures proper update sequencing by calling _update_cds(), _update_style(), and _update_selectable() methods after patching. It's designed to work within Panel's reactive framework and uses the @updating decorator to manage update states.

Source Code

class custom_tabulator(pn.widgets.Tabulator):
    @updating
    def _patch(self, patch):
        print("patching")
        if self.filters or self.sorters:
            self._updating = False
            self._update_cds()
            return
        if self.pagination == 'remote':
            self._updating = False
            self._update_cds()
            return
            nrows = self.page_size
            start = (self.page-1)*nrows
            end = start+nrows
            filtered = {}
            for c, values in patch.items():
                values = [(ind, val) for (ind, val) in values
                          if ind >= start and ind < end]
                if values:
                    filtered[c] = values
            patch = filtered
        if not patch:
            return
        super()._patch(patch)
        self._update_style()
        self._update_selectable()

Parameters

Name Type Default Kind
bases pn.widgets.Tabulator -

Parameter Details

patch: A dictionary containing column names as keys and lists of (index, value) tuples as values, representing the data changes to be applied to the table. The patch structure follows Tabulator's patch format for incremental updates.

Return Value

The _patch method does not return a value (implicitly returns None). It performs side effects by updating the internal state of the Tabulator widget through super()._patch(patch) and subsequent update method calls.

Class Interface

Methods

_patch(self, patch: dict) -> None

Purpose: Handles incremental data updates to the Tabulator widget with custom logic for different pagination and filtering states

Parameters:

  • patch: Dictionary mapping column names to lists of (index, value) tuples representing data changes to apply

Returns: None - performs side effects by updating widget state

Attributes

Name Type Description Scope
filters list or dict Inherited from pn.widgets.Tabulator - stores active filter configurations for the table instance
sorters list or dict Inherited from pn.widgets.Tabulator - stores active sorting configurations for the table instance
pagination str Inherited from pn.widgets.Tabulator - pagination mode, can be 'remote', 'local', or None instance
page_size int Inherited from pn.widgets.Tabulator - number of rows to display per page when pagination is enabled instance
page int Inherited from pn.widgets.Tabulator - current page number (1-indexed) when pagination is enabled instance
_updating bool Internal flag to track whether the widget is currently in an update cycle, used to prevent recursive updates instance

Dependencies

  • panel

Required Imports

import panel as pn

Usage Example

import panel as pn

# Assuming @updating decorator is defined
def updating(func):
    def wrapper(self, *args, **kwargs):
        self._updating = True
        result = func(self, *args, **kwargs)
        self._updating = False
        return result
    return wrapper

# Create instance with Tabulator parameters
tabulator = custom_tabulator(
    value={'A': [1, 2, 3], 'B': [4, 5, 6]},
    pagination='remote',
    page_size=10
)

# The _patch method is typically called internally by Panel
# when data updates occur, not directly by users
# Example of what happens internally:
# patch_data = {'A': [(0, 10), (1, 20)]}
# tabulator._patch(patch_data)

# Display in a Panel app
pn.serve(tabulator)

Best Practices

  • This class is designed to be instantiated like a regular Panel Tabulator widget with the same parameters
  • The _patch method is an internal method (indicated by leading underscore) and should not be called directly by users
  • The @updating decorator must be defined in the module scope before the class definition
  • When filters or sorters are active, the method triggers a full update via _update_cds() instead of applying patches
  • When pagination is set to 'remote', the method also triggers a full update rather than patching
  • The class assumes the parent class (pn.widgets.Tabulator) has _update_cds(), _update_style(), and _update_selectable() methods
  • The _updating attribute is used to manage update state and prevent recursive updates
  • Empty patches are ignored to avoid unnecessary processing
  • For local pagination, patches are filtered to only include rows within the current page range

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class Extending_column 55.0% similar

    A Panel ReactiveHTML component that creates a simple div container with an extendable column layout, displaying a parameter value within an HTML template.

    From: /tf/active/vicechatdev/resources/py2html.py
  • class Extending_row 52.0% similar

    A Panel ReactiveHTML component that renders a parameter value inside a div element with the class 'extend_row'.

    From: /tf/active/vicechatdev/resources/py2html.py
  • class Application 51.7% similar

    A custom Bokeh Application subclass that extends BkApplication to integrate with Panel's state management system, handling session creation callbacks and document initialization with templates.

    From: /tf/active/vicechatdev/patches/server.py
  • class Study_progress_v1 46.2% similar

    A Panel ReactiveHTML component that displays study progress as a visual taskbar with completed and pending items represented by SVG icons.

    From: /tf/active/vicechatdev/resources/py2html.py
  • class Study_progress 44.7% similar

    A Panel ReactiveHTML component that displays study progress as a visual taskbar with completed and pending items represented by SVG icons.

    From: /tf/active/vicechatdev/resources/.ipynb_checkpoints/py2html-checkpoint.py
← Back to Browse