Create a gui with tksheet with and example table. it should show dataformatting in tksheet. Create an option in the gui to change datatypes and also save the data in a json file. tksheet handbook: Data Formatting
By default tksheet stores all user inputted data as strings and while tksheet can store and display any datatype with a str() method this has some obvious limitations.
Data formatting aims to provide greater functionality when working with different datatypes and provide strict typing for the sheet. With formatting you can convert sheet data and user input to a specific datatype.
Additionally, formatting also provides a function for displaying data on the table GUI (as a rounded float for example) and logic for handling invalid and missing data.
tksheet has several basic built-in formatters and provides functionality for creating your own custom formats as well.
A demonstration of all the built-in and custom formatters can be found here.
Creation and deletion of data formatting rules
Creating a data format rule
Span objects (more information here) can be used to format data for cells, rows, columns and the entire sheet.
You can use either of the following methods:
Using a span method e.g. span.format() more information here.
Using a sheet method e.g. sheet.format(Span)
Or if you need user inserted row/columns in the middle of areas with data formatting to also be formatted you can use named spans, more information here.
Whether data is formatted for cells, rows or columns depends on the kind of span.
When applying multiple overlapping formats with e.g. a formatted cell which overlaps a formatted row, the priority is as follows:
Cell formats first.
Row formats second.
Column formats third.
Data formatting will effectively override validate_input = True on cells with dropdown boxes.
When getting data take careful note of the get_displayed options, as these are the difference between getting the actual formatted data and what is simply displayed on the table GUI.
Parameters:
key (CreateSpanTypes) either a span or a type which can create a span. See here for more information on the types that can create a span.
formatter_options (dict) a dictionary of keyword options/arguements to pass to the formatter, see here for information on what argument to use.
formatter_class (class) in case you want to use a custom class to store functions and information as opposed to using the built-in methods.
**kwargs any additional keyword options/arguements to pass to the formatter.
Deleting a data format rule
If the data format rule was created by a named span then the named span must be deleted, more information here.
Assistant
Otherwise you can use either of the following methods to delete/remove data formatting rules:
Using a span method e.g. span.del_format() more information here.
Using a sheet method e.g. sheet.del_format(Span) details below:
key (CreateSpanTypes) either a span or a type which can create a span. See here for more information on the types that can create a span.
clear_values (bool) if true, all the cells covered by the span will have their values cleared.
clear_values (bool) if true, all the sheets cell values will be cleared.
Reapply formatting to entire sheet
reapply_formatting() -> Sheet
Useful if you have manually changed the entire sheets data using sheet.MT.data = and want to reformat the sheet using any existing formatting you have set.
Check if a cell is formatted
formatted(r: int, c: int) -> dict
If the cell is formatted function returns a dict with all the format keyword arguments. The dict will be empty if the cell is not formatted.
Formatters
In addition to the generic formatter, tksheet provides formatters for many different data types.
A basic example showing how formatting some columns as float might be done:
import tkinter as tk
from tksheet import (
Sheet,
float_formatter,
)
from tksheet import (
num2alpha,
)
class demo(tk.Tk):
def init(self):
super().init()
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
self.sheet = Sheet(
self,
data=[[f"{r}", f"{r}"] for r in range(5)],
expand_sheet_if_paste_too_big=True,
theme="dark blue",
)
"""
Format example
"""
# some keyword arguments inside float_formatter()
self.sheet.format(
num2alpha(0), # column A
formatter_options=float_formatter(
decimals=1,
nullable=True,
),
)
# some keyword arguments outside
# of float_formatter() instead
self.sheet.format(
"B", # column B
formatter_options=float_formatter(),
decimals=3,
nullable=False,
)
"""
Rest of code
"""
self.sheet.grid(row=0, column=0, sticky="nswe")
self.sheet.enable_bindings(
"all",
"ctrl_select",
"edit_header",
"edit_index",
)
app = demo()
app.mainloop()
You can use any of the following formatters as an argument for the parameter formatter_options.
A full list of keyword arguments available to these formatters is shown here.
Int Formatter
The int_formatter is the basic configuration for a simple interger formatter.
format_function (function) a function that takes a string and returns an int. By default, this is set to the in-built tksheet.to_int. This function will always convert float-likes to its floor, for example "5.9" will be converted to 5.
to_str_function (function) By default, this is set to the in-built tksheet.to_str, which is a very basic function that will displace the default string representation of the value.
The float_formatter is the basic configuration for a simple float formatter. It will always round float-likes to the specified number of decimal places, for example "5.999" will be converted to "6.0" if decimals = 1.
format_function (function) a function that takes a string and returns a float. By default, this is set to the in-built tksheet.to_float.
to_str_function (function) By default, this is set to the in-built tksheet.float_to_str, which will display the float to the specified number of decimal places.
decimals (int, None) the number of decimal places to round to. Defaults to 2.
Example:
sheet.format_cell(0, 0, formatter_options = tksheet.float_formatter(decimals = None)) # A float formatter with maximum float() decimal places
Percentage Formatter
The percentage_formatter is the basic configuration for a simple percentage formatter. It will always round float-likes as a percentage to the specified number of decimal places, for example "5.999%" will be converted to "6.0%" if decimals = 1.
format_function (function) a function that takes a string and returns a float. By default, this is set to the in-built tksheet.to_percentage. This function will always convert percentages to their decimal equivalent, for example "5%" will be converted to 0.05.
to_str_function (function) By default, this is set to the in-built tksheet.percentage_to_str, which will display the float as a percentage to the specified number of decimal places. For example, 0.05 will be displayed as "5.0%".
decimals (int) the number of decimal places to round to. Defaults to 0.
Example:
sheet.format_cell(0, 0, formatter_options=tksheet.percentage_formatter(decimals=1)) # A percentage formatter with 1 decimal place
Note:
By default percentage_formatter() converts user entry 21 to 2100% and 21% to 21%. An example where 21 is converted to 21% instead is shown below:
format_function (function) a function that takes a string and returns a bool. By default, this is set to the in-built tksheet.to_bool.
to_str_function (function) By default, this is set to the in-built tksheet.bool_to_str, which will display the boolean as "True" or "False".
truthy (set) a set of values that will be converted to True. Defaults to the in-built tksheet.truthy.
falsy (set) a set of values that will be converted to False. Defaults to the in-built tksheet.falsy.
Example:
A bool formatter with custom truthy and falsy values to account for aussie and kiwi slang
This is the generic formatter options interface. You can use this to create your own custom formatters. The following options are available.
Note that all these options can also be passed to the Sheet() format functions as keyword arguments and are available as attributes for all formatters.
You can also provide functions of your own creation for all the below arguments which take functions if you require.
datatypes (list) a list of datatypes that the formatter will accept. For example, datatypes = [int, float] will accept integers and floats.
format_function (function) a function that takes a string and returns a value of the desired datatype. For example, format_function = int will convert a string to an integer.
to_str_function (function) a function that takes a value of the desired datatype and returns a string. This determines how the formatter displays its data on the table. For example, to_str_function = str will convert an integer to a string. Defaults to tksheet.to_str.
invalid_value (any) the value to return if the input string is invalid. For example, invalid_value = "NA" will return "NA" if the input string is invalid.
nullable (bool) if true, the formatter will accept None as a valid input.
pre_format_function (function) a function that takes a input string and returns a string. This function is called before the format_function and can be used to modify the input string before it is converted to the desired datatype. This can be useful if you want to strip out unwanted characters or convert a string to a different format before converting it to the desired datatype.
post_format_function (function) a function that takes a value which might not be of the desired datatype, e.g. None if the cell is nullable and empty and if successful returns a value of the desired datatype or if not successful returns the input value. This function is called after the format_function and can be used to modify the output value after it is converted to the desired datatype. This can be useful if you want to round a float for example.
clipboard_function (function) a function that takes a value of the desired datatype and returns a string. This function is called when the cell value is copied to the clipboard. This can be useful if you want to convert a value to a different format before it is copied to the clipboard.
**kwargs any additional keyword options/arguements to pass to the formatter. These keyword arguments will be passed to the format_function, to_str_function, and the clipboard_function. These can be useful if you want to specifiy any additional formatting options, such as the number of decimal places to round to.
Datetime Formatters and Designing Your Own Custom Formatters
tksheet is at the moment a dependency free library and so doesn't include a datetime parser as is.
You can however very easily make a datetime parser if you are willing to install a third-party package. Recommended are:
dateparser
dateutil
Both of these packages have a very comprehensive datetime parser which can be used to create a custom datetime formatter for tksheet.
Below is a simple example of how you might create a custom datetime formatter using the dateutil package.
from tksheet import *
from datetime import datetime, date
from dateutil.parser import parse
def to_local_datetime(dt, **kwargs):
'''
Our custom format_function, converts a string or a date to a datetime object in the local timezone.
'''
if isinstance(dt, datetime):
pass # Do nothing
elif isinstance(dt, date):
dt = datetime(dt.year, dt.month, dt.day) # Always good to account for unexpected inputs
else:
try:
dt = parser.parse(dt)
except:
raise ValueError(f"Could not parse {dt} as a datetime")
if dt.tzinfo is None:
dt = dt.replace(tzinfo = tzlocal()) # If no timezone is specified, assume local timezone
dt = dt.astimezone(tzlocal()) # Convert to local timezone
return dt
def datetime_to_str(dt, **kwargs):
'''
Our custom to_str_function, converts a datetime object to a string with a format that can be specfied in kwargs.
'''
return dt.strftime(kwargs['format'])
Now we can create our custom formatter dictionary from the generic formatter interface in tksheet
From here we can pass our datetime_formatter into sheet.format() or span.format() just like any other formatter
For those wanting even more customisation of their formatters you also have the option of creating a custom formatter class. This is a more advanced topic and is not covered here, but it's recommended to create a new class which is a subclass of the tksheet.Formatter class and overriding the methods you would like to customise. This custom class can then be passed into the format_cells() formatter_class argument.
Here is an example of a GUI application using tksheet that demonstrates data formatting. This application allows you to change the data types of columns and save the data to a JSON file.
import tkinter as tk
from tkinter import messagebox
from tksheet import Sheet, int_formatter, float_formatter, bool_formatter, formatter
import json
import datetime
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("tksheet Data Formatting Example")
self.geometry("800x600")
# Example data
self.sheet_data = [
['1', '2.34', 'True', "2023-10-21"],
['5', '6.78', 'False', "2023-10-22"],
['9', '10.11', 'True', "2023-10-23"],
]
self.create_widgets()
def create_widgets(self):
# Create the sheet
self.sheet = Sheet(
self,
data=self.sheet_data,
headers=['Integer', 'Float', 'Boolean', 'Date'],
expand_sheet_if_paste_too_big=True,
theme='dark blue',
)
self.sheet.enable_bindings(
"all",
"ctrl_select",
"edit_header",
"edit_index",
)
self.sheet.grid(row=0, column=0, sticky='nsew')
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
# Apply data formatting
self.apply_initial_formatting()
# Create options to change data types
self.create_options_panel()
def apply_initial_formatting(self):
# Apply initial data formatting to columns
self.sheet.format('Integer', formatter_options=int_formatter())
self.sheet.format('Float', formatter_options=float_formatter())
self.sheet.format('Boolean', formatter_options=bool_formatter())
# Create and apply date formatter
def to_date(s, **kwargs):
try:
return datetime.datetime.strptime(s, '%Y-%m-%d').date()
except ValueError:
raise ValueError(f"Invalid date format: {s}")
def date_to_str(d, **kwargs):
if isinstance(d, datetime.date):
return d.strftime('%Y-%m-%d')
else:
return str(d)
date_formatter = formatter(
datatypes=datetime.date,
format_function=to_date,
to_str_function=date_to_str,
invalid_value='Invalid Date',
)
self.sheet.format('Date', formatter_options=date_formatter)
def create_options_panel(self):
options_frame = tk.Frame(self)
options_frame.grid(row=1, column=0, sticky='ew')
tk.Label(options_frame, text="Change Data Type:").pack(side=tk.LEFT, padx=5)
self.column_var = tk.StringVar(value='Integer')
self.datatype_var = tk.StringVar(value='Int')
columns = self.sheet.headers()
tk.OptionMenu(options_frame, self.column_var, *columns).pack(side=tk.LEFT, padx=5)
data_types = ['Int', 'Float', 'Bool', 'Date', 'String']
tk.OptionMenu(options_frame, self.datatype_var, *data_types).pack(side=tk.LEFT, padx=5)
tk.Button(options_frame, text="Apply", command=self.change_datatype).pack(side=tk.LEFT, padx=5)
# Save button
tk.Button(options_frame, text="Save to JSON", command=self.save_to_json).pack(side=tk.RIGHT, padx=5)
def change_datatype(self):
column_name = self.column_var.get()
datatype = self.datatype_var.get()
# Remove existing formatting
self.sheet.del_format(column_name)
if datatype == 'Int':
self.sheet.format(column_name, formatter_options=int_formatter())
elif datatype == 'Float':
self.sheet.format(column_name, formatter_options=float_formatter())
elif datatype == 'Bool':
self.sheet.format(column_name, formatter_options=bool_formatter())
elif datatype == 'Date':
# Reuse the date formatter defined earlier
def to_date(s, **kwargs):
try:
return datetime.datetime.strptime(s, '%Y-%m-%d').date()
except ValueError:
raise ValueError(f"Invalid date format: {s}")
def date_to_str(d, **kwargs):
if isinstance(d, datetime.date):
return d.strftime('%Y-%m-%d')
else:
return str(d)
date_formatter = formatter(
datatypes=datetime.date,
format_function=to_date,
to_str_function=date_to_str,
invalid_value='Invalid Date',
)
self.sheet.format(column_name, formatter_options=date_formatter)
else:
# If 'String' is selected, no formatter is applied
pass
def save_to_json(self):
# Get data including headers
data = self.sheet.get_sheet_data(return_copy=True, get_header=True, get_index=False)
# Convert data to a list of dictionaries
headers = data.pop(0) # Remove headers from data
data_dicts = [dict(zip(headers, row)) for row in data]
filename = 'sheet_data.json'
with open(filename, 'w') as f:
json.dump(data_dicts, f, indent=4, default=str) # Use default=str to serialize dates
messagebox.showinfo("Save to JSON", f"Data saved to {filename}")
if __name__ == '__main__':
app = App()
app.mainloop()
Explanation:
Data Formatting:
The apply_initial_formatting method applies different data formatters to each column using the sheet.format method.
Integer, Float, and Boolean columns use the built-in formatters int_formatter, float_formatter, and bool_formatter.
The Date column uses a custom date formatter since tksheet does not include a date formatter by default.
Changing Data Types:
The create_options_panel method creates a panel with dropdown menus to select a column and a data type.
When the "Apply" button is clicked, the change_datatype method is called.
This method removes any existing formatting from the selected column and applies the new formatter based on the chosen data type.
Saving to JSON:
The save_to_json method retrieves the data from the sheet, including headers.
It converts the data into a list of dictionaries, where each dictionary represents a row with column headers as keys.
The data is saved to a JSON file named sheet_data.json.
The default=str argument in json.dump is used to handle serialization of data types like dates.
Notes:
Custom Date Formatter:
Since tksheet does not have a built-in date formatter, we define to_date and date_to_str functions.
to_date converts a string to a datetime.date object using strptime.
date_to_str converts a datetime.date object back to a string.
Dynamic Data Type Change:
Changing the data type of a column is as simple as applying a different formatter.
The del_format method is used to remove any existing formatter before applying a new one.
Error Handling:
The custom to_date function raises a ValueError if the date string is invalid.
This will prompt tksheet to display Invalid Date in the cell if the input is not a valid date.
Dependencies:
This script requires the tksheet module. Install it via pip install tksheet.
Screenshot of the GUI:
To visualize the application, here's how the GUI might look:
Save the script to a file, for example, tksheet_example.py, and run it using:
python tksheet_example.py
You can edit cells, change data types, and save the data to a JSON file. This example demonstrates how to use data formatting in tksheet and provides a foundation for more complex applications.