当前位置:网站首页>Flask application case
Flask application case
2022-06-27 22:13:00 【laufing】
demand
- establish app, And use the configuration file to configure mysql
- Create a apps Catalog , Create a... Inside users Catalog ,users Create under directory user The blueprint , And register to the app
- by user Blueprint configuration routing ; Native route /info return info.html; restful api route /user/mobile return json data .
- by app Configure cross domain ( Use flask_cors)
Case code
- app.py
from flask import Flask
from flask import request, session, current_app, g
from flask_restful import Api, Resource
from flask_sqlalchemy import SQLAlchemy
from apps.users.user_app import user_app
from apps.users import models
from flask_cors import CORS
app = Flask(__name__)
# To configure app
app.config.from_pyfile("settings.py")
# Initialize database connection
db = SQLAlchemy(app)
# api Interface , The blueprint has its own api object
api = Api(app)
# Registered blueprint , Blueprints are extended applications , Internal routes will also be put into url_map
app.register_blueprint(user_app)
if __name__ == "__main__":
print("xxx:", app.url_map)
# Configure cross domain , Tested
CORS(app, supports_credentials=True,
origins=["http://localhost:8080",],
methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["x-requested-with", 'content-type', 'authorization', "author"],
expose_headers="*")
app.run(host="localhost", port=5000, debug=True)
- user_app.py
from flask import request, render_template
from flask import Blueprint
from flask import jsonify
user_app = Blueprint("user", __name__,
static_folder="static/users", # There is no way to access the static file for the time being
template_folder="templates/users")
@user_app.route("/info", methods=["GET", "POST"], endpoint="userInfo")
def userInfo():
if request.method == "GET":
return render_template("users/info.html")
else:
return " No page "
from flask_restful import Api, Resource
# Instantiate blueprint api object , add to restful api
api = Api(user_app)
class UserResource(Resource):
def get(self):
res = jsonify({
"code": 200,
"msg": "1733532..."
})
return res
api.add_resource(UserResource, "/user/mobile")
- manage.py
from app import app, db
from app import user_app
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command("db", MigrateCommand)
if __name__ == "__main__":
manager.run()
- settings.py
# Use pymysql
import pymysql
pymysql.install_as_MySQLdb()
# Configuration database , With the above code , You can't mysql+pymysql
# SQLALCHEMY_DATABASE_URI = "mysql+pymysql://lauf:[email protected]:3306/"
SQLALCHEMY_DATABASE_URI = "mysql://lauf:[email protected]:3306/flask_625"
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ECHO = True
- models.py
边栏推荐
- Summary of Web testing and app testing by bat testing experts
- .NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
- 美团20k软件测试工程师的经验分享
- A method of go accessing gbase 8A database
- I think I should start writing my own blog.
- 石子合并问题分析
- Summary of gbase 8A database user password security related parameters
- Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function
- CDH集群之YARN性能调优
- Go from introduction to practice -- definition and implementation of behavior (notes)
猜你喜欢

使用Fiddler模拟弱网测试(2G/3G)

Go from introduction to actual combat - task cancellation (note)

Windwos 8.1系统安装vmware tool插件报错的解决方法

Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
扁平数组和JSON树的转换

管理系统-ITclub(中)

Figure countdownlatch and cyclicbarrier based on AQS queue

二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)

Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?

Codeforces Round #719 (Div. 3)
随机推荐
管理系统-ITclub(下)
Codeforces Round #717 (Div. 2)
Bit. Store: long bear market, stable stacking products may become the main theme
对话乔心昱:用户是魏牌的产品经理,零焦虑定义豪华
Interview question 3 of software test commonly used by large factories (with answers)
百万年薪独家专访,开发人员不修复bug怎么办?
Method of reading file contents by Excel
记一次List对象遍历及float类型判断大小
[LeetCode]161. Edit distance of 1
xpath
Gbase 8A OLAP analysis function cume_ Example of dist
Test automatique de Test logiciel - test d'interface de l'introduction à la maîtrise, apprendre un peu chaque jour
Go from introduction to actual combat - task cancellation (note)
Selenium上传文件有多少种方式?不信你有我全!
[sword offer ii] sword finger offer II 029 Sorted circular linked list
Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager
[LeetCode]161. 相隔为 1 的编辑距离
Quick excel export according to customized excel Title Template
gomock mockgen : unknown embedded interface
[LeetCode]186. 翻转字符串里的单词 II