# Best tool for printing tables in Jupyter notebook?

**URL:** <https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 6, 2017, 12:10am UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548 "2017-05-06T00:10:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 6, 2017, 12:10am UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/1 "2017-05-06T00:10:22Z")

</div>

Lets say I had something like:

```julia
 var | expected_value | actual_value
-----|----------------|--------------
 a | 5 | 5
 s | 8 | 8
 d | 13 | 14
 f | 21 | 23
 z | 34 | 35

```

What’s the best tool for printing the table in Jupyter so it looks nicer than just a `pretty print`?

* * *

edit: could just look something like [datatables.js](https://datatables.net/). // note the `.js`, not `.jl`

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 6, 2017, 12:22am UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/2 "2017-05-06T00:22:42Z")

</div>

I would just put it in a DataFrame. Doesn’t that do a pretty print?

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 6, 2017, 12:30am UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/3 "2017-05-06T00:30:52Z")

</div>

I want it to look more like the output from:

`display("text/html", "<h2 style='padding: 10px'>Arc</h2><table class='table table-striped'> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </tr> </thead> <tbody> <tr> <th scope='row'>1</th> <td>Mark</td> <td>Otto</td> <td>@mdo</td> </tr> <tr> <th scope='row'>2</th> <td>Jacob</td> <td>Thornton</td> <td>@fat</td> </tr> <tr> <th scope='row'>3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> </tbody> </table>")`

i.e.

 ![](https://global.discourse-cdn.com/julialang/original/3X/4/9/499503ede6a30407d6a2641b5955ed2ff957eb27.png)

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 6, 2017, 12:37am UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/4 "2017-05-06T00:37:25Z")

</div>

Not as pretty as your table, but FWIW this is what I use:

```julia
module HTMLElements

export table, fields_table

function table(arr::Matrix; pre=true, column_names=nothing)
    if column_names !== nothing
        arr = vcat(reshape(column_names, 1, length(column_names)), arr)
    end
    s =
    """
    <table border="1">
    """
    for r in 1:size(arr, 1)
        s *= "<tr>"
        for c in 1:size(arr, 2)
            s *= "<td>"
            if pre s *= "<pre>" end
            s *= string(arr[r, c])
            if pre s *= "</pre>" end
        end
        s *= "</tr>"
    end
    s *= "</table>"
    return HTML(s)
end

table(ass::Associative; kwargs...) = table(ass...; kwargs...)
table(pairs::Pair...; kwargs...) =
    table(hcat([x[1] for x in pairs], [x[2] for x in pairs]); kwargs...)

""" `fields_table(obj)` displays the fields of object in an HTML table """
fields_table(obj) =
    table(Pair[name=>getfield(obj, name) for name in fieldnames(obj)]...)

end

```

![](https://global.discourse-cdn.com/julialang/original/3X/8/3/8341b8d2aa934330822cc80694bca7a52a83a791.png)

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 6, 2017, 12:44am UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/5 "2017-05-06T00:44:19Z")

</div>

I agree this is a step in the right direction, but I don’t think we’re at the solution yet

two questions,

- what was your reasoning for adding the `<pre>` tags?
- does this link up with `DataFrames.jl` out of the box?

// update: durrrr… `<pre>` tags use monospace font

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 6, 2017, 12:48am UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/6 "2017-05-06T00:48:05Z")

</div>

> [@djsegal](#):
>
> does this link up with DataFrames.jl out of the box?

Set this up with as a DataStream Sink, and it will support DataFrames, DataTables, …

But I think this would be a nice package to have accessible somewhere.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [May 6, 2017, 12:57am UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/7 "2017-05-06T00:57:17Z")

</div>

> [@djsegal](#):
>
> what was your reasoning for adding the \<pre\> tags?

No reason; it’s just easier on the eyes for the kind of data that I’m working with.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 7, 2017, 8:23pm UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/8 "2017-05-07T20:23:06Z")

</div>

I wrote this up. It’s basically just @cstjean’s code from above:

There’s probably more to be done on it

```julia
module HTMLElements

  export table

  function table(cur_matrix::Matrix; header_row=[], title=nothing)

    cur_table = ""

    if title != nothing
      cur_table *= "<h2 style='padding: 10px'>$title</h2>"
    end

    cur_table *= "<table class='table table-striped'>"

    if !isempty(header_row)
      cur_table *= "<thead><tr>"

      for cur_header in header_row
        cur_table *= "<th>$cur_header</th>"
      end

      cur_table *= "</tr></thead>"
    end

    cur_table *= "<tbody>"

    for ii in 1:size(cur_matrix, 1)

      cur_table *= "<tr>"

      for jj in 1:size(cur_matrix, 2)
        cur_table *= "<td>"

        cur_table *= string(cur_matrix[ii, jj])

        cur_table *= "</td>"
      end

      cur_table *= "</tr>"

    end

    cur_table *= "</tbody>"

    cur_table *= "</table>"

    return HTML(cur_table)

  end

end

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 7, 2017, 8:45pm UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/9 "2017-05-07T20:45:57Z")

</div>

This is very similar to the format in the new version of the notebook (5.0). Install it via

```julia
julia> using Conda
julia> Conda.update()

```

Blog post: [http://blog.jupyter.org/2017/04/04/jupyter-notebook-5-0/](http://blog.jupyter.org/2017/04/04/jupyter-notebook-5-0/)

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 7, 2017, 9:01pm UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/10 "2017-05-07T21:01:21Z")

</div>

How do I make that table though?

I’m guessing it’s not done with `println`’s or `display("text/html", "...")`'s

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 7, 2017, 9:05pm UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/11 "2017-05-07T21:05:00Z")

</div>

Markdown table syntax.

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [May 7, 2017, 9:14pm UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/12 "2017-05-07T21:14:26Z")

</div>

I don’t think that will work.

I was trying to wrap this in a `@manipulate`

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 7, 2017, 9:19pm UTC](https://discourse.julialang.org/t/best-tool-for-printing-tables-in-jupyter-notebook/3548/13 "2017-05-07T21:19:06Z")

</div>

Ah, then no. You will effectively have to use the html output I guess, but as someone suggested, doesn’t DataFrames or whatever already have this? So just make am object of the relevant type.
