Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8133581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:39:26+00:00 2026-06-06T09:39:26+00:00

My app makes a call to an API that returns a dictionary. I want

  • 0

My app makes a call to an API that returns a dictionary. I want to pass information from this dict to JavaScript in the view. I am using the Google Maps API in the JS, specifically, so I’d like to pass it a list of tuples with the long/lat information. I know that render_template will pass these variables to the view so they can be used in HTML, but how could I pass them to JavaScript in the template?

from flask import Flask
from flask import render_template

app = Flask(__name__)

import foo_api

api = foo_api.API('API KEY')

@app.route('/')
def get_data():
    events = api.call(get_event, arg0, arg1)
    geocode = event['latitude'], event['longitude']
    return render_template('get_data.html', geocode=geocode)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:39:27+00:00Added an answer on June 6, 2026 at 9:39 am

    You can use {{ variable }} anywhere in your template, not just in the HTML part. So this should work:

    <html>
    <head>
      <script>
        var someJavaScriptVar = '{{ geocode[1] }}';
      </script>
    </head>
    <body>
      <p>Hello World</p>
      <button onclick="alert('Geocode: {{ geocode[0] }} ' + someJavaScriptVar)" />
    </body>
    </html>
    

    Think of it as a two-stage process: First, Jinja (the template engine Flask uses) generates your text output. This gets sent to the user who executes the JavaScript he sees. If you want your Flask variable to be available in JavaScript as an array, you have to generate an array definition in your output:

    <html>
      <head>
        <script>
          var myGeocode = ['{{ geocode[0] }}', '{{ geocode[1] }}'];
        </script>
      </head>
      <body>
        <p>Hello World</p>
        <button onclick="alert('Geocode: ' + myGeocode[0] + ' ' + myGeocode[1])" />
      </body>
    </html>
    

    Jinja also offers more advanced constructs from Python, so you can shorten it to:

    <html>
    <head>
      <script>
        var myGeocode = [{{ ', '.join(geocode) }}];
      </script>
    </head>
    <body>
      <p>Hello World</p>
      <button onclick="alert('Geocode: ' + myGeocode[0] + ' ' + myGeocode[1])" />
    </body>
    </html>
    

    You can also use for loops, if statements and many more, see the Jinja2 documentation for more.

    Also, have a look at Ford’s answer who points out the tojson filter which is an addition to Jinja2’s standard set of filters.

    Edit Nov 2018: tojson is now included in Jinja2’s standard set of filters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app that makes web api call , the rails app
I have a GWT app that makes an RPC call to a server to
I make call from my app with this code (i use webview because after
I currently have an iPhone application that makes a call to an API to
I have an app that makes SOAP calls. To keep the UI from blocking,
I'm developing an app that makes requests to a web service more often. What
I want to create an iPhone app which makes calls to a web service.
I'm making a simple app that makes some calculations. Some of these calculations are
I am creating a Rails 3 app that makes use of a nested form
I am working on a Rails 3.1 app that makes use of CoffeeScript. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.