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>I use code like this:</p>

<pre><code>self.content = '''
    &lt;html&gt;
        &lt;head&gt;
            ''' + base_pyhtml.getAdmin ('documentation') + '''
            &lt;style&gt;
                ''' + base_pycss.getAll () + '''
                ''' + documentation_pycss.getAll () + '''
            &lt;/style&gt;
        &lt;/head&gt;
        &lt;body&gt;
            ''' + base_pyhtml.getFixed () + '''

            &lt;div class="moving"&gt;
                &lt;div class="documentation"&gt;
</code></pre>

<p>and</p>

<pre><code>def getAll ():
    return '''
        * {
            margin: 0;
            padding: 0;
        }

        body {
            font-family: arial;
        }

        html {
            visibility: hidden;
        }

        h1 {
            margin-bottom: 30;
            padding: 10 1 10 1;
            text-align: center;
            color: ''' + logoGreen + ''';
            background-color: ''' + panoramaPink + ''';
            font-size: 110%;
</code></pre>

<p>For me it works fine in practice. This <a href="http://www.transcrypt.org" rel="nofollow">site</a> was completely made with this trivial technique. What I like is that I can use the full power of Python in this way, rather than being restricted by or even learning special template syntax.</p>

<p>By using .pyhtml and .pycss extensions and configuring my editor, I get HTML and CSS syntax highlighting on this type of code as shown below:</p>

<p><a href="http://i.stack.imgur.com/jdzDn.png" rel="nofollow"><img src="http://i.stack.imgur.com/jdzDn.png" alt="enter image description here"></a></p>

python html css django