How to configure mod_wsgi for embedded Python?

227 views
Skip to first unread message

Marc ThinlineData

unread,
Oct 5, 2012, 4:50:24 PM10/5/12
to mod...@googlegroups.com
I was wondering if mod_wsgi eventually allows me to embed Python code directly into HTML such as mod_python does? 

I have installed mod_wsgi but I came short to finding any topic that pointed me into a direction for allowing me to directly embedding Python code into an HTML file. 

What I cant really figure out would be what the extension of the files would then have to be ( .psp? ) 

And additionally to that, what is the coding syntax going to look like ( {% %} ) ? 

I was looking at Mako for Python to embed Python code directly into HTML, but it seems Mako still uses the template approach. And I am not really to happy about the entire MVC terminology for the small fixes and patches I need to do on certain websites. 

Hope someone can direct me to an article or tutorial that covers mod_wsgi with Python embedded into HTML. 

Jason Garber

unread,
Oct 5, 2012, 5:01:30 PM10/5/12
to mod...@googlegroups.com
Hi Marc,

I think you are not understanding what mod_wsgi is.  

"The aim of mod_wsgi is to implement a simple to use Apache module which can host any Python application which supports the Python WSGIinterface. The module would be suitable for use in hosting high performance production web sites, as well as your average self managed personal sites running on web hosting services."

WSGI is the "Web Server Gateway Interface", as (mostly) defined in http://www.python.org/dev/peps/pep-0333/

mod_wsgi provides a mechanism to connect apache with python, so that python can form a response to a web request.  However, the interface is very basic.  Python is expected to return the (usually html) data directly.

Any templating type systems would happen at a level above mod_wsgi.  Take a look at some of the python web frameworks out there (bottle, flask, django, etc...) to see what they have to offer.

Under mod_python, the PSP handler is what was responsible for the syntax you were referencing.  In the same way, if such a thing exists to be run under WSGI, it would be a separate project (and be able to run on any compliant wsgi server).

I am not personally aware of such a thing.  Personally, I accomplish a similar feat using python code directly:  Given that HS() is a function which encodes HTML entities, and QA() is an xmlish quote attribute function, and UE() is a url encode function:

html = '''
<div>
  <h1>Hi, this is an example</h1>
  <h2>Record List</h2>
  ''' + ''.join('''
  <div class="record">
     First Name: ''' + HS(row.FirstName) + '''<br />
     Last Name: ''' + HS(row.LastName) + '''<br />
     <a href=''' + QA('/user/details?id=' + UE(row.ID)) + '''>Details</a>
  </div>
  ''' for row in GetList()) + '''
</div>
'''

I've used PHP and ASP.Net razr, and this isn't that much different.  It requires you to think a little harder sometimes, but it is very possible to get the same effect using python syntax directly embedded in a view function.

JG





--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To view this discussion on the web visit https://groups.google.com/d/msg/modwsgi/-/g6BQdctrAj8J.
To post to this group, send email to mod...@googlegroups.com.
To unsubscribe from this group, send email to modwsgi+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/modwsgi?hl=en.

Marc ThinlineData

unread,
Oct 5, 2012, 5:21:23 PM10/5/12
to mod...@googlegroups.com
Hey Jason, 

Thank you for the quick reply. 

For some reason I was assuming mod_wsgi was embedding Python in the "PSP" way as well. 

But you lost me on your html code there. What did you just do? Did you just after all implement Python syntax in HTML without any template/framework?

My other approach was to make use of Mako, but since that too is a template I am yet to adhere to the template terminology :(

I have a Mako thread running here, but they are quite inactive in that group. https://groups.google.com/forum/#!topic/mako-discuss/kiC6zAN0VmY

To say the least, I am struggling to find a simple, light and "up-to-date" method in allowing Python to work directly inside HTML, thus why you surprised me there, please do share :)

Jason Garber

unread,
Oct 5, 2012, 6:18:47 PM10/5/12
to mod...@googlegroups.com
Hi Marc,

I'm using AppStruct, which is a python framework that I developed to interface with mod_wsgi.  I really dislike some of the overhead and complexity introduced by MVC type frameworks for simple projects. 

I've put together a pretty complete example of using AppStruct.  This is designed to run with Python 3 and mod_wsgi on RHEL/Centos linux.  


Here is the login page from the example gist

A couple of my objectives were:
1. simple --- one file should be enough to get a webpage added to the project
2. elegant --- minimal typing overhead
3. powerful  --- expose all of the layers down to the bare protocol level
4. easy --- if you don't need it, keep it out of your way

