|
| 1 | +import pli from "./internals/polars_internal"; |
| 2 | + |
1 | 3 | /**
|
2 | 4 | * Configure polars; offers options for table formatting and more.
|
3 | 5 | */
|
4 | 6 | export interface Config {
|
5 |
| - /** Use utf8 characters to print tables */ |
6 |
| - setUtf8Tables(): Config; |
7 |
| - /** Use ascii characters to print tables */ |
| 7 | + /** Use ASCII characters to display table outlines. |
| 8 | + * @param active - Set False to revert to the standard UTF8_FULL_CONDENSED formatting style. Default: true |
| 9 | + * |
| 10 | + * @example |
| 11 | + * const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]}) |
| 12 | + pl.Config.setAsciiTables(true) |
| 13 | + |
| 14 | + # shape: (3, 2) shape: (3, 2) |
| 15 | + # ┌─────┬───────┐ +-----+-------+ |
| 16 | + # │ abc ┆ xyz │ | abc | xyz | |
| 17 | + # │ --- ┆ --- │ | --- | --- | |
| 18 | + # │ f64 ┆ bool │ | f64 | bool | |
| 19 | + # ╞═════╪═══════╡ +=============+ |
| 20 | + # │ 1.0 ┆ true │ >> | 1.0 | true | |
| 21 | + # │ 2.5 ┆ false │ | 2.5 | false | |
| 22 | + # │ 5.0 ┆ true │ | 5.0 | true | |
| 23 | + # └─────┴───────┘ +-----+-------+ |
| 24 | + */ |
8 | 25 | setAsciiTables(): Config;
|
9 |
| - /** Set the number of character used to draw the table */ |
| 26 | + setAsciiTables(active: boolean): Config; |
| 27 | + /** Set the maximum width of a table in characters. |
| 28 | + * @param width - Maximum table width in characters; if n < 0 (eg: -1), display full width. |
| 29 | + * |
| 30 | + * @example |
| 31 | + * const df = pl.DataFrame({ id: ["SEQ1", "SEQ2"], seq: ["ATGATAAAGGAG", "GCAACGCATATA"] }); |
| 32 | + >>> df |
| 33 | + shape: (2, 2) |
| 34 | + ┌──────┬──────────────┐ |
| 35 | + │ id ┆ seq │ |
| 36 | + │ --- ┆ --- │ |
| 37 | + │ str ┆ str │ |
| 38 | + ╞══════╪══════════════╡ |
| 39 | + │ SEQ1 ┆ ATGATAAAGGAG │ |
| 40 | + │ SEQ2 ┆ GCAACGCATATA │ |
| 41 | + └──────┴──────────────┘ |
| 42 | + >>> pl.Config.setTblWidthChars(12); |
| 43 | + >>> df |
| 44 | + shape: (2, 2) |
| 45 | + ┌─────┬─────┐ |
| 46 | + │ id ┆ seq │ |
| 47 | + │ --- ┆ --- │ |
| 48 | + │ str ┆ str │ |
| 49 | + ╞═════╪═════╡ |
| 50 | + │ SEQ ┆ ATG │ |
| 51 | + │ 1 ┆ ATA │ |
| 52 | + │ ┆ AAG │ |
| 53 | + │ ┆ GAG │ |
| 54 | + │ SEQ ┆ GCA │ |
| 55 | + │ 2 ┆ ACG │ |
| 56 | + │ ┆ CAT │ |
| 57 | + │ ┆ ATA │ |
| 58 | + └─────┴─────┘ |
| 59 | + */ |
| 60 | + setTblWidthChars(): Config; |
10 | 61 | setTblWidthChars(width: number): Config;
|
11 |
| - /** Set the number of rows used to print tables */ |
| 62 | + /** Set the max number of rows used to draw the table (both Dataframe and Series). |
| 63 | + * @param n - Number of rows to display; if `n < 0` (eg: -1), display all rows (DataFrame) and all elements (Series). |
| 64 | + * |
| 65 | + * @example |
| 66 | + const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]}) |
| 67 | + pl.Config.setTblRows(2); |
| 68 | + |
| 69 | + shape: (4, 2) |
| 70 | + ┌─────┬───────┐ |
| 71 | + │ abc ┆ xyz │ |
| 72 | + │ --- ┆ --- │ |
| 73 | + │ f64 ┆ bool │ |
| 74 | + ╞═════╪═══════╡ |
| 75 | + │ 1.0 ┆ true │ |
| 76 | + │ … ┆ … │ |
| 77 | + │ 5.0 ┆ false │ |
| 78 | + └─────┴───────┘ |
| 79 | + */ |
| 80 | + setTblRows(): Config; |
12 | 81 | setTblRows(n: number): Config;
|
13 |
| - /** Set the number of columns used to print tables */ |
14 |
| - setTblCols(n: number): Config; |
| 82 | + /** Set the number of columns that are visible when displaying tables. |
| 83 | + * @param n - Number of columns to display; if `n < 0` (eg: -1), display all columns. |
| 84 | + * |
| 85 | + * @example |
| 86 | +
|
| 87 | + const df = pl.DataFrame( {abc: [1.0, 2.5, 3.5, 5.0], def: ["d", "e", "f", "g"], xyz: [true, false, true, false] } ); |
| 88 | + // Set number of displayed columns to a low value |
| 89 | + pl.Config.setTblCols(2); |
| 90 | + |
| 91 | + shape: (4, 3) |
| 92 | + ┌─────┬───┬───────┐ |
| 93 | + │ abc ┆ … ┆ xyz │ |
| 94 | + │ --- ┆ ┆ --- │ |
| 95 | + │ f64 ┆ ┆ bool │ |
| 96 | + ╞═════╪═══╪═══════╡ |
| 97 | + │ 1.0 ┆ … ┆ true │ |
| 98 | + │ 2.5 ┆ … ┆ false │ |
| 99 | + │ 3.5 ┆ … ┆ true │ |
| 100 | + │ 5.0 ┆ … ┆ false │ |
| 101 | + └─────┴───┴───────┘ |
| 102 | + */ |
| 103 | + setTblCols(n?: number): Config; |
| 104 | + |
| 105 | + /** |
| 106 | + * Display the data type next to the column name (to the right, in parentheses). |
| 107 | + * @param active - true / false Default - true |
| 108 | + * |
| 109 | + * @example |
| 110 | + -------- |
| 111 | + const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]}) |
| 112 | + pl.Config.setTblColumnDataTypeInline(true) |
| 113 | +
|
| 114 | + # shape: (3, 2) shape: (3, 2) |
| 115 | + # ┌─────┬───────┐ ┌───────────┬────────────┐ |
| 116 | + # │ abc ┆ xyz │ │ abc (f64) ┆ xyz (bool) │ |
| 117 | + # │ --- ┆ --- │ ╞═══════════╪════════════╡ |
| 118 | + # │ f64 ┆ bool │ │ 1.0 ┆ true │ |
| 119 | + # ╞═════╪═══════╡ >> │ 2.5 ┆ false │ |
| 120 | + # │ 1.0 ┆ true │ │ 5.0 ┆ true │ |
| 121 | + # │ 2.5 ┆ false │ └───────────┴────────────┘ |
| 122 | + # │ 5.0 ┆ true │ |
| 123 | + # └─────┴───────┘ |
| 124 | + */ |
| 125 | + |
| 126 | + setTblColumnDataTypeInline(active?: boolean): Config; |
| 127 | + |
| 128 | + /** |
| 129 | + * Hide table column data types (i64, f64, str etc.). |
| 130 | + * @param active - true / false Default - true |
| 131 | +
|
| 132 | + * @example |
| 133 | + const df = pl.DataFrame({abc: [1.0, 2.5, 5.0], xyz: [true, false, true]}) |
| 134 | + pl.Config.setTblHideColumnDataTypes(true) |
| 135 | + |
| 136 | + # shape: (3, 2) shape: (3, 2) |
| 137 | + # ┌─────┬───────┐ ┌─────┬───────┐ |
| 138 | + # │ abc ┆ xyz │ │ abc ┆ xyz │ |
| 139 | + # │ --- ┆ --- │ ╞═════╪═══════╡ |
| 140 | + # │ f64 ┆ bool │ │ 1.0 ┆ true │ |
| 141 | + # ╞═════╪═══════╡ >> │ 2.5 ┆ false │ |
| 142 | + # │ 1.0 ┆ true │ │ 5.0 ┆ true │ |
| 143 | + # │ 2.5 ┆ false │ └─────┴───────┘ |
| 144 | + # │ 5.0 ┆ true │ |
| 145 | + # └─────┴───────┘ |
| 146 | + */ |
| 147 | + setTblHideColumnDataTypes(active?: boolean): Config; |
| 148 | + |
| 149 | + /** |
| 150 | + * Enable additional verbose/debug logging. |
| 151 | + * @param active - true / false Default - true |
| 152 | + */ |
| 153 | + setVerbose(active?: boolean): Config; |
| 154 | + |
| 155 | + /** |
| 156 | + * Set the thousands grouping separator character. |
| 157 | + * @param separator : string | bool |
| 158 | + Set True to use the default "," (thousands) and "." (decimal) separators. |
| 159 | + Can also set a custom char, or set ``None`` to omit the separator. |
| 160 | + */ |
| 161 | + setThousandsSeparator(separator?: string | boolean): Config; |
| 162 | + // TODO: Implement these methods |
| 163 | + // set_auto_structify |
| 164 | + // set_decimal_separator |
| 165 | + // set_engine_affinity |
| 166 | + // set_float_precision |
| 167 | + // set_fmt_float |
| 168 | + // set_fmt_str_lengths |
| 169 | + // set_fmt_table_cell_list_len |
| 170 | + // set_streaming_chunk_size |
| 171 | + // set_tbl_cell_alignment |
| 172 | + // set_tbl_cell_numeric_alignment |
| 173 | + // set_tbl_dataframe_shape_below |
| 174 | + // set_tbl_formatting |
| 175 | + // set_tbl_hide_column_names |
| 176 | + // set_tbl_hide_dataframe_shape |
| 177 | + // set_tbl_hide_dtype_separator |
| 178 | + // set_trim_decimal_zeros |
15 | 179 | }
|
16 | 180 |
|
17 |
| -/** |
18 |
| - * @ignore |
19 |
| - */ |
20 | 181 | export const Config: Config = {
|
21 |
| - setUtf8Tables() { |
22 |
| - process.env["POLARS_FMT_NO_UTF8"] = undefined; |
| 182 | + setAsciiTables(active?: boolean) { |
| 183 | + pli.setAsciiTables(active); |
| 184 | + return this; |
| 185 | + }, |
| 186 | + setTblWidthChars(width?: number) { |
| 187 | + pli.setTblWidthChars(width); |
| 188 | + return this; |
| 189 | + }, |
| 190 | + setTblRows(n?: number) { |
| 191 | + pli.setTblRows(n); |
| 192 | + return this; |
| 193 | + }, |
| 194 | + setTblCols(n?: number) { |
| 195 | + pli.setTblCols(n); |
23 | 196 | return this;
|
24 | 197 | },
|
25 |
| - setAsciiTables() { |
26 |
| - process.env["POLARS_FMT_NO_UTF8"] = "1"; |
| 198 | + setTblColumnDataTypeInline(active?: boolean) { |
| 199 | + pli.setTblColumnDataTypeInline(active ? 1 : 0); |
27 | 200 | return this;
|
28 | 201 | },
|
29 |
| - setTblWidthChars(width) { |
30 |
| - process.env["POLARS_TABLE_WIDTH"] = String(width); |
| 202 | + setTblHideColumnDataTypes(active?: boolean) { |
| 203 | + pli.setTblHideColumnDataTypes(active ? 1 : 0); |
31 | 204 | return this;
|
32 | 205 | },
|
33 |
| - setTblRows(n) { |
34 |
| - process.env["POLARS_FMT_MAX_ROWS"] = String(n); |
| 206 | + setVerbose(active?: boolean) { |
| 207 | + pli.setVerbose(active ? 1 : 0); |
35 | 208 | return this;
|
36 | 209 | },
|
37 |
| - setTblCols(n) { |
38 |
| - process.env["POLARS_FMT_MAX_COLS"] = String(n); |
| 210 | + setThousandsSeparator(separator?: string | boolean) { |
| 211 | + if (typeof separator === "boolean" && separator) { |
| 212 | + pli.setDecimalSeparator("."); |
| 213 | + pli.setThousandsSeparator(","); |
| 214 | + } else if (typeof separator === "string") { |
| 215 | + if (separator.length > 1) |
| 216 | + throw new TypeError("separator must be a single character;"); |
| 217 | + pli.setThousandsSeparator(separator); |
| 218 | + } else { |
| 219 | + pli.setThousandsSeparator(); |
| 220 | + } |
39 | 221 | return this;
|
40 | 222 | },
|
41 | 223 | };
|
0 commit comments