0

The code bellow is a part of a Google App Engine project I am building with Python. I am taking input of x and y(not seen here), and printing it here as a table with its x,y coordinate as the text in the table. Additionally, I need to make it so that when you click on a cell of the table, it calls a function which will return a string for it to pop up with. For simplicity at this time it's just the X and Y again.
So in short, I need a way to pass a variable from Python to HTML so it can be passed to javascript. How can I do that?

def testFunc(self, x, y):
        return str(x) + " , " + str(y)
    def drawTable(self, row , col):
        write = self.response.write
        write(row)
        write(col) 
        write("<html><body><script> function tableInfo() { alert(\""+ self.testFunc(x, y)+""\");}</script><table>")
        for y in range(row):
            write("<tr>")
            for x in range(col):
                cell = "<td bgcolor=\"#00FF00\" onclick = \"tableInfo(x, y)\">" + str(x) + " " + str(y) + "</td>"
                if x % 2 == 0:
                    cell = "<td bgcolor=\"#FF0000\" onclick = \"tableInfo(x, y)\">" + str(x) + " " + str(y) + "</td>"
                write(cell)
            write("</tr>")    
        write("</table></body></html>")  
3
  • 1
    use a jinja template to create dynamic HTML. See getting started with Python on GAE and use jQuery to get the elements. Commented Jun 12, 2013 at 21:41
  • Is it wrong that in my 2ish months of learning GAE that I've never heard of jinja? Commented Jun 12, 2013 at 22:10
  • If you are new to GAE, the Python27 getting started quide is important, because of webapp2. Jinja2 is the preferred templating library for GAE with Python27. I can also recommend the udacity webdevelopment lessons. They also use GAE Python27. Commented Jun 13, 2013 at 10:46

1 Answer 1

1

You can write Javascript, substituting values from Python:

write("""<html><head>
    <script>
    var x = %d, y = %d;
    </script>
    </head>""" % (x, y))

Now you can use x and y in other Javascript code.

Sign up to request clarification or add additional context in comments.

1 Comment

... but don't. Use a template language, like Jinja.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.