Difference between revisions of "Data"
From Future Skill
(Created page with "This article goes through how to use the Data library to manage data and create tables in the canvas. == Overview == The library can currently only be used to make tables, b...") |
|||
Line 9: | Line 9: | ||
=== Tables === | === Tables === | ||
− | + | Tables show data as rows and columns. A number of leading/trailing rows/columns can be marked as headers/footers and will be handled slightly differently from normal rows/columns. There are various tools for formatting and styling cells based on position and/or contents. | |
+ | |||
+ | The following attributes are available for all tables and can be specified in the constructor: | ||
+ | ;<code>transpose</code> - <code>bool</code> | ||
+ | : Show rows as columns and vice versa, useful as rows and columns are not functionally identical | ||
+ | ;<code>default_data_formatter</code> - <code>Callable[[int, int, Any], str | BaseElement]</code> | ||
+ | : Used to format all non-header/footer cells that have not already been formatted | ||
+ | ;<code>column_data_formatters</code> - <code>dict[int, Callable[[int, int, Any], str | BaseElement]] | None</code> | ||
+ | : Used to format all non-header/footer cells of the indicated columns, other columns are formatted using the default formatter | ||
+ | ;<code>default_data_highlighter</code> - <code>Callable[[int, int, Any], CellStyle | None]</code> | ||
+ | : Used to apply highlight styles to all non-header/footer cells that have not already been highlighted | ||
+ | ;<code>column_data_highlighters</code> - <code>dict[int, Callable[[int, int, Any], CellStyle | None]] | None</code> | ||
+ | : Used to apply highlight styles to all non-header/footer cells of the indicated columns, other columns are styled using the default highlighter | ||
+ | ;<code>default_cell_style</code> - <code>CellStyle</code> | ||
+ | : Used as base style for non-header/footer cells | ||
+ | ;<code>default_header_cell_style</code> - <code>CellStyle</code> | ||
+ | : Used as base style for header/footer cells | ||
+ | ;<code>default_cell_highlighter</code> - <code>Callable[[int, int, AbstractTable], CellStyle]</code> | ||
+ | : Used to apply base styles for cells, can be used to add patterns to the table, default uses the the default styles | ||
+ | ;<code>cell_styles</code> - <code>dict[tuple[int, int], CellStyle] | None</code> | ||
+ | : Overrides the highlight styles for specific cells | ||
+ | ;<code>cell_padding</code> - <code>float</code> | ||
+ | : How much padding to add within each cell | ||
+ | ;<code>default_row_align</code> - <code>"top" | "bottom" | "center"</code> | ||
+ | : How to align the cell content of all rows unless otherwise specified | ||
+ | ;<code>default_column_align</code> - <code>"left" | "right" | "center"</code> | ||
+ | : How to align the cell content of all columns unless otherwise specified | ||
+ | ;<code>row_alignments</code> - <code>list["top" | "bottom" | "center" | None]</code> | ||
+ | : Set row alignment for specific rows | ||
+ | ;<code>column_alignments</code> - <code>list["left" | "right" | "center" | None]</code> | ||
+ | : Set column alignment for specific columns | ||
+ | ;<code>row_proportions</code> - <code>list[float]</code> | ||
+ | : Set row proportions, 0 means divide remaining evenly | ||
+ | ;<code>column_proportions</code> - <code>list[float]</code> | ||
+ | : Set column proportions, 0 means divide remaining evenly | ||
+ | ;<code>grid_line_size</code> - <code>float | None</code> | ||
+ | : The thickness of the grid lines, <code>None</code> disables the lines completely | ||
+ | |||
+ | The <code>AbstractTable</code> class is the base class for table data managers. | ||
==== DataTable ==== | ==== DataTable ==== | ||
Line 31: | Line 69: | ||
</div> | </div> | ||
The same indexing syntax can be used to extract data from the table. | The same indexing syntax can be used to extract data from the table. | ||
+ | |||
+ | The data table will automatically keep track of how many rows and columns the table has (based on the highest written row/column). | ||
+ | Currently there is no way to delete/reset cells, so these counts will not be lowered. | ||
You will have to manually specify the number of header/footer rows and columns using <code>header_row_count</code>, <code>header_column_count</code>, <code>footer_row_count</code>, and <code>footer_column_count</code>. | You will have to manually specify the number of header/footer rows and columns using <code>header_row_count</code>, <code>header_column_count</code>, <code>footer_row_count</code>, and <code>footer_column_count</code>. |
Revision as of 14:15, 19 March 2024
This article goes through how to use the Data library to manage data and create tables in the canvas.
Overview
The library can currently only be used to make tables, but other types of data views might be available in the future.
TODO
Tables
Tables show data as rows and columns. A number of leading/trailing rows/columns can be marked as headers/footers and will be handled slightly differently from normal rows/columns. There are various tools for formatting and styling cells based on position and/or contents.
The following attributes are available for all tables and can be specified in the constructor:
transpose
-bool
- Show rows as columns and vice versa, useful as rows and columns are not functionally identical
default_data_formatter
-Callable[[int, int, Any], str | BaseElement]
- Used to format all non-header/footer cells that have not already been formatted
column_data_formatters
-dict[int, Callable[[int, int, Any], str | BaseElement]] | None
- Used to format all non-header/footer cells of the indicated columns, other columns are formatted using the default formatter
default_data_highlighter
-Callable[[int, int, Any], CellStyle | None]
- Used to apply highlight styles to all non-header/footer cells that have not already been highlighted
column_data_highlighters
-dict[int, Callable[[int, int, Any], CellStyle | None]] | None
- Used to apply highlight styles to all non-header/footer cells of the indicated columns, other columns are styled using the default highlighter
default_cell_style
-CellStyle
- Used as base style for non-header/footer cells
default_header_cell_style
-CellStyle
- Used as base style for header/footer cells
default_cell_highlighter
-Callable[[int, int, AbstractTable], CellStyle]
- Used to apply base styles for cells, can be used to add patterns to the table, default uses the the default styles
cell_styles
-dict[tuple[int, int], CellStyle] | None
- Overrides the highlight styles for specific cells
cell_padding
-float
- How much padding to add within each cell
default_row_align
-"top" | "bottom" | "center"
- How to align the cell content of all rows unless otherwise specified
default_column_align
-"left" | "right" | "center"
- How to align the cell content of all columns unless otherwise specified
row_alignments
-list["top" | "bottom" | "center" | None]
- Set row alignment for specific rows
column_alignments
-list["left" | "right" | "center" | None]
- Set column alignment for specific columns
row_proportions
-list[float]
- Set row proportions, 0 means divide remaining evenly
column_proportions
-list[float]
- Set column proportions, 0 means divide remaining evenly
grid_line_size
-float | None
- The thickness of the grid lines,
None
disables the lines completely
The AbstractTable
class is the base class for table data managers.
DataTable
The DataTable
class is used to create tables by manually entering data.
This is done through indexing:
table = DataTable(header_row_count=1) table[1, 1] = 5 # Set value for a single table cell table[0, :] = ["Name", "Count"] # Set values for multiple cells (first row) table[1:, :] = [["Jane", 9], ["Joe", 7]] # Set values for multiple rows and columns # table will now contain: # |------|-------| # | Name | Count | # |------|-------| # | Jane | 9 | # |------|-------| # | Joe | 7 | # |------|-------|
The same indexing syntax can be used to extract data from the table.
The data table will automatically keep track of how many rows and columns the table has (based on the highest written row/column). Currently there is no way to delete/reset cells, so these counts will not be lowered.
You will have to manually specify the number of header/footer rows and columns using header_row_count
, header_column_count
, footer_row_count
, and footer_column_count
.