Skip to main content

pct()

Helper for providing a CSS length value as a percentage.

Usage

Source

pct(x)

A percentage value acts as a length value that is relative to an initial state. For instance an 80% value for something will size the target to 80% the size of its ‘previous’ value. This type of sizing is useful for sizing up or down a length value with an intuitive measure. This helper function can be used for the setting of font sizes (e.g., in style.text()) and for widths (e.g., table_width= in tab_options()). Should a more exact definition of size be required, the analogous helper function px() will be more useful.

Parameters

x: float
The integer or float value to format as a string-based percentage value for some arguments that can take percentage values.

Returns

str
The value as a CSS percentage (e.g., "80%").

Examples

The pct() helper turns a number into a CSS percentage.

from great_tables import pct

pct(80)
'80%'

Here, pct() sets the table’s width to 60% of the width of its container, and the font size of the char column’s values to 80% of the size they’d otherwise have.

from great_tables import GT, exibble, loc, style

(
    GT(exibble[["num", "char"]].head(3))
    .tab_options(table_width=pct(60))
    .tab_style(style=style.text(size=pct(80)), locations=loc.body(columns="char"))
)
num char
0.1111 apricot
2.222 banana
33.33 coconut