At my company, we write large and complex custom web applications.  We needed a way to do it that would stay our of our way, but give us the power to do many things.

Let me know if you would like to explore further.  Thanks.

--
The HTML example was just using Python triple-quotes (which can span lines) to make it easy.  Also I was using a generator expression to stand in for a loop.  

It is way way better than something like this:

*** awful way to generate HTML in python ***

html = '<h1>do this</h1>
for row in rows:
   html += '<div class="row">FirstName: '
   html += HS(row.FirstName)
   html += '<br />'
   html += 'LastName: '
   html += HS(row.LastName)
   html += '<br />'
   html += '</div>




To view this discussion on the web visit https://groups.google.com/d/msg/modwsgi/-/xZHEzegEbl8J.

Graham Dumpleton

unread,
Oct 5, 2012, 6:57:42 PM10/5/12
to mod...@googlegroups.com
Template systems which embed Python code directly into HTML is
generally regarded as bad practice in the Python world.

You are better off just learning about one of the more modern template systems.

I would very much suggest you try Flask and the Jinja2 that it
supports. This is going to be the simplest path.

Graham

Marc ThinlineData

unread,
Oct 5, 2012, 7:30:50 PM10/5/12
to mod...@googlegroups.com
Graham - I respectfully disagree, but not from the point of view that you may think I am. When looking at frameworks, Django probably pops up as the first one for first time Python people that want to expand to the web. 

Django is obscure, tedious and requires to adhere by principles and only works for large projects. I understand there is Flask, Pylons, Pyramid, Web2py, Webpy and probably more frameworks, than what we are allowed to type into as a response character wise. I am also well aware of the fact that they offer stability, professional coding techniques and security, over dropping code into files as with php/jsp. 

But I am coming from another world. A world that is outside of the hardcore development world, that focuses more on end user interfaces, production times and aesthetic value. Most companies probably cant offer to pay a developer wage, or multiple developers for that matter. A large number of internet business are ALSO about SEO value, CSS value and "just deploying a website". Why else would entire web platforms such as templatemonster.com have such a massive business. Most of their stuff is based on php. Not one single framework integration or MVC standard is there. 

For those people Wordpress, Joomla and Drupal is their world. They dont even think MVC or argue about Zend Vs Django or Flask Vs Rails (or other comparisons). For them the web interface is more simple. And for them the choice usually stands to be php and in increasingly rare instances asp. Javascript with jQuery takes care of the rest, and this more or less covers 95% of the web ecosystem. Remaining are the big boys that hires people which even knows what mod_wsgi is and why it should be installed, and can choose OS and probably a bunch of other stuff. 

So my point here is that offering the power of Python and the simplicity of Python to integrate into HTML will open up more doors than some might imagine to begin with. Remember that not everyone doing some code in PHP nescesarily is using Zend or CakePHP. A large industry is surviving purely on fixing, modding, editing and tweaking PHP files from popular CMS's as that is what most people are pitching since it is visually understandable for end users. 

Jason - Do you by any chance have this for Python 2.7 or can port it over to 2.7 without much work? It would really bring a lot of cool things to the Python ecosystem. Personally I am happy to see something like this take off, as it will allow more people to be introduced to Python/web without having to take long tutorials about MVC ideology. Does your mod allow you do run extensions such as .psp in the browser? What do you call your extension? And is the only logic separation in your HTML the triple quote to type Python code? 

Graham Dumpleton

unread,
Oct 5, 2012, 7:50:10 PM10/5/12
to mod...@googlegroups.com
The simple reality is that Python code indenting is significant. This
does not work very well when it comes to trying to embed Python code
inside of HTML. It is too easy, with editors not helping, to screw up
the indenting of the Python code and thereby break your page without
the person writing the page even realising how they have managed to
break it.

So Python being a generic programming language and not specifically
designed as a web language like PHP was, will always make code
embedding hard due to that one specific issue.

Yes PSP in mod_python did allow it, but it was still a right pain to use.

Graham
> https://groups.google.com/d/msg/modwsgi/-/Rs1sqr3JDOgJ.

Jason Garber

unread,
Oct 5, 2012, 10:02:52 PM10/5/12
to mod...@googlegroups.com
Thanks for your comments Graham, and as always, thank you for providing mod_wsgi to make all of these other things possible.

I'll take the discussion with Marc off-list.
Reply all
Reply to author
Forward
0 new messages