我有一個使用模板生成HTML頁面的python代碼。當發生點擊事件時,我需要讀取Excel文件並顯示一些結果,可能會彈出。我以爲我可以使用xlrd
來閱讀Excel文件。我可以使用ajax request
來給出讀取excel文件的python文件的url
。我決定爲此使用Prototype
的Ajax.Updater
。Ajax.Updater和python對請求的響應
Ajax.Updater應該用來自python文件display.py
的響應替換response
項。
我有兩個問題 -
我們怎麼發回響應從蟒蛇?這可能是一個微不足道的問題,但我找不到解釋這部分的任何地方。我寫一個函數和
return
那個響應值嗎?或者使用print
?或者我應該使用sys.stdout.write()
?我是一個新手,我不明白這是如何工作的。這裏有很多問題顯示使用了json
。我應該使用它嗎?你能解釋爲什麼它需要嗎?(我是初學者,對不起,如果這聽起來很蠢)有沒有更好的方法來實現我的目標?
的代碼看起來是這樣的:
myprog.py
from string import Template
import webbrowser
template1 = Template(
"""
<html>
<head>
<script type = "text/javascript" src = "\javascript\prototype.js">
</script>
<script>
function displayResults()
{
new Ajax.Updater("response", "\display.py");
}
</script>
</head>
<body>
<table>
<tr>
<td id = "myid" onclick = displayResults()> $something</td>
</tr>
</table>
<p id = "response">replace this</p>
</body>
</html>
""")
d = template1.substitute(something = nothing)
f= open('filename.html','w')
f.write(d)
f.close()
webbrowser.open_new('filename.html')
我的Python文件display.py
看起來像這樣
import sys
sys.stdout.write("Content-type: text/html")
response = "dadada"
sys.stdout.write(response)
sys.stdout.write("</br>")
我的平臺是Windows。我正在使用新版本的Prototype(1.7.1)。
感謝
Megh
我現在正在使用'print',但點擊時仍然沒有任何反應。根據[tutorials](http://www.tutorialspoint.com/python/python_cgi_programming.htm),我沒有在'\ www \ cgi-bin \'中存儲'display.py'。這有什麼影響嗎?我應該爲此設置一些配置文件嗎? – user2511458
您可以在瀏覽器中訪問'/ display.py'網址嗎?怎麼了? –
更多信息和教程:https://wiki.python.org/moin/CgiScripts –