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.
Answer from jxramos on Stack OverflowLooks 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.
There is a way, and it can look quite cool because you have unlimited option with html. Also if you are using plotly, the plots are already in html hence you can transfer them as they are and still keep the interactivity (zoom in, select etc)
First you create a template for Jinja. If the number of graphs is predefined then you just have a static one with placeholders (that's easier), otherwise you need to automate it. (using jinja )
Example of the template.html
<!DOCTYPE html>
<html style="text-align:center;">
<head>
<title> My Report </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
....
<div class="main box_around">
<h2 id="%count_of_unique_obs_over_time"><span> Count of unique obs Over Time </span>
<hr style="width:60%;">
</h2>
<p style="font-size: 21px;">
<div style="overflow-y:scroll;height: 500px; " align="center"> {{count_of_missing_values_over_time_fig }}</div>
</p>
</div>
...
Then you input the graph. ( I use plotly that you can use the ".to_html()" method)
data = {
"table_name_clean":table_name_clean,
...
"count_of_unique_obs_over_time_fig": count_of_unique_obs_over_time_fig.to_html()
}
and finally you fill the template with the data and you save.
with open('html_template.html', 'r') as f:
html_string = f.read()
j2_template = Template(html_string )
with open(save_output_path, 'w') as f:
f.write(j2_template.render(data))
Output images to html using python - Stack Overflow
Generate html document with images and text within python script (without servers if possible) - Stack Overflow
html reports using python
Reddit - The heart of the internet
What other report formats can I create with Python library?
Does your Python via .NET library support creating HTML reports programmatically?
- Install Aspose.Words for Python via .NET
- Add a library reference (import the library) to your Python project
- Create a HTML template marked up with LINQ based syntax
- Load the HTML template document
- Load your data from the data source: files, databases, or custom objects
- Build a report by passing your HTML template and data to a 'ReportingEngine' instance
- Save the generated report as a separate file
How to build HTML report?
Videos
» pip install numerous-html-report-generator
You can use this code to directly embed the image in your HTML: Python 3
import base64
data_uri = base64.b64encode(open('Graph.png', 'rb').read()).decode('utf-8')
img_tag = '<img src="data:image/png;base64,{0}">'.format(data_uri)
print(img_tag)
Python 2.7
data_uri = open('11.png', 'rb').read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}">'.format(data_uri)
print(img_tag)
Alternatively for Python <2.6:
data_uri = open('11.png', 'rb').read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,%s">' % data_uri
print(img_tag)
Images in web pages are typically a second request to the server. The HTML page itself has no images in it, simply references to images like <img src='the_url_to_the_image'>. Then the browser makes a second request to the server, and gets the image data.
The only option you have to serve images and HTML together is to use a data: url in the img tag.
Thanks for your answers, they helped me coming up with something that works for me.
In the script below I am using jinja2 to render an html file ('index.html').
from jinja2 import Template, Environment, FileSystemLoader
# Render html file
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('index.html')
output_from_parsed_template = template.render(no_users=len(users), do_stages=do_stages, users=users, message_counts=message_counts)
# to save the results
with open("OutputAnalysis.html", "w") as fh:
fh.write(output_from_parsed_template)
The html template is:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
<style type="text/css">
#share-buttons img {
width: 32px;
padding: 5px;
border: 0;
box-shadow: 0;
display: inline;
}
body {
padding-left: 5px;
padding-right: 5px;
color: black;
max-width: 450px;
text-align: center;
}
</style>
<title>Your file 2016</title>
</head>
<body>
<h1>This has {{ no_users }} users:</h1>
<ul id="users">
{% for user in users %}
<li>{{user}}</li>
{% endfor %}
</ul>
{# STAGE 1 #}
{% if 1 in do_stages%}
<h1>Something worth showing</h1>
<p>
<img src="image.png" alt="caption" style="width:200px;height:200px;">
</p>
{% endif %}
{# STAGE 2 #}
{% if 2 in do_stages%}
<h1>Other header</h1>
<p>
{% for u in range(0,no_users) %}
<p>{{users[u]}} sent {{message_counts[u]}} messages.</p>
{% endfor %}
</p>
<p>
{% endif %}
<div id="share-buttons">
<!-- Twitter -->
<a href="https://twitter.com/share?url=https://stefanocosentino.com" target="_blank">
<img src="twitter_icon.png" alt="Twitter" />
</a>
</body>
</html>
The html file has the <style> defined in the <head> and loads from the local folder "template".
There are quite a few python template engines - for a start you may want to have a look here : https://wiki.python.org/moin/Templating
As far as I'm concerned I'd use jinja2 but YMMV.
» pip install html-reports
I keep working on certain projects that produce data. The data changes everyday and it needs to be presentable in the form of clean tables for inference.
What I've done so far :- Create a parameterised jinja html template (with for loops and stuff) , put the right pieces at the right places, and render the html table
And I'm not really a front end person, is there any other approach (libraries, tools, etc ) that i could use to make life easier and work on the project itself rather than these html reports...!
Thanks in advance.