2015-09-17 39 views
1

我確定我錯過了教程中真正明顯的一些東西,但試圖通過實現一個簡單的腳本和html頁來學習webpy框架。我在命令行運行的主要腳本啓動了「服務器」(我沒有在Index類的工作/設置中正確運行POST函數,但只是試圖讓GET函數工作正確與開始):Webpy顯示pyc文件路徑而不是打印語句

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文件中的功能。

回答

2

您的初始模塊導入具有打印到標準輸出的副作用,因此它可能在您啓動應用程序時寫入日誌。

其次,您將module對象傳遞給您的模板,然後直接在您的標記中輸出它,這就是爲什麼它正在打印模塊的__repr__

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

好吧,所以只是因爲腳本中有一個打印語句正在導入,即使我試圖在html代碼中調用或使用該函數,也沒有設置輸出print語句到實際的HTML頁面? – SeanJarp

+0

正確。它被稱爲[導入副作用](http://chrismorgan.info/blog/say-no-to-import-side-effects-in-python.html)。如果您希望在模板中顯示,那麼您必須將其放入函數中,並將函數的返回值分配給模板變量。 –