Table#
The table widget is a widget for displaying tabular data. It can be instantiated with the list of headings and then data rows can be added.

Usage#
import toga
table = toga.Table(['Heading 1', 'Heading 2'])
# Append to end of table
table.data.append('Value 1', 'Value 2')
# Insert to row 2
table.data.insert(2, 'Value 1', 'Value 2')
Reference#
- class toga.widgets.table.Table(headings, id=None, style=None, data=None, accessors=None, multiple_select=False, on_select=None, on_double_click=None, missing_value=None, factory=None)#
A Table Widget allows the display of data in the form of columns and rows.
- Parameters:
headings (
list
ofstr
) – The list of headings for the table.id (str) – An identifier for this widget.
data (
list
oftuple
) – The data to be displayed on the table.accessors – A list of methods, same length as
headings
, that describes how to extract the data value for each column from the row. (Optional)style (
Style
) – An optional style object. If no style is provided` then a new one will be created for the widget.on_select (
callable
) – A function to be invoked on selecting a row of the table.on_double_click (
callable
) – A function to be invoked on double clicking a row of the table.missing_value (
str
orNone
) – value for replacing a missing value in the data source. (Default: None). When ‘None’, a warning message will be shown.
Examples
>>> headings = ['Head 1', 'Head 2', 'Head 3'] >>> data = [] >>> table = Table(headings, data=data)
Data can be in several forms. A list of dictionaries, where the keys match the heading names:
>>> data = [{'head_1': 'value 1', 'head_2': 'value 2', 'head_3': 'value3'}), >>> {'head_1': 'value 1', 'head_2': 'value 2', 'head_3': 'value3'}]
A list of lists. These will be mapped to the headings in order:
>>> data = [('value 1', 'value 2', 'value3'), >>> ('value 1', 'value 2', 'value3')]
A list of values. This is only accepted if there is a single heading.
>>> data = ['item 1', 'item 2', 'item 3']
Create a base Toga widget.
This is an abstract base class; it cannot be instantiated.
- Parameters:
id – The ID for the widget.
style – A style object. If no style is provided, a default style will be applied to the widget.
- add_column(heading, accessor=None)#
Add a new column to the table.
- Parameters:
heading (
string
) – title of the columnaccessor – accessor of this new column
- property data#
The data source of the widget. It accepts table data in the form of
list
,tuple
, orListSource
- Returns:
Returns a (
ListSource
).
- property missing_value#
- property multiple_select#
Does the table allow multiple rows to be selected?
- property on_double_click#
The callback function that is invoked when a row of the table is double clicked. The provided callback function has to accept two arguments table (
Table
) and row (Row
orNone
).The value of a column of row can be accessed with row.accessor_name
- Returns:
(
callable
) The callback function.
- property on_select#
The callback function that is invoked when a row of the table is selected. The provided callback function has to accept two arguments table (
Table
) and row (Row
orNone
).The value of a column of row can be accessed with row.accessor_name
- Returns:
(
callable
) The callback function.
- remove_column(column)#
Remove a table column.
- Parameters:
column (
int
) – accessor or position (>0)
- scroll_to_bottom()#
Scroll the view so that the bottom of the list (last row) is visible.
- scroll_to_row(row)#
Scroll the view so that the specified row index is visible.
- Parameters:
row – The index of the row to make visible. Negative values refer to the nth last row (-1 is the last row, -2 second last, and so on)
- scroll_to_top()#
Scroll the view so that the top of the list (first row) is visible.
- property selection#
The current selection of the table.
A value of None indicates no selection.
If the table allows multiple selection, returns a list of selected data nodes. Otherwise, returns a single data node.
The value of a column of the selection can be accessed with selection.accessor_name (for single selection) and with selection[x].accessor_name (for multiple selection)