当前位置:网站首页>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 gbase 8A database user password security related parameters
- 管理系统-ITclub(上)
- Little known MySQL import data
- Luogu p5706 redistributing fertilizer and house water
- [LeetCode]572. A subtree of another tree
- MYSQL和MongoDB的分析
- Codeforces Round #722 (Div. 2)
- 管理系统-ITclub(中)
- ∫(0→1) ln(1+x) / (x ² + 1) dx
- Bean paste green protects your eyes
猜你喜欢

Use Fiddler to simulate weak network test (2g/3g)
![[LeetCode]动态规划解分割数组I[Red Fox]](/img/b2/df87c3138c28e83a8a58f80b2938b8.png)
[LeetCode]动态规划解分割数组I[Red Fox]
![[LeetCode]动态规划解分割数组II[Arctic Fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[LeetCode]动态规划解分割数组II[Arctic Fox]
![[LeetCode]动态规划解拆分整数I[Silver Fox]](/img/18/8dc8159037ec1262444db8899cde0c.png)
[LeetCode]动态规划解拆分整数I[Silver Fox]

Simulink导出FMU模型文件方法

Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager

畅游动态规划之区间DP

Go from introduction to actual combat - execute only once (note)

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

C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.
随机推荐
Go 访问GBase 8a 数据库的一个方法
Remote invocation of microservices
登录凭证(cookie+session和Token令牌)
Figure countdownlatch and cyclicbarrier based on AQS queue
[LeetCode]508. 出現次數最多的子樹元素和
CDH集群之YARN性能调优
Go from introduction to actual combat - task cancellation (note)
使用sqlite3语句后出现省略号 ... 的解决方法
Simulink导出FMU模型文件方法
Golang uses regularity to match substring functions
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图
Test automatique de Test logiciel - test d'interface de l'introduction à la maîtrise, apprendre un peu chaque jour
[leetcode] dynamic programming solution split integer i[silver fox]
美团20k软件测试工程师的经验分享
软件缺陷管理——测试人员必会
Management system itclub (medium)
真香,自从用了Charles,Fiddler已经被我彻底卸载了
畅游动态规划之区间DP
[LeetCode]572. A subtree of another tree