Python列表不可调用

时间:2017-05-14 03:02:57

标签: python json python-3.x flask

我在Flask应用程序中有一个返回一些API数据的函数

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调用中返回,而是我得到了神秘的

TypeError: 'list' object is not callable

现在,如果我注释掉最后一行,并取消注释jsonify行,那就行了。 解析前端返回的对象是一件痛苦的事。我想退回listOfVehicles,但我不知道为什么它不让我。

编辑: 我忘记了上面定义的Train类:

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

1 个答案:

答案 0 :(得分:0)

问题是您的代码正在尝试返回list。 Flask不希望从视图函数返回list对象,但是response对象,请参考official docjsonify正在工作,因为它返回一个JSON响应对象。

如果您只想返回listOfVehicles,可以将其分配给变量,然后将其渲染为模板,如下所示:

from flask import render_template

def get_trains():
    train_list = fetch_trains()

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

在模板index.html中:

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

它将循环显示train_list,全部显示在index.html