python generate html report

Solutions on MaxInterview for python generate html report by the best coders in the world

showing results for - "python generate html report"
Helina
08 Nov 2016
1Looks like a couple of tools exist. I've focused on simple html text writers since I'll be crafting my report page structures completely from scratch. This may differ from the R2HTML which I would guess has a lot of convenience functionality for the sort of things one wishes to stuff into pages from R objects.
2
3HTMLTags This fella wrote a module from scratch at this ActiveState community page: HTMLTags - generate HTML in Python (Python recipe). In the comments I discovered most of the tools I enumerate for this answer.
4
5htmlgen This package looks pretty good and basic. I'm not sure if its the same module described in this old article.
6
7XIST this one looks pretty legit and includes a parser as well. Here's some sample page generation code using the format that you'll need to use to interject all the appropriate python commands inbetween various html elements. The other format utilizes a bunch of nested function calls which will make the python interjection very awkward at best.
8
9with xsc.build() :
10    with xsc.Frag() as myHtmlReport :
11        +xml.XML()
12        +html.DocTypeXHTML10transitional()
13        with html.html() :
14            reportTitle = "My report title"
15
16            with html.head() :
17                +meta.contenttype()
18                +html.title( reportTitle )
19
20            with html.body() :
21                # Insert title header
22                +html.h1( reportTitle )
23
24                with html.table() :
25                    # Header Row
26                    with html.tr() :
27                        with html.td() : 
28                            +xsc.Text( "Col 1 Header" )
29                        with html.td() :
30                            +xsc.Text( "Col 2 Header" )
31
32
33                    # Data Rows
34                    for i in [ 1, 2, 3, 4, 5 ] :
35
36                        with html.tr() :
37                            with html.td() : 
38                                +xsc.Text( "data1_" + str(i) )
39                            with html.td() :
40                                +xsc.Text( "data2_" + str(i) )
41
42# Write the report to disk
43with open( "MyReportfileName.html" , "wb" ) as f:
44    f.write( myHtmlReport.bytes( encoding="us-ascii" ) )
45libxml2 through Python bindings there's a plain vanilla xmlwriter module, which is probably too generic but good to know about nonetheless