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?

2
  • What about people that want to run python scripts on ugly websites with trivial content? Commented Aug 18, 2009 at 17:47
  • How could you even run a python script "mod-page"? What does that even mean? Are you trying to write CGI as a big shell script with small pieces of Python on the shell script? Why make it so complex? Commented Aug 18, 2009 at 18:17

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.