report

Report is a tcllib command for report generation.

Documentation can be found at http://core.tcl.tk/tcllib/doc/trunk/embedded/www/tcllib/files/modules/report/report.html

Use example

package require csv
package require report
package require struct

... open a channel to a a CSV file, with default separator (comma)

struct::matrix::matrix    m

# The following code extracts the number of columns from the file
# and initializes the matrix to that number of columns.

if {[gets $channel line] < 0} {
    # here, you either continue, return with an error, or take some other action for when the file being read is empty
}
set data [::csv::split $line $sepChar]

m add columns [llength $data]
m add row $data

csv::read2matrix $channel m
report::report         r [m columns] ; # (*)
r printmatrix2channel  m stdout

This should print the CSV file to stdout, auto-formatted so that each column is wide enough to hold the longest value of that column. No visual frills like header, footer or column separators.


For output as HTML table add

::report::defstyle html {} {
    set c  [columns]
    set cl $c ; incr cl -1
    data set "<tr> [split [string repeat " " $cl] ""] </tr>"
    for {set col 0} {$col < $c} {incr col} {
        pad $col left  "<td>"
        pad $col right "</td>"
    }
    return
}

somewhere before the creation of the report and use

report::report r [m columns] style html

to create the report object, see (*) above.


hv I struggled with the report for a while and finally got it. Since the recipe for reporting is somewhat complex, I wrote a procedure called print_matrix to hide all that complexity and to provide an example of how things work. I also wrote a small load_matrix procedure and hope it is useful to somebody.

package require csv
package require struct::matrix
package require report

# Loads a CSV file into a matrix
proc load_matrix {fileName} {
    set mx [struct::matrix]
    set infile [open $fileName r]
    csv::read2matrix $infile $mx , auto
    close $infile
    return $mx
}

# Prints a matrix as a table with text border
proc print_matrix {mx {headerRows 1}} {
    ::report::defstyle captionedTable {headerRows} {
        # Top and bottom lines
        top       set "[string repeat "+ = " [columns]]+"
        bottom    set [top get]
        
        # Data lines
        data      set "[string repeat "| "   [columns]]|"
        topdata   set [data get]

        # Top caption (AKA headers)
        topcapsep set "[string repeat "+ - " [columns]]+"
        tcaption  $headerRows

        topcapsep enable
        top       enable
        bottom    enable
    }

    ::report::report r [$mx columns] style captionedTable $headerRows
    puts [r printmatrix $mx]
    r destroy
}

#
# main
#
set mx [load_matrix "users.csv"]
print_matrix $mx

Sample CSV file (users.csv):

ID,alias,Full Name
45,alexd,Alexander Dunn
992,alicek,Alice Kim

Sample output:

+===+======+==============+
|ID |alias |Full Name     |
+---+------+--------------+
|45 |alexd |Alexander Dunn|
|992|alicek|Alice Kim     |
+===+======+==============+

Discussion

Nagu 2013-03-30: I've updated the source code of reportx framework at: ::reportx - On GitHub . This updated version handles nested tables. I've also posted a simple example of creating nested tables at: Nested tables using reportx . Please review and let me know if you have any comments/questions. Thanks.


Nagu 2013-03-22: On my Tcl blog at: ::reportx - Tcl Framework To Generate Tabular Reports , I have posted the details of a simple wrapper framework that simplifies the usage of ::report package in generating tabular reports. Please review and let me know if you have any comments/questions. Thanks.


Report is also a Wub utility for web and more general color manipulation.

Latest version may be sourced here [2 ] and perused here [1 ]

CMcC is the current custodian, but help and improvement is welcome.


See also