Data

Widgets and classes for displaying and managing tabular and file data.

TableView

class mytk.tableview.TableView(columns_labels, is_treetable=False, create_data_source=True)[source]

Bases: Base

A table widget wrapping tkinter Treeview with data binding and editing support.

exception DelegateError[source]

Bases: Exception

Raised when a delegate callback raises an unexpected error.

exception WidgetNotYetCreatedError[source]

Bases: Exception

Raised when accessing widget properties before the widget is created.

property columns

Return the list of column names.

column_info(cid)[source]

Return column configuration info for the given column identifier.

property displaycolumns

Return the list of currently displayed column names.

property headings

Return the list of column heading labels.

property columns_labels

Return a dict mapping column names to their heading labels.

heading_info(cid)[source]

Return heading configuration info for the given column identifier.

item_info(iid)[source]

Return item configuration info for the given item identifier.

create_widget(master)[source]

Create the underlying Treeview widget within the given master container.

source_data_changed(records)[source]

Update the widget to reflect changes in the data source records.

source_data_added_or_updated(records)[source]

Insert new records or update existing ones in the widget.

source_data_deleted(records)[source]

Remove widget items that are no longer present in the data source.

record_to_formatted_widget_values(record)[source]

Convert a data record to a list of formatted strings for display.

extract_record_from_formatted_widget_values()[source]

Extract a record from the formatted widget values (not implemented).

widget_data_changed()[source]

Handle notification that the widget data has changed.

items_ids()[source]

Return all item identifiers in the widget, including nested children.

item_modified(item_id, modified_record)[source]

Update a widget item and its backing data source record.

clear_widget_content()[source]

Delete all items from the widget and return their identifiers.

empty()[source]

Remove all items from the table.

selection_changed(event)[source]

Notify the delegate when the table selection changes.

identify_column_name(event_x)[source]

Return the column name at the given x pixel coordinate.

get_column_name(column_id=None, display_column_number=None)[source]

Return the column name for a given column id or display column number.

get_column_id(column_name)[source]

Return the 1-based column id for the given column name.

get_logical_column_id(column_name)[source]

Return the 0-based logical column index used to access item values.

click(event) bool[source]

Handle a single click event on the table.

click_cell(item_id, column_name)[source]

Handle a single click on a specific cell, opening URLs if applicable.

is_column_sorted(column_name)[source]

Return ‘<’ if sorted ascending, ‘>’ if descending, or None if unsorted.

sorted_column(column_name=None, reverse=False)[source]

Return item identifiers sorted by the given column.

sort_column(column_name=None, reverse=False)[source]

Sort the widget items in place by the given column.

click_header(column_name=None)[source]

Handle a click on a column header, toggling sort order.

doubleclick(event) bool[source]

Handle a double-click event on the table.

is_editable(item_id, column_name)[source]

Return whether the given cell is editable.

doubleclick_cell(item_id, column_name)[source]

Handle a double-click on a cell, entering edit mode if editable.

focus_edit_cell(item_id, column_name)[source]

Place an entry widget over the cell for inline editing.

doubleclick_header(column_name)[source]

Handle a double-click on a column header.

TabularData

class mytk.tabulardata.PostponeChangeCalls(data_source)[source]

Bases: object

Context manager that batches data change notifications until exit.

class mytk.tabulardata.TabularData(tableview=None, delegate=None, required_fields=None)[source]

Bases: Bindable

A data model for tabular records with field validation and persistence.

exception MissingFieldError[source]

Bases: Exception

Raised when a required field is missing from a record.

exception ExtraFieldError[source]

Bases: Exception

Raised when a record contains a field not in the required fields.

exception UnrecognizedFileFormatError[source]

Bases: Exception

Raised when attempting to load a file with an unsupported format.

disable_change_calls()[source]

Suppress data change notifications to the delegate.

enable_change_calls()[source]

Re-enable data change notifications and trigger a full update.

get_field_properties(field_name)[source]

Return a copy of the properties dict for the given field.

get_field_property(field_name, property_name)[source]

Return a single property value for the given field.

