lundi 19 février 2018

Passing Python results (array) to JavaScript

I'm working with Twitter API on Python to get the current trending hashtags of a city, then I should pass the results (as an array) to my HTML file where I'm working on visualizing the results using JavaScript.

So, I've tried multiple solutions but I thought that flask was yet the easiest.

This is my python code (hashtags.py):

import tweepy
import json
from flask import Flask, request, render_template

consumerKey = "x"
consumerSecret = "x"
accessToken = "x"
accessTokenSecret = "x"

auth = tweepy.OAuthHandler("x", "x")
auth.set_access_token("x", "x")
api = tweepy.API(auth)

app = Flask(__name__)
@app.route('/map/')
@app.route('/map/<trends>')
def trend_hash(id):
    for location in api.trends_place(id):
     for trend in location["trends"] :
            print " - %s" % trend["name"]
            trends = trend["name"]
        return render_template("map.html", trends=json.dumps(trends)) 

trend_hash(1939753)

This is my JavaScript code (map.html)

<!DOCTYPE html>
 <html>
  <head>
   <link rel="stylesheet" href="https://www.amcharts.com/lib/3/ammap.css" type="text/css" media="all" /> 
   <script src="https://www.amcharts.com/lib/3/ammap.js"></script> 
   <script src="https://www.amcharts.com/lib/3/maps/js/saudiArabiaHigh.js"></script>
  </head>

  <body>
   <div id="chartdiv"></div> 
     <script type="text/javascript"> 

     var map = AmCharts.makeChart("chartdiv", {
      "type": "map", 
      "dataProvider": { 
      "map": "saudiArabiaHigh", 
      "getAreasFromMap": true }, 
      "areasSettings": { 
      "selectedColor": "#ffffff" }, 
      "listeners": [ {
       "event": "init",
       "method": function (e) {
             var chart = e.chart;

       function getHashtags() {
        var hash = JSON.stringify('');
        console.log(hash); }

       getHashtags();  }
        }],
           });

     </script>
      </body>
       </html>

I tried to watch my console to see if it will print the trending hashtags, but this is what I got: enter image description here

it just prints the word (trends) without any results. What am I missing?

Thank you in advance




Aucun commentaire:

Enregistrer un commentaire