Best tool for printing tables in Jupyter notebook?

question

#1

Lets say I had something like:

 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. // note the .js, not .jl


Markdown tables
#2

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


#3

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.


#4

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

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


#5

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


#6

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.


#7

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


#8

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

There’s probably more to be done on it

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

Print matrix as html/pdf table
Pretty printing of tables
Markdown tables
#9

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

julia> using Conda
julia> Conda.update()

Blog post: http://blog.jupyter.org/2017/04/04/jupyter-notebook-5-0/


#10

How do I make that table though?

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


#11

Markdown table syntax.


#12

I don’t think that will work.

I was trying to wrap this in a @manipulate


#13

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.