Python flask_restful.marshal_with() Examples

The following are 1 code examples of flask_restful.marshal_with(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask_restful , or try the search function .
Example #1
Source File: interactive.py    From pytorch-human-performance-gec with Apache License 2.0 4 votes vote down vote up
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()