【问题标题】:Webpy displaying pyc file path instead of print statementWebpy显示pyc文件路径而不是打印语句
【发布时间】:2015-09-17 17:51:27
【问题描述】:

我确定我在教程中遗漏了一些非常明显的东西,但我尝试通过实现一个简单的脚本和 html 页面来学习 webpy 框架。

这是我从命令行运行以启动“服务器”的主脚本(我在 Index 类中没有正确工作/设置的 POST 函数,但只是尝试使用 GET 函数开始正常工作):

import web
import sys
sys.path.insert(0, '/Users/seanjarp/PythonLearnings/projects/GAME/ChipChippersonGame') 
from ChipChippersonGame import ChipChippersonGame

urls = (
    '/testhtml', 'Index'
) 

app = web.application(urls, globals())
render = web.template.render('templates/')

class Index(object):
    def GET(self):
        return render.testhtml(ChipChippersonGame)
       # return render.testhtml()
    #def POST(self):
    #    form = web.input(playerinput="HiChip")
    #    user_response = ChipChippersonGame.next_scene(form)


if __name__ == "__main__":
    app.run()

上面的这个脚本是在另一个导演中导入另一个脚本,目前我已经简化为只有一个打印语句来保持简单(导入工作正常):

print "ChipChippersonGame script is being called"

testhtml 文件如下所示:

$def with (ChipChippersonGame)

<html>
    <head>
        <title>What's Up!</title>
    </head>
<body>

$if ChipChippersonGame:
    $ChipChippersonGame

$else:
   <em>Script is not being called!</>

</body>
</html>

显然,例如,当我将其加载到浏览器中时:

http://0.0.0.0:4141/testhtml

它将以下内容打印到浏览器:

<module 'ChipChippersonGame.ChipChippersonGame' from '/Users/seanjarp/PythonLearnings/projects/GAME/ChipChippersonGame/ChipChippersonGame`/ChipChippersonGame.pyc

我做错了什么导致ChipChippersonGame.py 脚​​本中的打印语句无法打印到浏览器?

我假设我搞砸了我在 testhtml.html 文件中调用或设置函数的方式。

【问题讨论】:

    标签: python python-2.7 web.py web-frameworks


    【解决方案1】:

    您的初始模块导入具有打印到标准输出的副作用,因此它可能在您启动应用程序时被写入日志。

    其次,您将 module 对象传递给您的模板,然后将其直接输出到您的标记中,这就是它打印出模块的 __repr__ 的原因。

    $if ChipChippersonGame:
        $ChipChippersonGame
        ^__________________this is a module object hence <module 'ChipChippersonGame.ChipChippersonGame' from
    

    【讨论】:

    • 好的,所以只是因为我正在导入的脚本中有一个 print 语句,即使我试图在 html 代码中调用或使用该函数,它也没有设置为输出将语句打印到实际的 html 页面?
    • 正确。它被称为import side-effect。如果您希望它显示在模板中,则必须将其放入函数中,并将函数的返回值分配给模板变量。
    猜你喜欢
    • 2022-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多