I am working on a Python code that generates an HTML page using a template. When a click event happens, I need to read data from an Excel file and display the results, possibly in a pop-up window. My plan is to use xlrd
to read the Excel file and make an Ajax request
. This request will provide the URL of a Python file that reads the Excel file's content. I have chosen to utilize Prototype
's Ajax.Updater
method for this task.
The purpose of Ajax.Updater is to replace the response
item with the response from the Python file named display.py
.
I have two questions:
How do we send the response back from Python? This may be a basic question, but I couldn't find clear information on this topic. Do I need to define a function and use
return
to pass the response value? Should I useprint
orsys.stdout.write()
? As a beginner, I'm unsure about the best approach. Many resources mention usingjson
; should I implement that here? Could someone explain why it is necessary? (Apologies if this question seems naive)Is there a more efficient way to achieve my objective?
The relevant code snippet is as follows:
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')
This is how my Python file display.py
looks like:
import sys
sys.stdout.write("Content-type: text/html")
response = "dadada"
sys.stdout.write(response)
sys.stdout.write("</br>")
I am working on a Windows platform and using Prototype version 1.7.1.
Thank you,
Megh