0

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!

0

2 Answers 2

2

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>"""
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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.