Generate html document with images and text within python script (without servers if possible)
<p>How can I generate HTML containing images and text, using a template and css, in python?</p>

<p>There are few similar questions on stackoverflow (e.g.: <a href="http://stackoverflow.com/questions/6748559/generating-html-documents-in-python">Q1</a>, <a href="http://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django">Q2</a>, <a href="http://stackoverflow.com/questions/16523939/how-to-write-and-save-html-file-in-python">Q3</a>) but they offer solutions that (to me) seem overkill, like requiring servers (e.g. genshi).</p>

<p>Simple code using <code>django</code> would be as follows:</p>

<pre><code>from django.template import Template, Context
from django.conf import settings
settings.configure() # We have to do this to use django templates standalone - see
# http://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django

# Our template. Could just as easily be stored in a separate file
template = """
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Template {{ title }}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
Body with {{ mystring }}.
&lt;/body&gt;
&lt;/html&gt;
"""

t = Template(template)
c = Context({"title": "title from code",
             "mystring":"string from code"})
print t.render(c)
</code></pre>

<p>(From here: <a href="http://stackoverflow.com/questions/6748559/generating-html-documents-in-python">Generating HTML documents in python</a>)</p>

<p>This code produces an error, supposedly because I need to set up a backend:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;input&gt;", line 17, in &lt;module&gt;
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py", line 184, in __init__
    engine = Engine.get_default()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/engine.py", line 81, in get_default
    "No DjangoTemplates backend is configured.")
django.core.exceptions.ImproperlyConfigured: No DjangoTemplates backend is configured.
</code></pre>

<p>Is there a simple way to have a template.html and style.css and a bunch of images, and use data in a python script to replace placeholders in the template.html without having to set up a server?</p>

<p>Thanks for your answers, they helped me coming up with something that works for me.</p>

<p>I am using <code>jinja2</code> to render and html file ('index.html') that I have made separately.  </p>

<pre><code>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) 
</code></pre>

<p>The html template is:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;!-- &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt; --&gt;
    &lt;style type="text/css"&gt;
    #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; 
    }

    &lt;/style&gt;
    &lt;title&gt;Your file 2016&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;This has {{ no_users }} users:&lt;/h1&gt;
    &lt;ul id="users"&gt;
    {% for user in users %}
        &lt;li&gt;{{user}}&lt;/li&gt;
    {% endfor %}
    &lt;/ul&gt;

    {# STAGE 1 #}
    {% if 1 in do_stages%}
    &lt;h1&gt;Something worth showing&lt;/h1&gt;
    &lt;p&gt; 
        &lt;img src="image.png" alt="caption" style="width:200px;height:200px;"&gt; 
    &lt;/p&gt;
    {% endif %}

    {# STAGE 2 #}
    {% if 2 in do_stages%}
    &lt;h1&gt;Other header&lt;/h1&gt;
    &lt;p&gt; 
        {% for u in range(0,no_users) %}
        &lt;p&gt;{{users[u]}} sent {{message_counts[u]}} messages.&lt;/p&gt;
        {% endfor %}
    &lt;/p&gt;
    &lt;p&gt; 
    {% endif %}


    &lt;div id="share-buttons"&gt;
        &lt;!-- Twitter --&gt;
    &lt;a href="https://twitter.com/share?url=https://stefanocosentino.com" target="_blank"&gt;
        &lt;img src="twitter_icon.png" alt="Twitter" /&gt;
    &lt;/a&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Note that the html file has the style defined in the heather and loads from the local folder "template".</p>

python html css django