Skip to content

About

Print data in formatted tables.

Resources

Stars

480 stars

Watchers

3 watching

Forks

Repository files navigation

PrettyTables.jl

CI Codecov docs-stable docs-dev Code Style: Blue License DOI

PrettyTables.jl prints data in formatted tables using different back ends: text, HTML, LaTeX, Markdown, Typst, Excel (with XLSX.jl), and Word (with WriteDocx.jl). It was originally inspired by the functionality provided by ASCII Tables.

PrettyTables.jl allows printing the data together with some table sections, which can be modified by the user to obtain the desired output. The sections currently available are:

Table Design

This design is heavily inspired by the R package gt, but the API is quite different due to the differences between the R and Julia languages.

Installation

julia> using Pkg
julia> Pkg.add("PrettyTables")

Example

We present in the following an example showing some of the features available in PrettyTables.jl. The highlighters, the table format, and the table style are defined using the backend-agnostic types Highlighter, TableFormat, and TableStyle, so the same configuration can be used with every back end.

julia> using PrettyTables, StyledStrings

# == Creating the Table ====================================================================

julia> v1_t = 0:5:20

julia> v1_a = ones(length(v1_t)) * 1.0

julia> v1_v = @. 0 + v1_a * v1_t

julia> v1_d = @. 0 + v1_a * v1_t^2 / 2

julia> v2_t = 0:5:20

julia> v2_a = ones(length(v2_t)) * 0.75

julia> v2_v = @. 0 + v2_a * v2_t

julia> v2_d = @. 0 + v2_a * v2_t^2 / 2

julia> table = [
  v1_t v1_a v1_v v1_d
  v2_t v2_a v2_v v2_d
];

# == Configuring the Table =================================================================

julia> title = "Table 1. Data obtained from the test procedure."

julia> subtitle = "Comparison between two vehicles"

julia> column_labels = [
    [EmptyCells(2), MultiColumn(2, "Estimated Data")],
    ["Time (s)", "Acceleration", "Velocity", "Position"],
]

julia> units = [
  styled"{(foreground=gray):[s]}",
  styled"{(foreground=gray):[m / s²]}",
  styled"{(foreground=gray):[m / s]}",
  styled"{(foreground=gray):[m]}",
]

julia> push!(column_labels, units)

julia> merge_column_label_cells = :auto

julia> row_group_labels = [
    1 => "Vehicle #1",
    6 => "Vehicle #2"
]

julia> summary_rows = [
    (data, j) -> maximum(@views data[ 1:5, j]),
    (data, j) -> maximum(@views data[6:10, j]),
]

julia> summary_row_labels = [
    "Max. for Vehicle #1",
    "Max. for Vehicle #2",
]

julia> footnotes = [
    (:column_label, 1, 3) => "Estimated data based on the acceleration measurement."
]

julia> highlighters = [
    Highlighter(
        (data, i, j) -> (j == 3) && (data[i, j] > 10),
        Face(; weight = :bold, foreground = :red)
    ),
    Highlighter(
        (data, i, j) -> (j == 4) && (data[i, j] > 10),
        Face(; weight = :bold, foreground = :blue)
    ),
]

julia> table_format = TableFormat(;
    # Remove vertical lines.
    @no_vertical_lines
)

julia> style = TableStyle(;
    column_label                   = Face(; weight = :bold),
    first_line_merged_column_label = Face(;
        weight = :bold, foreground = :yellow, underline = true
    ),
    footnote                       = Face(; foreground = :cyan),
    row_group_label                = Face(; weight = :bold, foreground = :magenta),
    subtitle                       = Face(; slant = :italic),
    title                          = Face(; weight = :bold, foreground = :yellow),
)

# == Printing the Table ====================================================================

julia> pretty_table(
    table;
    column_labels,
    footnotes,
    highlighters,
    merge_column_label_cells,
    row_group_labels,
    style,
    subtitle,
    summary_row_labels,
    summary_rows,
    table_format,
    title,
)

PrettyTables.jl example

Usage

See the documentation.

About

Print data in formatted tables.

Resources

Stars

480 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages