我一直在努力的基礎上this codelab.我有,你在股票代號輸入文本框前端到我的App Engine應用程序種類(如說,AMZN或GOOG) ,它用來作爲標準,在後臺運行一個查詢,以谷歌的BigQuery,然後它應該在谷歌的可視化API line chart顯示在幾天的鳴叫計數。問題與App引擎谷歌可視化API
但是,根據我從網頁源代碼中所看到的,它不拉從BigQuery的查詢結果到數據模板變量{{數據}}。這是我的HTML代碼(稱爲index1.html),其中大部分是像代碼實驗室的:
<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Jibdan Analytics
</title>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
countdata = {{ data }}
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.DataTable(query_response);
// Create and draw the visualization.
var chart = new google.visualization.LineChart(document.getElementById('visualization'));
chart.draw(data, {title: "Tweets by Day by Ticker",
curveType: "function",
width: 800, height: 600}
);
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 800px; height: 640px;"></div>
</body>
</html>
我用HTML和JavaScript亂看的折線圖的谷歌代碼遊樂場代碼之後,但真正看起來問題在於該模板變量。
下面是相關的Python代碼了。第一種方法應該採用BigQuery響應並將其放入應由Visualization API攝取的格式中以生成折線圖。 get方法有查詢字符串運行並將結果呈現到模板index1.html中。非常像codelab:
class QueryHandler(webapp2.RequestHandler):
def _bq2line(self, bqdata):
logging.info(bqdata)
columnNameDate = bqdata['schema']['fields'][0]['name']
columnNameVal = bqdata['schema']['fields'][1]['name']
logging.info("Column Names=%s, %s" % (columnNameDate, columnNameVal))
countdata = { 'cols': ({'id':columnNameDate, 'label':columnNameDate, 'type':'string'},
{'id':columnNameVal, 'label':columnNameVal, 'type':'number'})}
countdata['rows'] = [];
logging.info(countdata)
for row in bqdata['rows']:
newrow = ({'c':[]})
newrow['c'].append({'v': row['f'][0]['v']})
newrow['c'].append({'v':row['f'][1]['v']})
countdata['rows'].append(newrow)
logging.info('FINAL COUNTDATA---')
logging.info(countdata)
self.response.out.write(countdata)
return json.dumps(countdata)
def get(self):
QUERY = """
SELECT STRFTIME_UTC_USEC(querydate, "%Y-%m-%d") AS tweet_date, COUNT(tweet_id) AS tweet_count
FROM [jibdantweetstream.tweetdata_09_21_2013]
WHERE gcs_load = true AND (REGEXP_MATCH(ticker, '""" + self.request.get('stock') + """'))
GROUP BY tweet_date
ORDER BY tweet_date
"""
try:
query_request = bigquery_service.jobs()
query = {'data': self._bq2line(bq.Query(QUERY, BILLING_ID)),
'query': QUERY}
query_response = query_request.query(projectId=BILLING_ID,
body=query).execute()
template = os.path.join(os.path.dirname(__file__), 'result1.html')
self.response.out.write(render(template, query_response))
finally:
self.response.out.write('<a href="/">Click here</a> to go back to the Search page. ')
所以,這就是我所擁有的。您會看到我有幾個self.response.out.write
語句,因爲我想查看是否收到帶有查詢結果的回覆。我得到的結果,所以我知道這不是一個OAuth2問題。我只是不知道它會是什麼。
很多預先感謝。
我認爲有一些服務器端處理的'的地方去上輸出數據{{數據}}' ,對嗎?如果是這樣,那麼問題在於你正在創建一個變量'countdata = {{data}}',但是當你創建DataTable對象時引用一個變量'query_response'。 – asgallant
@asgallant是的,有。它應該檢索查詢結果並將它們放在'{{data}}'的位置。但我得到的只是一個空白的等式:'countdata ='。所以,我完全困惑。感謝您的評論,它給了我一些東西來看/搗鼓。 –
在這種情況下,在'渲染(模板,query_response)','query_response'不會有鑰匙(「數據」)的模板期望。 –