我在Python上使用Twitter API来获取城市的当前趋势标签,然后我应该将结果(作为数组)传递给我的HTML文件,我正在研究结果的可视化使用JavaScript。
所以,我尝试了多种解决方案,但我认为那个烧瓶是最简单的。
这是我的python代码(hashtags.py):
project
- views
-- site
--- index.php
--- about.php
--- (more files)
-- blog
--- index.php
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionBlog()
{
// not working
return $this->render('blog/index');
}
}
这是我的JavaScript代码(map.html)
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)
我试着看我的控制台,看它是否会打印趋势标签,但这就是我得到的:
它只打印单词(趋势)而没有任何结果。 我错过了什么?
更新
我对python中的代码非常肯定,因为我试图打印:
<!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('{{ trends }}');
console.log(hash); }
getHashtags(); }
}],
});
</script>
</body>
</html>
并打印带有趋势值的map.html。
答案 0 :(得分:0)
请修复# Store the links we need in a list
links_to_keep = []
with open("Anilinks.txt", "r") as f:
for line in f.readlines():
if 'solidfiles.com' in line:
links_to_keep.append(line)
# Write all the links in our list to the file
with open("Anilinks.txt", "w") as f:
for link in links_to_keep:
f.write(link)
中的缩进,以便我们了解您分配值的位置以及您拨打hashtags.py
的位置。
但要回答你的问题:
render_template
与变量''
一起使用,因为您希望将对象传递给{{ trends }}
,而不是字符串。JSON.stringify
过滤器。map.html:
safe