How to send the value of a Python variable to JavaScript in HTML -
i using google app engine display google map on web page.
i want bring latitude , longitude in database , display on map. this, need pass imported latitude , longitude javascript in html. have tried several ways useless. (ex. {{variable}}
useless.)
how best debug or otherwise proceed on this?
class map(webapp2.requesthandler): db = connect_to_cloudsql() cursor = db.cursor() cursor.execute("""select latitude,longitude user;""") data=cursor.fetchone() lat=data[0] lng=data[1] formstring = """ <!doctype html> <html lang="ko-kr"> <head> <meta charset="utf-8"/> <meta name="google-site-verification" content="9eqlgizcmwfo7xacse4sbnz_t0guladyef9bco0dy3k"/> </head> <body class> <style> #map { width: 80%; height: 400px; background-color: grey; } </style> <div style="position:relative;width:1080px;margin:0 auto;z-index:11"> <div class="container" role="main"> <div id="map"></div> <br><br> <script async defer src="https://maps.googleapis.com/maps/api/js?key=mykey&callback=initmap"> </script> <script> function initmap() { var uluru = {lat: {lat} , lng: {lng} }; var map = new google.maps.map(document.getelementbyid('map'), { zoom: 4, center: uluru }); var marker = new google.maps.marker({ position: uluru, map: map }); } </script> </div> </div> </body> </html> """
take @ string formatters. grow web app, you'll want switch using templates because more robust. webapp2 provides support jinja2 templates, used , make life easier in long run.
but in short run, use string formatters:
formstring = """ <!doctype html> ... var uluru = {lat: %f , lng: %f }; var map = new google.maps.map(document.getelementbyid('map'), { ... """ % (lat, lng) # assuming lat/lng floats, use %s instead of %f strings
alternatively, use "new way":
formstring = """<html.......{lat: {:f}, lng: {:f}...""".format(lat, lng)
again, @ string formatters.
Comments
Post a Comment