gpt4 book ai didi

flask - 如何将 flask 路线分离到另一个模块

转载 作者:行者123 更新时间:2023-12-03 17:18:18 24 4
gpt4 key购买 nike

我的 flask 主模块中有数百条路线,

我认为它需要将这数百条路由与主模块分开。

怎么做 ?

#!/usr/bin/env python3
# -*- coding: utf8 -*-
from flask import request, url_for
from flask import Flask, request, jsonify
from flask_request_params import bind_request_params
from flask import g
import datetime
import pandas as pd
import pymongo
from webargs import Arg
from webargs.flaskparser import use_args, use_kwargs
import yaml
import time, functools
from pdb import set_trace
from pandas_helper import PandasHelper
import errors
from app_helper import *
from release_schedule import ReleaseSchedule
from mongo import Mongo


@app.route('/next_release', methods=["GET"])
@return_json
def next_release():
schedules = ReleaseSchedule.next_release(DB)
return pd.DataFrame([sche for sche in schedules])

...
@app.route('/last_release', methods=["GET"])

最佳答案

This is what blueprints were made to do.

Another alternative is flask-classy (which is awesome) .我将讨论蓝图方法,因为这是我更了解的。

如果我处于你的位置,我想根据常见的进口来划分我的路线。

在不知道您的应用程序的情况下,我会猜测像这样的发行版
parse_user_data_views.py

from webargs import Arg
from webargs.flaskparser import use_args, use_kwargs
import yaml
push_to_db_views.py
from pandas_helper import PandasHelper
from mongo import Mongo
import pymongo
import pandas as pd
import datetime
release_views.py
from release_schedule import ReleaseSchedule
import pandas as pd

@app.route('/next_release', methods=["GET"])
@return_json
def next_release():
schedules = ReleaseSchedule.next_release(DB)
return pd.DataFrame([sche for sche in schedules])

很可能是分布。我们无法为您解答,只有您可以。

但这允许您以一些非常好的方式分离您的应用程序。

__init__.py
from flask import Flask 

from yourapplication.release_views import release_views
from yourapplication.push_to_db_views import push_to_db_views
from yourapplication.parse_user_data_views import parse_user_data_views


app = Flask(__name__)
app.register_blueprint(release_views)
app.register_blueprint(push_to_db_views)
app.register_blueprint(parse_user_data_views)

关于flask - 如何将 flask 路线分离到另一个模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30161047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com