update_field_properties(field_name, new_properties)[source]

Merge new properties into the existing properties for a field.

property record_count

Return the number of records.

default_namedtuple_type()[source]

Return a namedtuple type derived from the current record fields.

records_as_namedtuples(namedtuple_type=None)[source]

Return all records converted to namedtuples.

ordered_records()[source]

Return records ordered by parent-child hierarchy.

record_fields(internal=False)[source]

Return a sorted list of field names present across all records.

append_record(values)[source]

Append a new record to the end of the data source.

remove_record(index_or_uuid)[source]

Remove and return the record at the given index or with the given UUID.

remove_all_records()[source]

Remove all records from the data source.

empty_record()[source]

Return a new record with all required fields set to defaults.

new_record(values, pid=None)[source]

Create and return a normalized record without inserting it.

insert_child_records(index, records, pid)[source]

Insert multiple records as children of the given parent UUID.

insert_record(index, values, pid=None)[source]

Insert a record at the given index with an optional parent UUID.

update_record(index_or_uuid, values)[source]

Update an existing record identified by index or UUID with new values.

update_field(name, values)[source]

Update a field across all records with the given list of values.

record(index_or_uuid)[source]

Return the record at the given index or with the given UUID.

record_childs(index_or_uuid)[source]

Return a list of child records for the given parent record.

record_depth_level(uuid)[source]

Return the nesting depth of the record in the parent-child hierarchy.

field(name)[source]

Return a list of values for the given field across all records.

element(index_or_uuid, name)[source]

Return a single field value from the record at the given index or UUID.

remove_field(name)[source]

Remove the named field from all records.

rename_field(old_name, new_name)[source]

Rename a field across all records.

sorted_records_uuids(field, only_uuids=None, reverse=False)[source]

Return record UUIDs sorted by the given field.

source_records_changed(changed_records=None)[source]

Notify the delegate that records have changed.

load(filepath)[source]

Load records from a JSON file and insert them into the data source.

load_records_from_json(filepath)[source]

Read and return records from a JSON file.

save(filepath)[source]

Save all records to a JSON file, excluding internal fields.

save_records_to_json(records, filepath)[source]

Write records to a JSON file with indentation.

load_tabular_data(filepath)[source]

Load tabular data from a CSV or Excel file and return a DataFrame.

load_dataframe_from_tabular_data(filepath, header_row=None)[source]

Load a CSV or Excel file into a pandas DataFrame.

set_records_from_dataframe(df)[source]

Populate the data source from a pandas DataFrame.

FileViewer

class mytk.fileviewer.FileRecord(uuid, puuid, name, size, modification_date, fullpath, is_system_file, is_directory, is_directory_content_loaded, depth_level)

Bases: tuple

depth_level

Alias for field number 9

fullpath

Alias for field number 5

is_directory

Alias for field number 7

is_directory_content_loaded

Alias for field number 8

is_system_file

Alias for field number 6

modification_date

Alias for field number 4

name

Alias for field number 2

puuid

Alias for field number 1

size

Alias for field number 3

uuid

Alias for field number 0

class mytk.fileviewer.FileTreeData(root_dir, tableview, required_fields)[source]

Bases: TabularData

A TabularData subclass that reads file system directory contents.

is_system_file(filename)[source]

Return whether the filename matches a known system file pattern.

is_directory(fullpath)[source]

Return whether the path is a directory, treating macOS bundles as files.

recordid_with_fullpath(fullpath)[source]

Return the UUID of the record matching the given full path, or None.

insert_child_records_for_directory(root_dir, pid=None)[source]

Scan a directory and insert its entries as child records.

records_directory_content(root_dir)[source]

Return a list of file records for the contents of a directory.

class mytk.fileviewer.FileViewer(root_dir, columns_labels=None, custom_columns=None)[source]

Bases: TableView

A tree-style file browser widget built on TableView.

source_data_changed(records)[source]

Update the widget, optionally filtering out system files.

create_widget(master)[source]

Create the file viewer widget with default column layout.

selection_changed(event)[source]

Lazily load directory contents when a folder is selected.