I'm trying to create a wordcloud with the Google's visualization sample:
<link rel="stylesheet" type="text/css" href="http://visapi-gadgets.googlecode.com/svn/trunk/wordcloud/wc.css"/>
<script type="text/javascript" src="http://visapi-gadgets.googlecode.com/svn/trunk/wordcloud/wc.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<div id="wcdiv"></div>
<script type="text/javascript">
google.load("visualization", "1");
google.setOnLoadCallback(draw);
function draw() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Text1');
data.addRows(160);
{{datamade}}
var outputDiv = document.getElementById('wcdiv');
var wc = new WordCloud(outputDiv);
wc.draw(data, null);
}
</script>
I'm creating {{datamade}} in my main.py file, then passing it as a template variable:
tweets1 = []
fetched = urlfetch.fetch("http://api.twitter.com/1/statuses/user_timeline.json?screen_name="+tweets.username+"&count=200")
statustext = json.loads(fetched.content)
for tweetInfo in statustext:
tweets1.append(tweetInfo["text"])
datamake = []
n = 1
for n in range(160):
tweet = tweets1[n]
datamake.append("data.setCell("+str(n)+", 0, '"+tweet+"');")
datamade = '<br>'.join(datamake)
content_values = {
'datamade':datamade,
'username':tweets.username,
}
When I print the {{datamade}}, I see the correct Javascript code. And when I hardcode the values into my statuspage.html, the javascript executes correctly. But when I pass the variable directly into the javascript, the javascript does not execute properly.
It my javascript executing prior to the template value is passed? Not sure how to adjust for this. I'd appreciate any advice.
Disclaimer: I'm a total newb.
Thank you! Jake
'datamade': SafeString(datamade)instead of'datamade': database. Django could be escaping your javascript/html, something that using a SafeString would prevent.