DzITC
DzITC

Reputation: 879

Python: Flask passing values to javascript

So basically I'm doing google map and I need to add some markers, to do that I need to pass some values I did some research but many of them are executed by pressing a button, I'm really green on Javascript. My python code:

@app.route('/<api_key>')
def main_screen2(api_key=None):
    try:
        api_key = api_key or 'None'
        Reach_sr = Back_End()
        Reach_sr.GetCameras_Info(api_key)
        camera2_title = 'TESTTTaaaazzz'
        camera2_lat = 15.737769
        camera2_log = 40.25693
        return render_template('MAIN_index.html', camera2_title=camera2_title, camera2_lat=camera2_lat, camera2_log=camera2_log )
    except Exception as error:
        print(error) 

And the Javascript code:

function myFunction() {
  var camera2_log = parseFloat("{{ camera2_log }}")
  var camera2_lat = parseFloat("{{ camera2_lat }}")
}

myFunction()

marker2 = new google.maps.Marker({
  map: map,
  draggable: false,
  title: '{{camera2_title}}',
  icon: "https://developers.google.com/maps/documentation/javascript/examples/full/images/info-i_maps.png",
  animation: google.maps.Animation.DROP,
  position: {lat: camera2_lat, lng: camera2_log}
});

The title fits the name correctly, but I have no idea how to also add numbers

Upvotes: 1

Views: 232

Answers (2)

Eric
Eric

Reputation: 409

Surround your variables in double curly brackets so Jinja2 knows to fill them in using the variables passed to render_template, e.g.

position: {lat: {{ camera2_lat }}, lng: {{ camera2_log }}}

Upvotes: 0

DzITC
DzITC

Reputation: 879

I made a solution to pass a value in a string for example - "5" In the Javascript i did this:

var X1= parseFloat("{{X1_frompython}}", 10);
var X2= parseFloat("{{X2_frompython}}", 10);

This will convert a string into a float, and to access it use - X1, X2

Upvotes: 1

Related Questions