【问题标题】:Python passing arguments to function FlaskPython将参数传递给函数Flask
【发布时间】:2019-06-29 02:02:17
【问题描述】:

我正在编写一个 Flask 应用程序脚本,并且遇到了一个函数,它在独立脚本中接受 Flask 应用程序之外的参数和函数,但不接受我在 Flask 应用程序内部传递给它的“TFEVT_node_id”参数。这是烧瓶代码:

#################################################
# Flask Setup
#################################################
#app = Flask(__name__, static_url_path='')
app = Flask(__name__)
CORS(app)
#################################################
#global variables
TFEVT_node_id = ''
token = ''
#################################################
# CityIQ API calls
#################################################
def get_token():
    print("Get Token")
    url = 'https://auth.aa.cityiq.io/oauth/token'
    querystring = {"grant_type":"client_credentials"}

    response = requests.get(url, auth=HTTPBasicAuth(client,secret), params=querystring).json()
    token = response['access_token']
    print("Token Received")
    return token

#given a specific subasset this function returns the CityIQ traffic events for a given period of time in minutes
def get_traffic(TFEVT_node_id, lookback_minutes):
    url_string = '/events'
    url = "https://sandiego.cityiq.io/api/v2/event/assets/"+TFEVT_node_id+url_string
    ts = datetime.datetime.now().timestamp()
    CityIQ_Current_TS = int(ts*1000)
    CityIQ_TS_Calc_TS =  datetime.datetime.now() - timedelta(minutes=lookback_minutes)
    CityIQ_Starttime_TS =  int((ts-(lookback_minutes*60))*1000)
    querystring = {"eventType":"TFEVT","startTime":CityIQ_Starttime_TS,"endTime":CityIQ_Current_TS,"pageSize":"100"}
    payload = ""
    headers = {
            'Authorization': "Bearer {}".format(token),
            'Predix-Zone-Id': "SD-IE-TRAFFIC",
            'cache-control': "no-cache",
            }
    response = requests.request("GET", url, headers=headers, params=querystring).json()
    return(response)

#give it an CityIQ node ID and it will return the asset TFEVT child .
def get_asset_TFEVT(node_id):
    url = "https://sandiego.cityiq.io/api/v2/metadata/assets/"+node_id+"/subAssets"
    payload = ""
    headers = {
        'Authorization': "Bearer {}".format(token),
        'Predix-Zone-Id': "SD-IE-ENVIRONMENTAL",
        'cache-control': "no-cache",
        }
    response = requests.request("GET", url, data=payload, headers=headers).json()
    for i in response['assets']:
        if any('TFEVT' in i for i in [i][0]['eventTypes']):
            global TFEVT_node_id
            TFEVT_node_id = ([i][0]['assetUid'])
    return(TFEVT_node_id)

#test def that should be removed in production
def test(TFEVT_node_id, lookback_minutes):
    found_cars = get_traffic(TFEVT_node_id, lookback_minutes)
    print(found_cars)
    return(found_cars)

#################################################
# Flask Routes
#################################################
@app.route('/')
def hello_world():
    global token
    token = get_token()
    return 'documentation comming soon!'

@app.route('/test/<string:node_id>')
def go(node_id):
    global token
    token = get_token()
    global TFEVT_node_id
    TFEVT_node_id = get_asset_TFEVT(node_id)
    cars_list = []
    lookback_minutes = 15
    env_output = {}
    ccars = test(TFEVT_node_id, lookback_minutes)
    cars_list.append(ccars)
    env_output.update({'Cars' : cars_list})

if __name__ == '__main__':
    app.run()

再次,当这些函数在 Flask 应用程序之外运行时,我得到了预期的结果。在 Flask 应用程序内部(运行上面的代码)返回一个 TypeError: The view function did not return a valid response。我已经将其追溯到 gettraffic 函数中的 404 响应,其中回溯分钟变量正在通过,但 TFEVT_node_id 没有。

我是 Python 新手。

【问题讨论】:

    标签: python-3.x flask arguments


    【解决方案1】:

    您的go() 函数没有返回值。预计会有回应。请参阅:About responses

    【讨论】:

    • 感谢您的回复,但这不是问题......即使 go() 函数返回,它仍然提供相同的错误。
    猜你喜欢
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2013-07-24
    相关资源
    最近更新 更多