Python Inside HTML

hi, can anyone confirm PIH capabilities. source: http://karrigell.sourceforge.net/en/pythoninsidehtml.htm

cant get it work with classes nor functions. HTML code doesnt work inside block.

----------[ works ]-------------
image of indent: http://i32.tinypic.com/2v3fqkz.jpg

<% import zigbee %>
<% import sys %>
<% from socket import * %>
<% import struct %>

start

 &lt;% print "hello discovery" %&gt;
 &lt;% refresh = True %&gt;
 &lt;% node_list = zigbee.getnodelist(refresh) %&gt;
 &lt;% netw = [] %&gt;
 &lt;% for node in node_list: %&gt;
     &lt;% if node.type != "coordinator": %&gt;
       &lt;% n = { 'nm':node.label, 'add':node.addr_extended, 'st':1, 'er':0} %&gt;
       &lt;% try: %&gt;
         &lt;% print "(zb) ", netw.index(n) %&gt;
       &lt;% except Exception, ex: %&gt;
         &lt;% netw.append( n) %&gt;
 &lt;% print "------------------" %&gt;

end

------[ doesnt work ]-----------
image of indent: http://i25.tinypic.com/2h4xsi0.jpg

<% import zigbee %>
<% import sys %>
<% from socket import * %>
<% import struct %>

start

<% def discovery(): %>
<% print “hello discovery” %>
<% refresh = True %>
<% node_list = zigbee.getnodelist(refresh) %>
<% netw = [] %>
<% for node in node_list: %>
<% if node.type != “coordinator”: %>
<% n = { ‘nm’:node.label, ‘add’:node.addr_extended, ‘st’:1, ‘er’:0} %>
<% try: %>
<% print "(zb) ", netw.index(n) %>
<% except Exception, ex: %>
<% netw.append( n) %>
<% print “------------------” %>
<% return netw %>

<% net = discovery() %>

end
--------------------------------

Traceback (most recent call last):
File “”, line 7, in http_handler
File “”, line 22, in ?
File “”, line 11, in discovery
NameError: global name ‘zigbee’ is not defined
----------------------------------

  • can anyone give support on PIH?
  • has anyone got their own version of Karrigell?
  • does PIH really support classes and functions?
  • any alternatives of Python server that would work with Digi X4?

thank you in advance,
Simon

How are you currently hooking PIH into the system… are you using the “Template” model?

Have you considered building the HTML pages directly with the embedded “digiweb” module?

creating functions from within the pih file can be a little tricky, you can get this working by moving your imports within the function.


<% refresh = True %>
<% import zigbee %>
<% node_list = zigbee.getnodelist(refresh) %>
<% netw = [] %>

better yet is to declare your functions in the python script that handles the requests and generate the netw list there(test.py if using the example from the wiki). This would also be closer to following a MVC style architecture (http://en.wikipedia.org/wiki/Model–view–controller).

Message was edited by: clohfink

im using Media:PythonInsideHTML.zip‎ module. http://www.digi.com/wiki/developer/index.php/Python_inside_HTML

What do you have in mind with your second question? HTML Inside Python?

thanks, your post gave me some ideas.
following test.py i extended “server” process, python code is in test.py, interface is supplied by the parties talking to it.

could you confirm or deny, is it possible to post HTML code from test.py to the page calling it?
for example (code runs in test.py):
“|<%=state%>|” - prints nothing
print ‘%s’ % (state) - prints to py console (not page)

thanks,
Simon

Read http://www.digi.com/wiki/developer/index.php/Digiweb The test.py explained in the python inside html article uses Karrigell’s template code to turn the “|<%=state%>|” into a string like
“”"
py_code.write(“|”)
py_code.write(state)
py_code.write(“|”)
“”"
then uses pythons exec to run it on the scope of the the digiweb callback, sending the resulting py_code.getvalue() back as the HTTP response.

This is just an example of course, you can use cheetah or djangos templating just as well. You can also do all the work without any html templates at all, like shown in the digiweb wiki page.