Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate HTML report in Python?

I'm looking for a way to print out all my graphs (from matplotlib but already saved as png files) and some data frames in HTML, just like what I usually do with R2HTML.

However I couldn't find detailed descriptions about Python module or functions doing this. Could anyone give me some suggestions?

like image 966
lsheng Avatar asked May 29 '14 05:05

lsheng


People also ask

How do you create an HTML test report in Python?

To generate a HTML report for a Selenium test, we have to install a plugin with the command: pip install pytest-html. To generate the report, we have to move from the current directory to the directory of the Pytest file that we want to execute. Then run the command: pytest --html=report. html.

Can Python be used for reporting?

Overview of Python reporting The standard formats of reports are Excel, HTML, and PDF. The good news is, Python can generate reports in all these formats. So you can choose any of these formats, depending on the needs of the report's users. Below is a summary of what we'll cover in this tutorial.


1 Answers

Looks 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.

HTMLTags 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.

htmlgen This package looks pretty good and basic. I'm not sure if its the same module described in this old article.

XIST 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.

with xsc.build() :
    with xsc.Frag() as myHtmlReport :
        +xml.XML()
        +html.DocTypeXHTML10transitional()
        with html.html() :
            reportTitle = "My report title"

            with html.head() :
                +meta.contenttype()
                +html.title( reportTitle )

            with html.body() :
                # Insert title header
                +html.h1( reportTitle )

                with html.table() :
                    # Header Row
                    with html.tr() :
                        with html.td() : 
                            +xsc.Text( "Col 1 Header" )
                        with html.td() :
                            +xsc.Text( "Col 2 Header" )


                    # Data Rows
                    for i in [ 1, 2, 3, 4, 5 ] :

                        with html.tr() :
                            with html.td() : 
                                +xsc.Text( "data1_" + str(i) )
                            with html.td() :
                                +xsc.Text( "data2_" + str(i) )

# Write the report to disk
with open( "MyReportfileName.html" , "wb" ) as f:
    f.write( myHtmlReport.bytes( encoding="us-ascii" ) )

libxml2 through Python bindings there's a plain vanilla xmlwriter module, which is probably too generic but good to know about nonetheless. Windows binaries for this package can be found here.

like image 194
jxramos Avatar answered Nov 01 '22 10:11

jxramos