我正在尝试为新项目构建框架代码设置。 在这种情况下,我需要使用从“ flask”导入的jsonify()来生成str-> json。 但是,当我尝试执行此操作时,出现RunTimeError:“在应用程序上下文之外工作”。
当我使用google时,我看到很多人实际上使用Flask创建了一个应用程序实例,但是我不需要一个应用程序实例。我见过人们无需进行应用程序初始化就可以使它正常工作(app = Flask( name ))。
任何人都可以解释我在做什么错吗?
这是我的控制器:
# Standard library imports
import requests
import json
import logging
# Third party imports
from flask import Flask, request, jsonify
# Internal codebase
from testing import TestObjects as testObj
from core import RequestHandler as rh
from intent import Heartbeat as hb
from intent import CenterCapacity as cc
class Controller():
def __init__(self):
# Key is the naming for action internally, value is the actual syntax for the action, mathcing the DialogFlow configuration.
# If any actions change in DialogFlow, they should be corrected in the dictionary aswell.
self.actionValues = {
'getCenterCapacity': 'get.center.capacity', 'heartbeat': 'heartbeat'}
def main(self, request):
self.action = rh.getActionFromRequest(request)
print(self.action)
if self.action == self.actionValues.get('getCenterCapacity'):
print(cc.getCenterCapacity())
elif self.action == self.actionValues.get('heartbeat'):
print(hb.emptyHeartbeat())
if __name__ == '__main__':
c = Controller()
c.main(testObj.getCapacityObj())
c.main(testObj.getHeartbeatObj())
这是引起问题的我的意图“心跳”。
# Third party imports
from flask import jsonify
def emptyHeartbeat():
print("You entered the heartbeat method!")
msg = {
"fulfillmentMessages": [
{
"text": {
"text": [""]
}
}
]
}
return jsonify(msg)