# \[ANN\] PrettyTables.jl - Print formatted tables in Julia

**URL:** <https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550>\
**Category:** Package Announcements\
**Created:** [January 12, 2019, 3:49am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550 "2019-01-12T03:49:21Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 18, 2019, 2:32am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/41 "2019-01-18T02:32:44Z")

</div>

Guys!

I needed to change the API because I wanted to support Crayons.jl. Now, every single styling is handle by Crayons.jl. The downside is that this is breaking (sorry…). The good side is that we have now many options to customize our tables, like changing the background to highlight rows and columns:

 ![welcome_figure](https://global.discourse-cdn.com/julialang/original/3X/6/0/602809b99e6db7da155aad886ee55e43866b4353.jpeg)

I did not tag a version yet and I would love some feedback before 🙂

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [January 18, 2019, 6:19am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/42 "2019-01-18T06:19:22Z")

</div>

Beautiful!

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 30, 2019, 5:29pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/43 "2019-01-30T17:29:02Z")

</div>

Hi guys!

I have updated the package (`master`) to support multiple lines in the cells. Hence, we can now have something like:

```julia
julia> data = Any[1.0 "Flag 1, Flag 2, Flag 3\nFlag 4, Flag 5, Flag 6, Flag 7";
                  2.0 "Flag 2, Flag 3, Flag 8\nFlag 10";
                  3.0 "Flag 4, Flag 6"];

julia> pretty_table(data, ["t" "Active flags"]; linebreaks=true, alignment=:l, hlines = [1,2,3])
┌─────┬────────────────────────────────┐
│ t │ Active flags │
├─────┼────────────────────────────────┤
│ 1.0 │ Flag 1, Flag 2, Flag 3 │
│ │ Flag 4, Flag 5, Flag 6, Flag 7 │
├─────┼────────────────────────────────┤
│ 2.0 │ Flag 2, Flag 3, Flag 8 │
│ │ Flag 10 │
├─────┼────────────────────────────────┤
│ 3.0 │ Flag 4, Flag 6 │
└─────┴────────────────────────────────┘

```

I would appreciate if someone can test before I tag the new version 🙂

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 30, 2019, 6:01pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/44 "2019-01-30T18:01:21Z")

</div>

I just noticed this package — it might be just what I was looking for. I looked in the documentation and didn’t see this there, but is it possible to print a custom row name rather than a row number?

Edit: I suppose it could be treated as just another column in this formulation.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 30, 2019, 6:07pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/45 "2019-01-30T18:07:35Z")

</div>

> [@yurivish](#):
>
> Edit: I suppose it could be treated as just another column in this formulation.

Yeah! Just use `hcat` and add whatever you want 🙂 Let me know if this works for you.

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 30, 2019, 6:14pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/46 "2019-01-30T18:14:29Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/5/1/51885f9a8fdd697d7c6adfa0b763ce69b51f7ea9.png)

Neat! 🙂

Is there a way to make the elements of the first column bold to match the headers in the top row?

Edit: Ah, right, I should be able to do it with a Highlighter:

```julia
highlight_row_label = Highlighter(
    f=(data, i, j) -> j == 1,
    crayon=Crayon(bold=true)
)

```

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 30, 2019, 6:20pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/47 "2019-01-30T18:20:35Z")

</div>

Yes, notice that a crayon can be created easier with just `crayon"bold"`.

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 30, 2019, 6:24pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/48 "2019-01-30T18:24:26Z")

</div>

Oh, that’s really nice!

So I think I’ve found a possibly legitimate reason to want row labels — the row labels might be a different type than the array. And the array might be sparse, and there are currently some problems in Base with some operations on `SparseArray`s of type `Any`, which is what you’d get if the row labels were concatenated to the matrix. There’s also the performance impact of constructing the row-concatenated array purely for display purposes.

I also noticed that larger tables don’t get visually abbreviated, which may be a feature or a curse, depending on circumstance. 😃

My use case is for implementing the printing of a custom array type, which may get very large, so I’d need to edit down the printed array in some fashion.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/c/4c3dd8a9c45b25a6967e403cd2bdc615f9ac5b90.png)

Otherwise, things look good on `master` to me!

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 30, 2019, 6:28pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/49 "2019-01-30T18:28:54Z")

</div>

I think I found an escaping bug:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a3a16c1da4c24d48586ff95227a7f837750c7b1.png)

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 30, 2019, 6:50pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/50 "2019-01-30T18:50:07Z")

</div>

Yeah, I haven’t though yet what to do if the table is bigger than the display. When I have to print really long tables, I print to files and open in an editor without line wrapping.

> [@yurivish](#):
>
> I think I found an escaping bug:

I have to escape the string to avoid formatting problems. The Julia function `escape_string` automatically escape `"` by default. I have not idea how to easily fix that without introducing many other problems. For example, if I do not escape the string, a header with “1234\t” will break the format of the table.

Any ideas?

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 30, 2019, 6:57pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/51 "2019-01-30T18:57:38Z")

</div>

Escaping usually depends on what you’re trying to accomplish – is the goal here to know how many on-screen characters each string takes up so that you can compute the layout parameters?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 30, 2019, 7:01pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/52 "2019-01-30T19:01:53Z")

</div>

Yes it is. I need to compute the size of the each column.

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 30, 2019, 7:05pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/53 "2019-01-30T19:05:40Z")

</div>

Then you might be able to get away with simply stripping out characters of variable width (eg. `strip(s, ['\t'])`. In ASCII I don’t know of any other than tab off the top of my head, so that might be a good start.

Escaping is also used e.g. in HTML to prevent accidental injection of content as code, but that’s a different use. In case that’s also useful for PrettyTables HTML display you can see how I implemented it in my Hyperscript package [here](https://github.com/yurivish/Hyperscript.jl/blob/master/src/Hyperscript.jl#L323) and [here](https://github.com/yurivish/Hyperscript.jl/blob/master/src/Hyperscript.jl#L179).

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [January 30, 2019, 7:09pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/54 "2019-01-30T19:09:21Z")

</div>

The problem of selecting which characters to escape with `string` is that I may miss many more, like, for example, `\e` etc. That’s why escaping the entire string (like the display function of DataFrames do) seems more appropriate.

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 30, 2019, 7:34pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/55 "2019-01-30T19:34:51Z")

</div>

Oh, I see. Thanks for that example. I’ll think about it and let you know if I come up with anything.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [January 31, 2019, 2:06am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/56 "2019-01-31T02:06:25Z")

</div>

You could print a subset of large tables in the REPL and use arrow keys to scroll around. REPGamesBase.jl has functionality for the key input.

---

<div class="post-metadata">

**Author:** ![sbk](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@sbk](https://discourse.julialang.org/u/sbk)\
**Post date:** [April 24, 2019, 6:09pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/57 "2019-04-24T18:09:20Z")

</div>

Hi,

This is a really nice package. How do I print tables side by side.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [April 24, 2019, 6:16pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/58 "2019-04-24T18:16:52Z")

</div>

Hi @sbk,

Thanks 🙂 In fact, we do not have support to print two tables side by side. Can you explain a little more what do you want to achieve?

---

<div class="post-metadata">

**Author:** ![sbk](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@sbk](https://discourse.julialang.org/u/sbk)\
**Post date:** [April 24, 2019, 6:21pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/59 "2019-04-24T18:21:23Z")

</div>

Thanks for your reply. What I want is exactly two tables side by side.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 24, 2019, 6:31pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/60 "2019-04-24T18:31:20Z")

</div>

just hcat the tables

[Previous page](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550.md?page=2)

[Next page](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550.md?page=4)
