2017-05-14 3 views
0

私はむしろ、私は謎めいPythonのリストは

TypeError: 'list' object is not callable 
を取得し、

def fetch_trains(): 
    r = requests.get('https://developer.trimet.org/ws/v2/vehicles/appID/[app id goes here]') 
    vehicles = r.json() 
    listOfVehicles = [] 
    for vehicle in vehicles['resultSet']['vehicle']: 
     if vehicle['type'] =='rail': 
      vehToAdd = Train() 

      vehToAdd.bearing = vehicle['bearing'] 
      vehToAdd.blockID = vehicle['blockID'] 
      vehToAdd.delay = vehicle['delay'] 
      vehToAdd.direction = vehicle['direction'] 
      vehToAdd.extraBlockID = vehicle['extraBlockID'] 
      vehToAdd.gararge = vehicle['garage'] 
      vehToAdd.inCongestion = vehicle['inCongestion'] 
      vehToAdd.latitude = vehicle['latitude'] 
      vehToAdd.longitude = vehicle['longitude'] 
      vehToAdd.lastLocID = vehicle['lastLocID'] 
      vehToAdd.offRoute = vehicle['offRoute'] 
      vehToAdd.signMessageLong = vehicle['signMessageLong'] 
      vehToAdd.time = vehicle['time'] 
      vehToAdd.tripID = vehicle['tripID'] 
      vehToAdd.vehicleID = vehicle['vehicleID'] 
      vehToAdd.nextLocID = vehicle['nextLocID'] 
      listOfVehicles.append(vehToAdd) 
    #return jsonify({'List of vehicles':listOfVehicles}) 
    return listOfVehicles 

上記listOfVehiclesは、APIの呼び出しで返されていないいくつかのAPIデータを返すフラスコアプリの機能を持って呼び出すことはできません

最後の行をコメントアウトして、jsonify行のコメントを外すと、それが機能します。 しかし、フロントエンドで返されたオブジェクトを解析するのは苦痛です。私はlistOfVehiclesを返すだけですが、私はなぜそれが私を放っていないのかわかりません。

編集: 私は上記で定義された列車のクラスを忘れてしまった:

class Train: 
    def __init__(self): 
     self.bearing = None 
     self.blockID = None 
     self.delay = None 
     self.direction = None 
     self.extraBlockID = None 
     self.garage = None 
     self.inCongestion = False 
     self.latitude = None 
     self.longitude = None 
     self.lastLocID = None 
     self.nextLocID = None 
     self.offRoute = False 
     self.signMessageLong = None 
     self.time = None 
     self.tripID = None 
     self.vehicleID = None 

    def __html__(self): 
     return { 
      "Bearing": self.bearing, 
      "BlockID": self.blockID, 
      "Delay": self.delay, 
      "Direction": self.direction, 
      "extraBlockID": self.extraBlockID, 
      "Garage": self.garage, 
      "inCongestion": self.inCongestion, 
      "Latitude": self.latitude, 
      "Longitude": self.longitude, 
      "lastLocID": self.lastLocID, 
      "nextLocID": self.nextLocID, 
      "offRoute": self.offRoute, 
      "signMessageLong": self.signMessageLong, 
      "time": self.time, 
      "vehicleID": self.vehicleID, 
      "TripID": self.tripID 
     } 

EDIT2: コールのfetch_trains():

@app.route('/') 
@app.route('/get_trains', methods=['GET', 'OPTIONS']) 
def get_trains(): 
    train_list = fetch_trains() 

    return train_list 
+0

「fetch_trains」の呼び出し元に問題があるようです... –

+0

私はこの問題があなたがここに投稿したものの範囲外だと思います。 –

+0

どうですか?どのような道を取るべきですか? –

答えて

0

事はあなたのコードがlistを返すようにしようとしていることです。フラスコはlistオブジェクトをビュー機能から返すことを期待していませんが、responseオブジェクトはofficial docを参照してください。 jsonifyはJSON応答オブジェクトを返すときに機能しています。

あなただけlistOfVehiclesを返すようにしたい場合は、あなたがこのような場合、テンプレートにそれをレンダリングし、それを変数に割り当てることができますテンプレートindex.html

from flask import render_template 

def get_trains(): 
    train_list = fetch_trains() 

    #return train_list 
    return render_template('index.html', train_list=train_list) 

{% for train in train_list %} 
    {{train}}</br> 
{% endfor %} 

それがためになりますループはtrain_listを超え、すべてをindex.htmlに表示します。