0

我一直在努力的基礎上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問題。我只是不知道它會是什麼。

很多預先感謝。

+1

我認爲有一些服務器端處理的'的地方去上輸出數據{{數據}}' ,對嗎?如果是這樣,那麼問題在於你正在創建一個變量'countdata = {{data}}',但是當你創建DataTable對象時引用一個變量'query_response'。 – asgallant

+0

@asgallant是的,有。它應該檢索查詢結果並將它們放在'{{data}}'的位置。但我得到的只是一個空白的等式:'countdata ='。所以,我完全困惑。感謝您的評論,它給了我一些東西來看/搗鼓。 –

+0

在這種情況下,在'渲染(模板,query_response)','query_response'不會有鑰匙(「數據」)的模板期望。 –

回答

0

「查看源文件」,再加上一些(螢火蟲,Chrome開發者工具),這將告訴你在你的JavaScript語法錯誤是你的朋友。看看你提供了什麼,我猜想擴大{{ data }}將導致語法錯誤。正如asgallant指出的那樣,您正在設置countdata,但改用query_response

由於您的數據是日期和計數,因此您不會遇到實體轉義問題,但是如果您擴展查詢以包含其他內容,則會遇到{{ data }}被HTML轉義的問題,它需要逃避JS。要處理該問題,您需要使用escapejs過濾器。

+0

首先,謝謝。事實上,它不會拋出語法錯誤或任何東西。我忘記補充說,當我查看源代碼時,我得到了'query_response =' 所以沒有任何回報。 **這讓我難以置信地感到困惑。 –

+0

糟糕。留下應該去過這裏的原帖的評論。 –

0

在您的代碼:

countdata = { 'cols': ({'id':columnNameDate, 'label':columnNameDate, 'type':'string'}, 
    {'id':columnNameVal, 'label':columnNameVal, 'type':'number'})} 

它應該是:

countdata = { 'cols': [{'id':columnNameDate, 'label':columnNameDate, 'type':'string'}, 
    {'id':columnNameVal, 'label':columnNameVal, 'type':'number'}]}