0
所以我寫了一個python應用程序來獲取highcharts的股票數據。我在終端Highcharts not rendering
127.0.0.1 - - [28/Mar/2013 13:47:02] "GET/HTTP/1.1" 200 -
127.0.0.1 - - [28/Mar/2013 13:47:02] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [28/Mar/2013 13:47:15] "GET /json/msft/?startdate=2010-01-01?enddate=2011-01-01 HTTP/1.1" 200 -
這裏得到這個是我的javascript代碼
<script type="text/javascript">
function stock() {
console.log('pass 1')
url = 'http://127.0.0.1:5000/json/' + document.getElementById("ticker").value + '/?startdate=' + document.getElementById("startdate").value + '?enddate=' + document.getElementById("enddate").value
$.getJSON(url, function(data) {
console.log('pass 2')
// split the data set into ohlc and volume
var ohlc = [],
volume = [],
dataLength = data.length;
for (i = 0; i < dataLength; i++) {
ohlc.push([
data[i]["date"], // the date
data[i]["open"], // open
data[i]["high"], // high
data[i]["low"], // low
data[i]["close"] // close
]);
volume.push([
data[i]["date"], // the date
data[i]["volume"] // the volume
])
}
// set the allowed units for data grouping
var groupingUnits = [[
'week', // unit name
[1] // allowed multiples
], [
'month',
[1, 2, 3, 4, 6]
]];
// create the chart
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: 'AAPL Historical'
},
yAxis: [{
title: {
text: 'OHLC'
},
height: 200,
lineWidth: 2
}, {
title: {
text: 'Volume'
},
top: 300,
height: 100,
offset: 0,
lineWidth: 2
}],
series: [{
type: 'candlestick',
name: 'AAPL',
data: ohlc,
dataGrouping: {
units: groupingUnits
}
}, {
type: 'column',
name: 'Volume',
data: volume,
yAxis: 1,
dataGrouping: {
units: groupingUnits
}
}]
});
});
}
</script>
併爲JSON請求
@app.route('/json/<ticker>/', methods = ['GET'])
def json_route(ticker):
startdate = request.args.get('startdate','')
enddate = request.args.get('enddate','')
check = datecheck(startdate,enddate)
if check != 'pass':
return check
check = datacheck(ticker,startdate,enddate)
if check != 'got data':
urldata(ticker)
print '-----------------data-----------------'
conn = engine.connect()
entries = conn.execute(select([Stocks]).where(Stocks.symbol == ticker).where(Stocks.date.between(startdate,enddate))).fetchall()
stocklist = []
print entries
for x in entries:
stocklist.append({
'volume': float('%f' %x[7]),
'high': float('%.2f' %x[4]),
'open': float('%.2f' %x[3]),
'low': float('%.2f' %x[5]),
'close': float('%.2f' %x[6]),
'date': str(x[2]),
'stock': x[1],
})
conn.close()
return json.dumps(stocklist)
我的Python代碼我在做什麼錯?我認爲getjson將是可行的,如果它在同一個域本地主機上。只有console.log('pass one')在檢查元素時工作並顯示在控制檯中。通過二是永遠不會被擊中。
雖然它不會再給我一個錯誤,但它也不會從url中獲得json。需要它在本地主機上工作。 – 2013-03-27 21:07:31
不知道這是否會有所幫助,但是flask有jsonify的輔助函數用於json響應,它將添加相應的內容類型標頭:http://flask.pocoo.org/docs/api/#returning-json – Smoe 2013-03-27 22:17:48
@Smoe運作良好,但仍然沒有達到二傳或呈現高位 – 2013-03-28 17:54:38