[ANN] PrettyTables.jl v2.3.0 - Markdown back end

Hi everyone!

I was a little off from the community due to some problems, but Iā€™m back now. :slight_smile:

I want to announce that I just tagged PrettyTables.jl v2.3.0. We have some important internal improvements (like using PackageCompiler.jl PrecomipleTools.jl to generate precompilation statements), but the significant feature here is the new true Markdown back end.

If you pass backend = Val(:markdown), you will see:

julia> pretty_table(rand(4, 4), backend = Val(:markdown))
| **Col. 1** | **Col. 2** | **Col. 3** | **Col. 4** |
|-----------:|-----------:|-----------:|-----------:|
| 0.91437    | 0.166273   | 0.0286585  | 0.51545    |
| 0.869749   | 0.332251   | 0.598352   | 0.815126   |
| 0.959903   | 0.910812   | 0.773085   | 0.860781   |
| 0.0229836  | 0.583163   | 0.473758   | 0.334024   |

Differently from what we had in the past (using text back end), if we change the column alignment, the text itself does not change, but the alignment row is update accordingly:

julia> pretty_table(rand(4, 4), backend = Val(:markdown), alignment = [:l, :c, :r, :r])
| **Col. 1** | **Col. 2** | **Col. 3** | **Col. 4** |
|:-----------|:----------:|-----------:|-----------:|
| 0.593286   | 0.547841   | 0.255428   | 0.0311389  |
| 0.662025   | 0.333026   | 0.229236   | 0.852613   |
| 0.357072   | 0.86252    | 0.175641   | 0.376265   |
| 0.880573   | 0.94005    | 0.279839   | 0.998748   |

The highlighters in the Markdown back end receives an object of type MarkdownDecoration that can modify the text in the cell by setting it to be bold, italic, ~strikethrough~, or code. For example:

julia> pretty_table(
           rand(4, 4),
           backend = Val(:markdown),
           highlighters = (
               hl_col(1, MarkdownDecoration(bold = true)),
               hl_col(2, MarkdownDecoration(italic = true)),
               hl_col(3, MarkdownDecoration(strikethrough = true)),
               hl_col(4, MarkdownDecoration(code = true))
           )
       )
| **Col. 1**    | **Col. 2** | **Col. 3** | **Col. 4** |
|--------------:|-----------:|-----------:|-----------:|
| **0.869034**  | _0.861814_ | ~0.335917~ | `0.637624` |
| **0.986734**  | _0.234084_ | ~0.507542~ | `0.113188` |
| **0.0876319** | _0.881392_ | ~0.302185~ | `0.656289` |
| **0.829593**  | _0.478708_ | ~0.876485~ | `0.266334` |

Since it will work with any Tables.jl object, we can now print a DataFrame to Markdown using:

julia> df = DataFrame(a=["a", "None", "b", "None"], b=1:4,
                      c=["None", "j", "k", "h"], d=["x", "y", "None", "z"])
4Ɨ4 DataFrame
 Row ā”‚ a       b      c       d      
     ā”‚ String  Int64  String  String 
ā”€ā”€ā”€ā”€ā”€ā”¼ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€
   1 ā”‚ a           1  None    x
   2 ā”‚ None        2  j       y
   3 ā”‚ b           3  k       None
   4 ā”‚ None        4  h       z

julia> pretty_table(df, backend = Val(:markdown))
| **a**<br>`String` | **b**<br>`Int64` | **c**<br>`String` | **d**<br>`String` |
|------------------:|-----------------:|------------------:|------------------:|
| a                 | 1                | None              | x                 |
| None              | 2                | j                 | y                 |
| b                 | 3                | k                 | None              |
| None              | 4                | h                 | z                 |

which is rendered in https://markdownlivepreview.com as:

The Markdown output is not limited by the display size as in text back end. However, we can set max_num_of_rows and max_num_of_columns to crop the output for big tables:

julia> pretty_table(
           rand(1000, 1000),
           backend = Val(:markdown),
           max_num_of_rows = 4,
           max_num_of_columns = 4,
           show_omitted_cell_summary = true
       )
| **Col. 1** | **Col. 2** | **Col. 3** | **Col. 4** | ā‹Æ |
|-----------:|-----------:|-----------:|-----------:|:-:|
| 0.322182   | 0.532873   | 0.586433   | 0.0899228  | ā‹Æ |
| 0.31999    | 0.392158   | 0.351315   | 0.696006   | ā‹Æ |
| 0.0767427  | 0.885363   | 0.454803   | 0.627742   | ā‹Æ |
| 0.143568   | 0.35654    | 0.410591   | 0.347192   | ā‹Æ |
| ā‹®          | ā‹®          | ā‹®          | ā‹®          | ā‹± |

_996 columns and 996 rows omitted_

which is renderer as:

51 Likes

Thanks for your hard work! PrettyTables is a extremely helpful package.

1 Like

This is awesome! Thank you!

1 Like

Actually I meant PrecompileTools.jl here :smiley: Thanks @Palli

1 Like

How does Pretty Tables compare with table management in EMACS org mode?

PrettyTables.jl only prints tables in different back ends (text, latex, html, and now markdown). It also has the possibility to format cells and apply some decorations. It does not have all the features we see in org-mode, like cell formulas etc. However, this kind of processing can be easily done using DataFrames, which uses PrettyTables.jl to display the data.

2 Likes