generativepy.table module

By Martin McBride, 2023-04-27
Tags: formula tex latex
Categories: generativepy generative art


The table module can be used to draw tables.

A Table object will draw a simple table grid. It is possible to set the number of columns and the width of each column. The table can then be drawn using the specified fill and stroke styles.

The table content must be drawn separately. This makes it possible to draw any content in the table, formatted in any way.

The table get function can be used to obtain the coordinates of the centre of any table cell. THis allows user code to draw any required content. It is the responsibility of the user code to ensure that the content is correctly scaled and clipped as needed.

A companion class, TableLayout, can be used to obtains the metrics of the grid of a table, without actually drawing a table. This means that the TableLayout object can be created without the need for a drawing context. This is useful if you want to lay items out in a grid, but a drwaing context isn't available - for example, in the setup stages of creating a movie, or when working with a bitmap image mode.

Table class

Constructor

Table(ctx, position)
Parameter Type Description
ctx Drawing context
position (Number, Number) (x, y) posiyion of top left-hand corner of the Table

Creates a Table object, associated with the drawing context, at the specified position.

of_rows_cols

of_rows_cols(rows, cols)
Parameter Type Description
rows List of numbers List (or other sequence) of row widths
cols List of numbers List (or other sequence) of columns heights

Sets the size and number of rows and columns. Notice that the parameter are order rows then cols, rather than x, y ordering.

The rows list sets the width of each row, and the length of the rows list controls the number of rows.

The cols list sets the heights of each column, and the length of the cols list controls the number of columns.

Returns self.

get

get(row, col)
Parameter Type Description
row int row number
col int column number

Gets the position of the centre of a particular cell (row col). Notice that the parameter are order row then col, rather than x, y ordering.

Returns (x, y) position of centre of cell.

Table class

Constructor

Table(ctx, position)
Parameter Type Description
ctx Drawing context
position (Number, Number) (x, y) posiyion of top left-hand corner of the Table

Creates a Table object, associated with the drawing context, at the specified position.

of_rows_cols

of_rows_cols(rows, cols)
Parameter Type Description
rows List of numbers List (or other sequence) of row widths
cols List of numbers List (or other sequence) of columns heights

Sets the size and number of rows and columns. Notice that the parameter are order rows then cols, rather than x, y ordering.

The rows list sets the width of each row, and the length of the rows list controls the number of rows.

The cols list sets the heights of each column, and the length of the cols list controls the number of columns.

Returns self.

background

Sets the fill for the background of the table.

If this method in not called, the table will not have a background fill.

background(pattern=Color(0))
Parameter Type Description
pattern Color or Pattern The fill pattern/colour.

This method is similar to the stroke method of shape objects.

linestyle

Controls the outline and inner divisions of the table.

If this method isn't called, the outline and iner divisions will not be drawn,

linestyle(pattern=Color(0), line_width=1, dash=[], cap=SQUARE, join=MITER, miter_limit=None)
Parameter Type Description
pattern Color or Pattern The stroke pattern/colour.
line_width number The line width.
dash array of numbers The dash style.
cap enum The type of line cap.
join enum The type of line join.
miter_limit number The mitre limit.

This method is similar to the fill method of shape objects. There is no fill_rule for this parameter, because the table area is always a single rectangular area, so the fil rule would have no effect.

draw

draw()

Draws the table using the attributes set by the background and linestyle methods.

If neither of those methods has been called, the table will be invisible. In that case it is not necessary to call draw, as it will have no effect. It isn't an error to call draw in that case, it will just have no effect.

It is valid to have am invisible table, as it can still be used to position other elements on a grid. But if the table is not intended to be visible, consider using a TableLayout, which is effectively a table with no drawing capability. See below

get

get(row, col)
Parameter Type Description
row int row number
col int column number

Gets the position of the centre of a particular cell (row col). Notice that the parameter are order row then col, rather than x, y ordering.

Returns (x, y) position of centre of cell.

TableLayout

A TableLayout object creates a table that is not visible, but still provides a get method to allow other objects to be positioned on a grid.

Since a TableLayout cannot be drawn, its contructor doesn't require a drawing context. It is useful for cases where you might need an invisible table layout but a drawing context isn't available. For example, if you are creating a bitmap image.

Constructor

Table(position)
Parameter Type Description
position (Number, Number) (x, y) posiyion of top left-hand corner of the Table

Creates a TableLayout object at the specified position.

of_rows_cols

of_rows_cols(rows, cols)
Parameter Type Description
rows List of numbers List (or other sequence) of row widths
cols List of numbers List (or other sequence) of columns heights

Sets the size and number of rows and columns. Notice that the parameter are order rows then cols, rather than x, y ordering.

The rows list sets the width of each row, and the length of the rows list controls the number of rows.

The cols list sets the heights of each column, and the length of the cols list controls the number of columns.

Returns self.

get

get(row, col)
Parameter Type Description
row int row number
col int column number

Gets the position of the centre of a particular cell (row col). Notice that the parameter are order row then col, rather than x, y ordering.

Returns (x, y) position of centre of cell.

See also

If you found this article useful, you might be interested in the book NumPy Recipes or other books by the same author.

Join the PythonInformer Newsletter

Sign up using this form to receive an email when new content is added:

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest