Table[source]
A component for displaying a table with various features such as filtering, highlighting rows, and handling cell changes.
Examples
from pret import component, create_store, use_store_snapshot, use_event_callback
from metanno import Table
table_state = create_store([
{"id": "1", "date": "2023-01-01", "text": "Sample text 1", "type": "ENT", "labels": ["ready"]},
{"id": "2", "date": "2023-01-03", "text": "Sample text 2", "type": "OTHER", "labels": ["ready", "danger"]},
{"id": "3", "date": "2023-01-05", "text": "Sample text 3", "type": "ENT", "labels": ["blue"]},
{"id": "4", "date": "2023-01-07", "text": "Sample text 4", "type": "OTHER", "labels": ["bad"]},
{"id": "5", "date": "2023-01-09", "text": "Sample text 5", "type": "ENT", "labels": []},
{"id": "6", "date": "2023-01-11", "text": "Sample text 6", "type": "OTHER", "labels": ["custom"]},
{"id": "7", "date": "2023-01-13", "text": "Sample text 7", "type": "FOO", "labels": ["ready"]},
{"id": "8", "date": "2023-01-15", "text": "Sample text 8", "type": "FOO2", "labels": ["danger"]},
{"id": "9", "date": "2023-01-17", "text": "Sample text 9", "type": "FOO3", "labels": ["blue"]},
{"id": "10", "date": "2023-01-19", "text": "Sample text 10", "type": "ENT", "labels": ["bad"]},
{"id": "11", "date": "2023-01-21", "text": "Sample text 11", "type": "OTHER", "labels": ["custom"]},
{"id": "12", "date": "2023-01-23", "text": "Sample text 12", "type": "ENT", "labels": ["ready"]},
{"id": "13", "date": "2023-01-25", "text": "Sample text 13", "type": "OTHER", "labels": ["danger"]},
{"id": "14", "date": "2023-01-27", "text": "Sample text 14", "type": "FOO2", "labels": ["blue"]},
{"id": "15", "date": "2023-01-29", "text": "Sample text 15", "type": "FOO3", "labels": ["bad"]},
])
columns = [
{"key": "id", "kind": "text", "name": "id", "filterable": True},
{"key": "date", "kind": "text", "name": "end", "filterable": True},
{"key": "text", "kind": "text", "name": "text", "filterable": True, "editable": True},
{"key": "type", "kind": "text", "name": "label", "filterable": True, "editable": True, "choices": ["ENT", "OTHER", "STUFF", "FOO", "FOO2", "FOO3"]},
{"key": "labels", "kind": "multi-text", "name": "labels", "filterable": True, "editable": True, "choices": ["ready", "danger", "blue", "bad", "custom"]},
]
@component
def MyTable():
@use_event_callback
def on_cell_change(row_id, row_idx, col, new_value):
table_state[row_idx][col] = new_value
view_state = use_store_snapshot(table_state)
return Table(
primary_key="id",
rows=view_state,
columns=columns,
auto_filter=True,
on_cell_change=on_cell_change,
)
MyTable()
Parameters
| PARAMETER | DESCRIPTION |
|---|---|
rows | The data for each row in the table, where each row is a dictionary mapping column keys to their values. TYPE: |
columns | The columns to display in the table:
TYPE: |
primary_key | The key used to uniquely identify each row in the table. TYPE: |
filters | The current filters applied to the table, mapping column keys to filter values. TYPE: |
highlighted_rows | List of row indices that should be highlighted. TYPE: |
handle | Imperative handle for actions that can be performed on the table, such as scrolling:
TYPE: |
auto_filter | Whether to automatically apply filters as the user types. TYPE: |
input_value | The current input value to show in the input field when the user is editing a cell. If undefined, this is automatically handled by the component. TYPE: |
suggestions | List of suggestions to show when the user is typing in the input field. TYPE: |
position | The current position of the cursor in the table, including:
TYPE: |
style | Custom styles for the table component. TYPE: |
key | A unique key for the component instance, used for React's reconciliation. TYPE: |
on_input_change | Callback triggered when the input value changes in a cell:
TYPE: |
on_scroll_bottom | Callback triggered when the user scrolls to the bottom of the table:
TYPE: |
on_cell_change | Callback triggered when a cell's value changes:
TYPE: |
on_click_cell_content | Callback triggered when the content of a cell is clicked (like a hyperlink):
TYPE: |
on_filters_change | Callback triggered when filters are updated:
TYPE: |
on_mouse_enter_row | Callback triggered when the mouse enters a row:
TYPE: |
on_mouse_leave_row | Callback triggered when the mouse leaves a row:
TYPE: |
on_mouse_hover_row | Callback triggered when the mouse hovers over a row:
TYPE: |
on_position_change | Callback triggered when the cursor position changes:
TYPE: |
on_subset_change | Callback triggered when the subset of visible rows changes:
TYPE: |