本文整理汇总了Python中flask_restful.marshal_with方法的典型用法代码示例。如果您正苦于以下问题:Python flask_restful.marshal_with方法的具体用法?Python flask_restful.marshal_with怎么用?Python flask_restful.marshal_with使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask_restful
的用法示例。
在下文中一共展示了flask_restful.marshal_with方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: listen_to_web
# 需要导入模块: import flask_restful [as 别名]
# 或者: from flask_restful import marshal_with [as 别名]
def listen_to_web(args, max_positions, task, process_batch):
# initialize web app
app = Flask(__name__, static_folder='')
api = Api(app)
# register route for web server
# a simple form page
@app.route('/form')
def form():
input = request.args.get('input', '')
inputs = [input]
results, outputs = process_inputs(args, inputs, max_positions, task, process_batch)
return render_template('form.html', input=input, outputs=outputs)
# a dynamic web app with static resource
@app.route('/')
def index():
return render_template('index.html')
@app.route('/static/<path:path>')
def send_static(path):
return send_from_directory('templates/static', path)
# a JSON api
resource_fields = {
'iteration': fields.Integer,
'src_str': fields.String, 'hypo_str': fields.String,
'hypo_score': fields.Float, 'pos_scores': fields.Float, 'gleu_scores': fields.Float,
'fluency_scores': fields.Float, 'alignments': fields.Float,
'hypo_score_str': fields.String, 'pos_scores_str': fields.String, 'gleu_scores_str': fields.String,
'fluency_scores_str': fields.String, 'alignments_str': fields.String
}
class API(Resource):
@marshal_with(resource_fields)
def get(self, input):
inputs = [input]
results, outputs = process_inputs(args, inputs, max_positions, task, process_batch)
# return outputs # raw string outputs
return results # json
# register routes for API
api.add_resource(API, '/api/<string:input>')
# listen with web server
print('server running at port: {}'.format(args.port))
http_server = WSGIServer(('', args.port), app)
http_server.serve_forever()