2011-07-18 56 views
0

我將應用程序從cgi移動到web.py.我得到了結果,並通過cgi中的簡單打印語句動態打印它們。所以結果在頁面完成加載之前已經打印出來了。在web.py中,我有一個return語句,但無法弄清楚如何實現這一點。web.py中的動態打印

樣品CGI代碼:

print "<p><b>Displaying Search Results</b></p>" 
print "<table>" 
cmd = subprocess.Popen(["find", location ,"-name", fileName], stdout=subprocess.PIPE) 
for line in cmd.stdout: 
    tableData=line.rstrip("\n") 
    tableRow="<tr><td>tableData</td></tr>" 
    print tableRow 
print "</table>" 

,因爲它是生成上述代碼將在一次打印一行。如何爲web.py編寫相同的代碼?

回答

2

要麼使用你追加到和一個字符串最後返回:

s = "My page with..." 
s += "More data" 
for y in x: 
    s += "And this!" 
return s 

或者產生數據中所描述的能力,http://webpy.org/cookbook/streaming_large_files 有一些陷阱,你必須設置一些標題,但不能混合returnyield,因爲你使它成爲一個發電機。

設置標題的一部分,您可以使用:yield "Some more data",與您的CGI腳本中的print相似。

+0

非常感謝!!!!正是我在找什麼! – StuckAgain

1

儘管可以通過連接和單個返回替換print來緩解移植,但可以考慮使用模板引擎(web.py具有名爲Templetor的內置模板引擎)。

從演示文稿中分離邏輯允許您更改其中一個,而不用考慮另一個。