tags:

views:

95

answers:

1

I'm new to Python and CGI, so this may be trivial. But I'd like Python to produce a block of HTML whenever a visitor loads a page.

<html>
<body>
<!-- Beautiful website with great content -->

<!-- Suddenly... -->
<h1> Here's Some Python Output </h1>
<!-- RUN PYTHON SCRIPT, display output -->

</body>
</html>

Using a Python script that, for simplicity, looks something like this:

#!/usr/bin/python
print "Content-type: text/html"
print 
print "<p>Hello world!</p>"

Most resources I was able to find demonstrate how to produce an ENTIRE webpage using a Python script. Can you invoke a Python script mid-page to produce JUST an HTML snippet?

+3  A: 

Use AJAX client-side, or templates server side.

A template will allow you to keep most of your page static, but use a server-side scripting language (like Python) to fill in the dynamic bits. There are lots of good posts on Python template systems on Stackoverflow. Here's one

AJAX will allow you to update a page after it initially loads with results from a server.

Triptych
Yeah, Python is not PHP. Embedding code in HTML is bad style and the fast road to maintenance nightmares.
mikl
Thanks for the advice everyone. AJAX seems like a good place to start.Similar example for those interested:http://www.degraeve.com/reference/simple-ajax-example.php
Derek