Skip to content

Instantly share code, notes, and snippets.

@VioletVivirand
Created April 25, 2017 08:48
Show Gist options
  • Save VioletVivirand/924db6d13c094df96f652c8527950f7c to your computer and use it in GitHub Desktop.
Save VioletVivirand/924db6d13c094df96f652c8527950f7c to your computer and use it in GitHub Desktop.
Flask marshal_with Demo
from flask import Flask, make_response
from flask_restful import Resource, Api, fields, marshal_with
import pandas as pd
# 初始化
app = Flask(__name__)
api = Api(app)
todo_fields = {
"col_one": fields.String(attribute="col1"),
"col2": fields.String
}
data = [[1,2],[3,4],[5,6]]
d = pd.DataFrame(data, columns=["col1", "col2"]).to_dict(orient="records")
# d =
# col1 col2
# 0 1 2
# 1 3 4
# 2 5 6
class JsonContent(Resource):
@marshal_with(todo_fields)
def get(self):
return d
# 添加一個 Endpoint
api.add_resource(JsonContent, '/')
if __name__ == '__main__':
app.run(debug=True, host="127.0.0.1", port=8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment