--- responses: 200: description: current user info examples: json: { "name": "meteorix" } """ if current_user.is_authenticated: name = current_user.username else: name = "anonymous" return jsonify({"name": name}) @app.route("/post_or_get_example", methods=["GET", "POST"]) @swag_from("simple_get.yml", methods=["GET"]) @swag_from("simple_post.yml", methods=["POST"]) def post_or_get_example(): if request.method == "GET": get_input = request.args["get_input"] get_output = "output of " + get_input return jsonify({"get_output": get_output}) else: post_input = request.form["post_input"] post_output = "output of " + post_input return jsonify({"post_output": post_output}) if __name__ == "__main__": gevent_run(app)
type: file responses: 200: description: download file or error info examples: json: { "error": "...", "success": false} """ fs = request.files['file'] upload_dir = "uploads" if not os.path.exists(upload_dir): os.makedirs(upload_dir) filepath = os.path.join(upload_dir, fs.filename) fs.save(filepath) # example: call external programs out_path = filepath + ".copy" print(filepath, out_path) proc = sb.Popen(["cp", filepath, out_path], stdout=sb.PIPE, stderr=sb.STDOUT) out, err = proc.communicate() app.logger.info(out.decode("gbk")) if proc.returncode == 0: return send_file(os.path.abspath(out_path), as_attachment=True) else: return jsonify({"error": out, "success": False}) if __name__ == "__main__": gevent_run(app, port=5002)
# coding=utf-8 # Created by Meteorix at 2019/4/10 from flaskweb.app import create_app, gevent_run, db, api, admin, ModelView from flaskweb.config import DebugConfig import models import views import os basedir = os.path.dirname(os.path.abspath(__file__)) class MyConfig(DebugConfig): # override default Configs SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db') LOGGING_FILE = os.path.join(basedir, "logs", "app.log") UPLOAD_DIR = os.path.join(basedir, "uploads") app = create_app(MyConfig) app.register_blueprint(views.bp) # api api.add_namespace(views.api) # admin admin.add_view(ModelView(models.Todo, db.session)) admin.add_view(ModelView(models.TodoItem, db.session)) if __name__ == "__main__": # for debug only gevent_run(app, "0.0.0.0", 5000)