如何将烧瓶路线分离到另一个模块

时间:2015-05-11 06:39:52

标签: 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"])

2 个答案:

答案 0 :(得分:3)

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)

答案 1 :(得分:0)

创建一个名为views.py的新文件,并在那里添加所有路由。然后在__ init __.py中导入views.py。