简体   繁体   English

使用HTML格式化.py文件

[英]Formatting .py file using HTML

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. 我想在终端中使用HTML格式化.py文件(每次刷新页面时都会生成一个随机面孔),该文件可以在浏览器中运行。 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. 该代码在IDLE中有效,但我无法在网页上正常工作。 Thanks in advance for any help! 在此先感谢您的帮助!

This is neither valid HTML nor valid Python. 这既不是有效的HTML也不是有效的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. 您不能像这样简单地将HTML标记混入Python脚本的中间:您至少需要将它们放在引号中,以便它们是有效的字符串。

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

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

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

您需要像jinja这样的模板引擎才能拥有此http://jinja.pocoo.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM