Question

I want to format a .py file (that generates a random face every time the page is refreshed) using HTML, in the Terminal, that can run in a browser. I have chmod`ed it so it should work, but whenever I run in it in a browser, I get an internal service error. Can someone help me figure out what it wrong?

#!/usr/bin/python
print "Content-Type: text/html\n"
print ""

<!DOCTYPE html>
<html>
<pre>

from random import choice

def facegenerator():
  T = ""
  hair = ["I I I I I","^ ^ ^ ^ ^"]
  eyes = ["O   O"," O O ",]
  nose = ["  O  ","  v  "]
  mouth = ["~~~~~","_____","-----"]
  T += choice(hair)
  T += "\n"
  T += choice(eyes)
  T += "\n"
  T += choice(nose)
  T += "\n"
  T += choice(mouth)
  T += "\n"
  return T

print facegenerator()
</pre>
</html>

The code works in IDLE, but I can`t it to work on a webpage. Thanks in advance for any help!

Was it helpful?

Solution

This is neither valid HTML nor valid Python. You can't simply mix in HTML tags into the middle of a Python script like that: you need at the very least to put them inside quotes so that they are a valid string.

#!/usr/bin/python
print "Content-Type: text/html\n"
print """

<!DOCTYPE html>
<html>
<pre>
"""
def ...

print facegenerator()
print """</pre>
</html>"""

OTHER TIPS

You need a templating engine like jinja to have this http://jinja.pocoo.org/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top