Skip to content

Commit 1af6e40

Browse files
committed
Add initial docs for output customization
1 parent 9a5854f commit 1af6e40

File tree

1 file changed

+203
-9
lines changed

1 file changed

+203
-9
lines changed

docs/src/man/getting_started.md

Lines changed: 203 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ relevant variables into your current namespace.
2727
file. See [here](https://jupyter-client.readthedocs.io/en/stable/kernels.html) for information about location
2828
and specification of Jupyter kernels.
2929

30-
The package [PrettyTables.jl](https://github.com/ronisbr/PrettyTables.jl) renders the `DataFrame` in the
31-
Jupyter notebook. Users can customize the output by passing keywords arguments `kwargs...` to the
32-
function `show`: `show(stdout, MIME("text/html"), df; kwargs...)`, where `df` is the `DataFrame`. Any
33-
argument supported by PrettyTables.jl in the HTML backend can be used here. Hence, for example, if the user
34-
wants to change the color of all numbers smaller than 0 to red in Jupyter, they can execute:
35-
`show(stdout, MIME("text/html"), df; highlighters = hl_lt(0, HtmlDecoration(color = "red")))` after
36-
`using PrettyTables`. For more information about the available options, check
37-
[PrettyTables.jl documentation](https://ronisbr.github.io/PrettyTables.jl/stable/man/usage/).
38-
3930
## The `DataFrame` Type
4031

4132
Objects of the `DataFrame` type represent a data table as a series of vectors,
@@ -464,3 +455,206 @@ julia> Tables.rowtable(df)
464455
(a = 1, b = 2)
465456
(a = 3, b = 4)
466457
```
458+
459+
## Customizing Display Output
460+
461+
DataFrames.jl uses [PrettyTables.jl](https://github.com/ronisbr/PrettyTables.jl) to render
462+
tables in both plain text (`show(df; ...)`) and HTML
463+
(`show(stdout, MIME("text/html"), df; ...)` in notebook-like environments).
464+
465+
The `show` function exposes DataFrames-specific controls and also forwards `kwargs...` to
466+
PrettyTables.jl, so you can customize formatting, styling, and highlights.
467+
468+
DataFrames-specific keywords accepted by `show` are:
469+
470+
- `allrows::Bool` (text): print all rows instead of only the rows that fit the display
471+
height.
472+
- `allcols::Bool` (text): print all columns instead of only the columns that fit the display
473+
width.
474+
- `rowlabel::Symbol` (text): set the label used for the row-number column (default: `:Row`).
475+
- `summary::Bool` (text and HTML): show or hide the summary line above the table (for
476+
example, `3×3 DataFrame`).
477+
- `eltypes::Bool` (text and HTML): show or hide the column element types under the column
478+
names.
479+
- `truncate::Int` (text): maximum display width for each data column before truncation with
480+
``; `0` or negative disables truncation.
481+
- `show_row_number::Bool` (text and HTML): show or hide row numbers.
482+
- `max_column_width::AbstractString` (HTML): maximum column width as a CSS length (for
483+
example, `"120px"`); empty string means no width limit.
484+
485+
The remaining `kwargs...` are forwarded to PrettyTables.jl for backend-specific
486+
customization.
487+
488+
For HTML output, DataFrames.jl reserves `rowid`, `title`, and `truncate` keywords. Use
489+
`max_column_width` (instead of `truncate`) to control cell width in HTML rendering, and
490+
`top_left_str` (instead of `title`) to set the table title in HTML rendering.
491+
492+
```julia
493+
julia> using DataFrames
494+
495+
julia> df = DataFrame(
496+
a = [1, 2, 3],
497+
b = [3.14, -1.2, 42.0],
498+
c = ["short", "a very very very very very long string", "ok"]
499+
);
500+
501+
# This is the default output.
502+
julia> df
503+
3×3 DataFrame
504+
Row │ a b c
505+
│ Int64 Float64 String
506+
─────┼───────────────────────────────────────────────────
507+
11 3.14 short
508+
22 -1.2 a very very very very very long
509+
33 42.0 ok
510+
511+
# Using this option, no cell will be truncated if there is room to display it.
512+
julia> show(df; truncate = 0)
513+
3×3 DataFrame
514+
Row │ a b c
515+
│ Int64 Float64 String
516+
─────┼────────────────────────────────────────────────────────
517+
11 3.14 short
518+
22 -1.2 a very very very very very long string
519+
33 42.0 ok
520+
521+
# Hide row numbers and rename the row label column in text output.
522+
julia> show(df; show_row_number = false)
523+
3×3 DataFrame
524+
a b c
525+
Int64 Float64 String
526+
───────────────────────────────────────────────────
527+
1 3.14 short
528+
2 -1.2 a very very very very very long
529+
3 42.0 ok
530+
531+
# Hide the column element types in text output.
532+
julia> show(df; eltypes = false)
533+
3×3 DataFrame
534+
Row │ a b c
535+
─────┼─────────────────────────────────────────────
536+
11 3.14 short
537+
22 -1.2 a very very very very very long
538+
33 42.0 ok
539+
```
540+
541+
!!! note
542+
543+
The following examples assume that PrettyTables.jl v3.0 or later is installed. If you
544+
have an older version of PrettyTables.jl, you may need to update it to use the features
545+
shown in the examples below.
546+
547+
We can use formatters in PrettyTables.jl to change how cells are converted to strings. The
548+
following example shows how to replace negative values with parentheses in text output.
549+
550+
```julia
551+
julia> using PrettyTables
552+
553+
julia> df = DataFrame(
554+
A = [ 0.73, -1.28, 1.91, -0.44, 0.12, -2.35, 1.08],
555+
B = [-0.55, 0.67, -1.49, 2.11, -0.03, 0.94, -2.20],
556+
C = [ 1.34, -0.88, 0.45, -1.76, 2.53, -0.61, 0.07],
557+
D = [-1.02, 2.40, -0.31, 0.58, -2.14, 1.77, -0.90],
558+
E = [ 0.26, -1.67, 2.22, -0.75, 1.05, -0.48, -2.93]
559+
);
560+
561+
# This is the default output.
562+
julia> df
563+
7×5 DataFrame
564+
Row │ A B C D E
565+
│ Float64 Float64 Float64 Float64 Float64
566+
─────┼──────────────────────────────────────────────
567+
10.73 -0.55 1.34 -1.02 0.26
568+
2-1.28 0.67 -0.88 2.4 -1.67
569+
31.91 -1.49 0.45 -0.31 2.22
570+
4-0.44 2.11 -1.76 0.58 -0.75
571+
50.12 -0.03 2.53 -2.14 1.05
572+
6-2.35 0.94 -0.61 1.77 -0.48
573+
71.08 -2.2 0.07 -0.9 -2.93
574+
575+
# We can replace the negative values in text back-end with parentheses using formatters.
576+
# This function is called for each cell in the table. `v` is the current cell value, `i` and
577+
# `j` are the row and column indices of the cell. It must return the new object which will
578+
# be printed in the cell. In this case, we only want to change cells that are negative
579+
# numbers, so we return the original value for all other cells.
580+
julia> function parentheses_fmt(v, i, j)
581+
!(v isa Number) && return v
582+
v < 0 && return "($(-v))"
583+
return v
584+
end
585+
586+
julia> show(df; formatters = [parentheses_fmt])
587+
7×5 DataFrame
588+
Row │ A B C D E
589+
│ Float64 Float64 Float64 Float64 Float64
590+
─────┼─────────────────────────────────────────────
591+
10.73 (0.55) 1.34 (1.02) 0.26
592+
2 │ (1.28) 0.67 (0.88) 2.4 (1.67)
593+
31.91 (1.49) 0.45 (0.31) 2.22
594+
4 │ (0.44) 2.11 (1.76) 0.58 (0.75)
595+
50.12 (0.03) 2.53 (2.14) 1.05
596+
6 │ (2.35) 0.94 (0.61) 1.77 (0.48)
597+
71.08 (2.2) 0.07 (0.9) (2.93)
598+
```
599+
600+
The color of the cells can be changed using highlighters. The following example shows how to
601+
highlight negative values in red in HTML output.
602+
603+
```julia
604+
# HTML output (e.g. in Jupyter): cap column width and highlight negatives in red.
605+
julia> hl = HtmlHighlighter((data, i, j) -> data[i, j] < 0, ["color" => "red"]);
606+
607+
julia> show(
608+
stdout,
609+
MIME("text/html"),
610+
df;
611+
highlighters = [hl]
612+
)
613+
```
614+
615+
You can also add summary rows at the bottom of a table using PrettyTables.jl keywords. Pass
616+
a vector of functions to the `summary_rows` parameter to compute metrics, and optionally use
617+
`summary_row_labels` to set labels for those rows.
618+
619+
In the following example, a table displays quarterly profits for a fictional company. The
620+
columns represent years (2020 through 2025), the rows represent quarters, and the summary
621+
rows show the mean and standard deviation for each year, calculated across the four
622+
quarterly values in that column.
623+
624+
```julia
625+
julia> using Statistics, PrettyTables
626+
627+
julia> profit = DataFrame(
628+
"2020" => [ 94.6, -105.6, -104.9, -88.0],
629+
"2021" => [-84.3, -8.7, -109.6, 75.8],
630+
"2022" => [172.6, -42.5, 95.5, -141.0],
631+
"2023" => [-71.2, 51.6, 114.3, 15.5],
632+
"2024" => [-35.4, -44.9, 140.3, 30.8],
633+
"2025" => [ 24.1, 136.1, 34.8, -183.7]
634+
);
635+
636+
julia> metrics = [mean, std];
637+
638+
julia> show(
639+
profit;
640+
# We use this option to align the summary rows with the data rows at the decimal
641+
# point.
642+
apply_alignment_regex_to_summary_rows = true,
643+
summary_rows = metrics,
644+
summary_row_labels = ["Mean", "Std. Dev."]
645+
)
646+
4×6 DataFrame
647+
Row │ 2020 2021 2022 2023 2024 2025
648+
│ Float64 Float64 Float64 Float64 Float64 Float64
649+
───────────┼──────────────────────────────────────────────────────────────
650+
194.6 -84.3 172.6 -71.2 -35.4 24.1
651+
2-105.6 -8.7 -42.5 51.6 -44.9 136.1
652+
3-104.9 -109.6 95.5 114.3 140.3 34.8
653+
4-88.0 75.8 -141.0 15.5 30.8 -183.7
654+
───────────┼──────────────────────────────────────────────────────────────
655+
Mean │ -50.975 -31.7 21.15 27.55 22.7 2.825
656+
Std. Dev. │ 97.3905 83.5073 140.011 77.4612 85.3244 134.2
657+
```
658+
659+
For more customization options, check the
660+
[PrettyTables.jl documentation](https://ronisbr.github.io/PrettyTables.jl/stable/).

0 commit comments

Comments
 (0)