Config#
Config options#
|
Use ASCII characters to display table outlines. |
|
Allow multi-output expressions to be automatically turned into Structs. |
|
Set the decimal separator character. |
|
Control the number of decimal places displayed for floating point values. |
|
Control how floating point values are displayed. |
Set the number of characters used to display string values. |
|
Set the number of elements to display for List values. |
|
Overwrite chunk size used in |
|
|
Set table cell alignment. |
Set table cell alignment for numeric columns. |
|
Set the number of columns that are visible when displaying tables. |
|
|
Display the data type next to the column name (to the right, in parentheses). |
|
Print the DataFrame shape information below the data when displaying tables. |
|
Set table formatting style. |
|
Hide table column data types (i64, f64, str etc.). |
|
Hide table column names. |
|
Hide the DataFrame shape information when displaying tables. |
|
Hide the '---' separator displayed between the column names and column types. |
Set the max number of rows used to draw the table (both Dataframe and Series). |
|
|
Set the maximum width of a table in characters. |
|
Set the thousands grouping separator character. |
|
Strip trailing zeros from Decimal data type values. |
|
Enable additional verbose/debug logging. |
Config load, save, state#
|
Load (and set) previously saved Config options from a JSON string. |
|
Load (and set) previously saved Config options from file. |
|
Save the current set of Config options as a JSON string. |
|
Save the current set of Config options as a JSON file. |
|
Show the current state of all Config variables in the environment as a dict. |
Reset all polars Config settings to their default state. |
While it is easy to restore all configuration options to their default
value using restore_defaults
, it can also be useful to reset individual
options. This can be done by setting the related value to None
, eg:
pl.Config.set_tbl_rows(None)
Use as a context manager#
Note that Config
supports setting context-scoped options. These options
are valid only during scope lifetime, and are reset to their initial values
(whatever they were before entering the new context) on scope exit.
You can take advantage of this by initialising a Config
instance and then
explicitly calling one or more of the available “set_” methods on it…
with pl.Config() as cfg:
cfg.set_verbose(True)
do_various_things()
# on scope exit any modified settings are restored to their previous state
…or, often cleaner, by setting the options in the Config
init directly
(optionally omitting the “set_” prefix for brevity):
with pl.Config(verbose=True):
do_various_things()
Use as a decorator#
In the same vein, you can also use a Config
instance as a function decorator
to temporarily set options for the duration of the function call:
cfg_ascii_frames = pl.Config(ascii_tables=True, apply_on_context_enter=True)
@cfg_ascii_frames
def write_markdown_frame_to_stdout(df: pl.DataFrame) -> None:
sys.stdout.write(str(df))
Multiple Config instances#
You may want to establish related bundles of Config
options for use in different
parts of your code. Usually options are set immediately on Config
init, meaning
the Config
instance cannot be reused; however, you can defer this so that options
are only invoked when entering context scope (which includes function entry if used
as a decorator)._
This allows you to create multiple reusable Config
instances in one place, update
and modify them centrally, and apply them as needed throughout your codebase.
cfg_verbose = pl.Config(verbose=True, apply_on_context_enter=True)
cfg_markdown = pl.Config(tbl_formatting="MARKDOWN", apply_on_context_enter=True)
@cfg_markdown
def write_markdown_frame_to_stdout(df: pl.DataFrame) -> None:
sys.stdout.write(str(df))
@cfg_verbose
def do_various_things():
...