[ANN] PrettyTables 0.8.0

Hi guys!

I have just requested the registration of the v0.8.0 of PrettyTables.jl. This version has two major features.

The first is that you can use the function include_pt_to_file to add a table into a file between two marks. This works nice together with LaTeX to update tables when writing reports.

The second is the LaTeX back-end. I tried to add a minimal documentation, but everything should be considered experimental (I haven’t coded any tests about it yet…). Please, open issues on GitHub if you find any bug or strange behavior.

Using the following code:

julia> t = 0:1:20;

julia> data = hcat(t, ones(length(t))*1, 1*t, 0.5.*t.^2);

julia> header = ["Time" "Acceleration" "Velocity" "Distance";
                  "[s]"  "[m/s\$^2\$]"    "[m/s]"      "[m]"];

julia> hl_v = LatexHighlighter( (data,i,j)->(j == 3) && data[i,3] > 9, ["color{blue}","textbf"]);

julia> hl_p = LatexHighlighter( (data,i,j)->(j == 4) && data[i,4] > 10, ["color{red}", "textbf"])

julia> hl_e = LatexHighlighter( (data,i,j)->(i == 10), ["cellcolor{black}", "color{white}", "textbf"])

julia> pretty_table(data, header, backend = :latex, highlighters = (hl_e, hl_p, hl_v))

it is possible to render the following LaTeX table:

24 Likes

Thank you for beautiful table utility!

The only thing I miss is user’s control over padding size. With backend=:html, there is zero padding between table border and cell border.

table

I feel a little bit cramped.

1 Like

Thanks!

Can you please tell me how you are printing this table? The predefined formats have a padding of 4px and this should not have happened. This is what I see when I render an HTML: https://ronisbr.github.io/PrettyTables.jl/dev/man/html_backend/#HTML-table-formats-1

@Ronis_BR, thank you for your reply.

I tried printing a table with Weave and I see this:

table

The padding on the right side of the cell is shown to be as zero.

The content of test.jmd is just:

```julia
using DataFrames, PrettyTables

df = DataFrame(x=1:3, y=β€˜a’:β€˜c’)
pretty_table(df, backend=:html)

```

I tested on Julia 1.3.0, PrettyTables v0.8.0, Weave v0.9.1

Can PrettyTables display DataFrames?

I think I need help here. Maybe it is something related to Weave. See the HTML code I get when using your example:

julia> using DataFrames

julia> df = DataFrame(x=1:3, y='a':'c')
using Pr3Γ—2 DataFrame
β”‚ Row β”‚ x     β”‚ y    β”‚
β”‚     β”‚ Int64 β”‚ Char β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€
β”‚ 1   β”‚ 1     β”‚ 'a'  β”‚
β”‚ 2   β”‚ 2     β”‚ 'b'  β”‚
β”‚ 3   β”‚ 3     β”‚ 'c'  β”‚

julia> using PrettyTables
[ Info: Precompiling PrettyTables [08abe8d2-0d0c-5749-adfa-8a2ac140af0d]

julia> pretty_table(df, backend = :html)
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<style>
table, td, th {
    border-collapse: collapse;
    font-family: sans-serif;
}

td, th {
    border-bottom: 0;
    padding: 4px
}

tr:nth-child(odd) {
    background: #eee;
}

tr:nth-child(even) {
    background: #fff;
}

tr.header {
    background: navy !important;
    color: white;
    font-weight: bold;
}

tr.subheader {
    background: lightgray !important;
    color: black;
}

tr.headerLastRow {
    border-bottom: 2px solid black;
}

th.rowNumber, td.rowNumber {
    text-align: right;
}

</style>
<body>
<table>
<tr class = header>
<th style = "text-align: right; ">x</th>
<th style = "text-align: right; ">y</th>
</tr>
<tr class = "subheader headerLastRow">
<th style = "text-align: right; ">Int64</th>
<th style = "text-align: right; ">Char</th>
</tr>
<tr>
<td style = "text-align: right; ">1</td>
<td style = "text-align: right; ">a</td>
</tr>
<tr>
<td style = "text-align: right; ">2</td>
<td style = "text-align: right; ">b</td>
</tr>
<tr>
<td style = "text-align: right; ">3</td>
<td style = "text-align: right; ">c</td>
</tr>
</table>
</body>
</html>

Look at the style part that we have padding: 4px. Thus, something is forcing it to be 0.

Yes, it can! In fact, it can print anything that complies with Tables.jl API.

@Sijun

Notice that everything is fine with Jupyter. I really cannot reproduce this error:

Ah, yes indeed. I tried putting !important at the end in the html file, and it started working.

padding: 4px !important

As you said, Weave seems to overwrite PrettyTables settings. Maybe I need to tell Weave contributors.

1 Like

And how can I use the html backend? I am using vscode. I just get the html source code printed in the terminal, but this is not what I want.

You can define your own HTML style to be used in Weave. Like:

julia> html_weave = HTMLTableFormat(
    css = """
    table, td, th {
        border-collapse: collapse;
        font-family: sans-serif;
    }

    td, th {
        border-bottom: 0;
        padding: 4px !important
    }

    tr:nth-child(odd) {
        background: #eee;
    }

    tr:nth-child(even) {
        background: #fff;
    }

    tr.header {
        background: navy !important;
        color: white;
        font-weight: bold;
    }

    tr.subheader {
        background: lightgray !important;
        color: black;
    }

    tr.headerLastRow {
        border-bottom: 2px solid black;
    }

    th.rowNumber, td.rowNumber {
        text-align: right;
    }
    """
);

julia> df = DataFrame(x=1:3, y='a':'c');

julia> pretty_table(df, backend = :html, tf = html_weave)
2 Likes

Thank you so much. Now it works very well!

1 Like

I do not use vscode. Can it render HTML? The current implementation of the HTML back-end works in Jupyter for example. It prints to stdout using display("text/html", ...). Thus, if what you are using supports HTML, then it should render it as an HTML page. However, maybe it needs tweaking to work in other environments.

1 Like

I also tried Blink, also doesn’t work. How can I store the html in a string?

You can store the HTML as string using an IOBuffer:

julia> io = IOBuffer();

julia> pretty_table(io, [1 1 1; 1 1 1])

julia> str = String(take!(io))
"β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”\nβ”‚ Col. 1 β”‚ Col. 2 β”‚ Col. 3 β”‚\nβ”œβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€\nβ”‚      1 β”‚      1 β”‚      1 β”‚\nβ”‚      1 β”‚      1 β”‚      1 β”‚\nβ””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜\n"

(Just use the HTML back-end in the example above)