Skip to main content

A PySide6 DataTable widget with jQuery DataTable-like functionality

Project description

PySide6 DataTable

A powerful DataTable widget for PySide6 applications with functionality similar to jQuery DataTable.

Features

  • Customizable Table: Easily configure columns, types, and formatting
  • Data Type Detection: Automatically detect and handle different data types [NOT-IMPLEMENTED-YET]
  • Type-based Sorting: Different column types sort appropriately
  • Search Functionality: Global and column-specific search
  • Row Collapsing: Support for expandable/collapsible rows
  • Pagination: Built-in pagination with configurable page sizes [SEMI-IMPLEMENTED]
  • Column Visibility: Show/hide columns easily
  • Aggregation Functions: Calculate sums, averages, percentages, etc. [SEMI-IMPLEMENTED]
  • Custom Formatting: Format data display for different column types
  • Observer Pattern: Event-driven architecture for clear code organization

Installation

pip install pyside6-datatable-widget

Basic Usage

import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from datatable import DataTable, DataType

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("DataTable Example")
        self.resize(800, 600)
        
        # Create central widget and layout
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QVBoxLayout(central_widget)
        
        # Create DataTable
        self.data_table = DataTable()
        layout.addWidget(self.data_table)
        
        # Set up columns (key, header, data_type)
        columns = [
            ("id", "ID", DataType.NUMERIC),
            ("name", "Name", DataType.STRING),
            ("age", "Age", DataType.NUMERIC),
            ("active", "Active", DataType.BOOLEAN)
        ]
        
        # Set up data
        data = [
            {"id": 1, "name": "John", "age": 30, "active": True},
            {"id": 2, "name": "Jane", "age": 25, "active": False},
            {"id": 3, "name": "Bob", "age": 40, "active": True}
        ]
        
        # Apply to table
        self.data_table.setColumns(columns)
        self.data_table.setData(data)
        
        # Connect signals
        self.data_table.rowSelected.connect(self.on_row_selected)
        
    def on_row_selected(self, row, row_data):
        print(f"Row {row} selected: {row_data}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

Advanced Features

Custom Column Formatting

from datatable import DataTableModel

# Create model with custom formatting
model = DataTableModel()
model.setFormattingFunction("price", lambda value: f"${value:.2f}")
model.setFormattingFunction("percentage", lambda value: f"{value:.1f}%")

# Set model to table
data_table.setModel(model)

Row Collapsing

# Enable row collapsing
data_table.enableRowCollapsing(True, "subrows")

# Example data with subrows
data = [
    {
        "id": 1,
        "name": "Category A",
        "total": 1000,
        "subrows": [
            {"id": 101, "name": "Item A1", "total": 500},
            {"id": 102, "name": "Item A2", "total": 500}
        ]
    },
    {
        "id": 2,
        "name": "Category B",
        "total": 2000,
        "subrows": [
            {"id": 201, "name": "Item B1", "total": 1200},
            {"id": 202, "name": "Item B2", "total": 800}
        ]
    }
]

data_table.setData(data)

# Connect expansion signals
data_table.rowExpanded.connect(lambda row, data: print(f"Row {row} expanded"))
data_table.rowCollapsed.connect(lambda row, data: print(f"Row {row} collapsed"))

Aggregation Functions

# Get aggregate values
total = data_table.getAggregateValue("amount", "sum")
average = data_table.getAggregateValue("amount", "avg")
count = data_table.getAggregateValue("id", "count")

# Calculate percentage of a row value relative to total
row_data = data_table.getSelectedRow()
if row_data:
    amount = row_data["amount"]
    total = data_table.getAggregateValue("amount", "sum")
    percentage = data_table.calculateRowPercentage(row_index, "amount")

Custom Search Functions

# Set custom search function for a column
model.setSearchFunction("complex_data", lambda value, term: term in str(value["name"]))

# Search in table
data_table.search("search term")

# Search specific column
matching_rows = model.searchColumn("name", "John")

API Reference

DataTable

Main widget class that provides the UI and functionality.

Methods

  • setData(data): Set table data
  • appendRow(row_data): Append a row to the table
  • insertRow(row_index, row_data): Insert a row at a specific index
  • setColumns(columns): Set table columns
  • setVisibleColumns(columns): Set which columns are visible
  • enableRowCollapsing(enabled, child_row_key): Enable/disable row collapsing
  • search(term): Search the table
  • sort(column_key, order): Sort the table
  • setPage(page): Set current page
  • setRowsPerPage(rows): Set rows per page
  • getData(): Get current table data
  • getSelectedRow(): Get selected row data
  • getAggregateValue(column_key, agg_type): Get aggregate value for column

Signals

  • pageChanged(page): Emitted when page changes
  • rowSelected(row, row_data): Emitted when row is selected
  • rowExpanded(row, row_data): Emitted when row is expanded
  • rowCollapsed(row, row_data): Emitted when row is collapsed
  • dataFiltered(rows): Emitted when data is filtered
  • sortChanged(column, order): Emitted when sort order changes

DataTableModel

Model class that manages data and operations.

Methods

  • setData(data): Set model data
  • setColumns(columns): Set model columns
  • setFormattingFunction(column_key, func): Set formatting function
  • setEditableColumns(editable_columns): Set which columns are editable
  • setVisibleColumns(visible_columns): Set which columns are visible
  • setSearchFunction(column_key, func): Set search function
  • setSortFunction(column_key, func): Set sort function
  • setAggregationFunction(column_key, agg_type, func): Set aggregation function
  • enableRowCollapsing(enabled, child_row_key): Enable row collapsing
  • search(term): Search all rows
  • searchColumn(column_key, term): Search specific column
  • aggregate(column_key, agg_type): Aggregate column values
  • calculateRowPercentage(row_index, column_key): Calculate row percentage

Signals

  • rowExpandedCollapsed(int, bool) : row, is_expanded. Emitted when a row, sub-rows expanded or collapsed

Note: The model is child class of QAbstractTableModel. So these signals below are inherited (from QAbstractItemModel).

  • dataChanged(topLeft, bottomRight, roles): Emitted when data in the specified range has been modified.
  • headerDataChanged(orientation, first, last): Emitted when header data for a section changes.
  • layoutChanged(): Emitted when layout of the model changes drastically.
  • layoutAboutToBeChanged(): Emitted before a major layout change.
  • modelReset(): Emitted after the model is reset.
  • rowsAboutToBeInserted(parent, start, end): Emitted before rows are inserted.
  • rowsInserted(parent, start, end): Emitted after rows are inserted.
  • rowsAboutToBeRemoved(parent, start, end): Emitted before rows are removed.
  • rowsRemoved(parent, start, end): Emitted after rows are removed.
  • columnsAboutToBeInserted(parent, start, end): Emitted before columns are inserted.
  • columnsInserted(parent, start, end): Emitted after columns are inserted.
  • columnsAboutToBeRemoved(parent, start, end): Emitted before columns are removed.
  • columnsRemoved(parent, start, end): Emitted after columns are removed.
  • rowsMoved(parent, start, end, destination, row): Emitted after rows are moved.
  • columnsMoved(parent, start, end, destination, column): Emitted after columns are moved.

License

This project is licensed under the GNU General Public License v3.0 (GPLv3).
See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyside6_datatable_widget-1.1.0.4.tar.gz (42.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyside6_datatable_widget-1.1.0.4-py3-none-any.whl (44.1 kB view details)

Uploaded Python 3

File details

Details for the file pyside6_datatable_widget-1.1.0.4.tar.gz.

File metadata

File hashes

Hashes for pyside6_datatable_widget-1.1.0.4.tar.gz
Algorithm Hash digest
SHA256 3fa186e98c1c10eec003615c904fb702c519dc46994cba193e810d2149a3d9f4
MD5 f08e9dd417026c719d4337773f6584d1
BLAKE2b-256 47e4296bf4856cfb54230bf86c71d35b18e0a740d58581c3c5a512cfc0151c52

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyside6_datatable_widget-1.1.0.4.tar.gz:

Publisher: python-publish.yml on ultra-bugs/pyside6-datatable-widget

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyside6_datatable_widget-1.1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pyside6_datatable_widget-1.1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 dc97a185faf80ada8b954de52586679c684808f2c8cb263f341e15d7dcd18521
MD5 5ce94bb027ba3d63c61a9febc75a430b
BLAKE2b-256 9599a83db542ccb309efe4d3e898fc3d1b22fda74db76c3a551bbc0f45632a66

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyside6_datatable_widget-1.1.0.4-py3-none-any.whl:

Publisher: python-publish.yml on ultra-bugs/pyside6-datatable-widget

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page