Python inside HTML

Allows you to be able to embed Python within HTML documents, similiar to mod_Python or PHP.


By taking advantage of both the Module: digiweb and a slightly modified version of from the BSD licensed project Karrigell Python web server to make it a stand alone library. It is possible to run embed Python within a HTML document that can be executed at run time.

Inside HTML


By enclosing Python statements within a <% %> tag the Python interpreter will execute said statements. In the following example a "stored_time" variable will be created and will save the time on the local scope.

<% import time %>
<% stored_time = time.strftime("%d:%m:%y",time.localtime(time.time())) %>

If enclosed with <%= %> it will evaluate the statement and replace the tag with the result of the executed statement. In the following example the HTML created will contain the day:month:year from the devices internal clock.

<% import time %>
<%= time.strftime("%d:%m:%y",time.localtime(time.time())) %>


Declared Indentation

A file is converted into Python code, which must be indented according to Python rules ; whereas in normal HTML indentation is used only for readability.

So beware if you mix Python and HTML :

    1 <% for i in range(10): %>
    2 <%= i %>*<%= i %> : <b> <%= i*i %> </b>

This will work because after a loop or a condition the following HTML is automatically indented by PIH.

To decrement indentation, use <% end %> :


    1 <% for i in range(10): %>
    2 <%= i %>*<%= i %> : <b> <%= i*i %> </b>
    3 <% end %>
    4 <h3>done</h3>

in this example, "done" will be written after the for loop is finished.

Another example for an if... else... :

    1 <% if i: %>
    2   output someting
    3 <% end %>
    4 <% else: %>
    5   output someting else
    6 <% end %>
    7 <h3>done</h3>

(Don't forget the last <% end %> otherwise "done" would have the same indentation as line 5) But this :

    1 <% for i in range(10):
    2    data= '%s * %s' %(i,i) %>
    3    <b> <%= i*i %> </b>
    4 <h3>done</h3>

Won't work, because after the print statement on line 2 indentation goes back to 0 (it begins with plain HTML).

The <INDENT> Tag

If you have complex code where Python and HTML are mixed, embed it between the tags <indent> and </indent> :

    1  <indent>
    2  <% for i in range(10):
    3    data= '%s * %s' %(i,i) %>
    4    <b> <%= i*i %> </b>
    5  </indent>
    6  <h3>Table</h3>
    7  <table>
    8    <tr>
    9      <td>A cell</td>
    10   </tr>
    11 </table>

<indent> means : from now on, and until the matching </indent> tag, use the indentation in PIH source and leave it as it is to produce Python code In the above example, indentation is used until line 5 and ignored afterwards If the <indent> tag itself is indented, the following code is indented relatively to it :


    1   <table border=1>
    2     <tr>
    3       <th>Number</th>
    4       <th>Square</th>
    5     </tr>
    6     <indent>
    7     <% for i in range(10): %>
    8       <tr>
    9       <td><%= i %></td>
    10      <td><%= i**2 %></td>
    11      </tr>
    12    </indent>
    13  </table>

In line 7, <% is aligned on <indent> so the Python code will not be indented.

Ending Script

If you want to exit the script before the end of the document, raise a

SCRIPT_END exception
  raise SCRIPT_END,message

Writing to HTML output

If you want to write to the HTML output without using the evaluating tags you can write directly to the Python code output via

py_code.write( "<H1> Heading </H1>" )

Working example from the Python

For this example we will create a generic handler for a filetype for the web server by using the Module:digiweb.

import sys,time,digiweb
from PythonInsideHTML import PIH

def http_handler(type, path, headers, args):
 exec PIH("WEB/Python%s"%(path)).PythonCode()
 return (digiweb.TextHtml,py_code.getvalue())

hnd = digiweb.Callback(http_handler)
while (True):

Now you can just upload a file ending in a via the Python file management section of the webui, then just navigate to http://device_address/filename.

For example uploading the following (template.pih) will demonstrate displaying information about the HTTP request passed to the http_handler. The Python code generated by this script is run on the same scope as the http_handler function so has access to the arguments (type, path, headers, args).

<html><head><title>Request info</title></head>
<%= type %> request for path '<%= path %>'
<table border=1 >
<% for h in headers: %>
      <td> <%= h %> </td> <td> <%= headers[h] %> </td>
<% end %>
Args: <%= args %>

Navigating your browser to "http://device_address/template.pih" will give you a page displaying information about the HTTP request.

GET request for path '/template.pih'
host	device_address
agent	Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/ Safari/525.19
Args: None


by importing the following module you can use these features:


Module: digiweb


© 2018 Digi International Inc. All rights reserved.
Python inside HTML updated on 11 Jun 2018 12:08 PM