2016-04-12 11 views
1

私は青写真オブジェクト "api"とapi.routeアノテーションで多くのAPIを定義したapis.pyファイルを持っています。例えば:url_map.iter_rulesをappの代わりに青写真オブジェクトで使用する方法

@api.route('/', methods=['GET']) 
def get_info(): 

私は、我々はAppオブジェクトの「url_map.iter_rules」を使用する場合、我々は何を得ると同じ持つすべてのAPIの概要を反復して取得したいです。 api blueprintオブジェクトを使ってこれをどうやって行うことができますか?私は例えば、私はあなたが青写真を登録した後、あなたがapp.url_map.iter_rules()を呼び出す場合は、あまりにもサブドメインのすべてのエンドポイントを取得すると思う

from .api_1 import api as api_blueprint 
app.register_blueprint(api_blueprint) 

答えて

1

を使用して、私のinit.pyファイルに青写真を登録しています

api.py

from flask import Blueprint 
api = Blueprint('api', __name__) 
@api.route('/') 
def call_api(): 
     return "" 

init.py:

from flask import Flask, Blueprint 
from api import api 

public = Blueprint('public', __name__) 
@public.route('/') 
def home(): 
     return render_template('public/home.html') 


app = Flask(__name__) 
app.register_blueprint(public) 
app.register_blueprint(api, subdomain='api') 
print(list(app.url_map.iter_rules())) 

[<Rule 'api|/' (GET, HEAD, OPTIONS) -> api.call_api>, 
<Rule '/' (GET, HEAD, OPTIONS) -> public.home>, 
<Rule '/static/<filename>' (GET, HEAD, OPTIONS) -> static>] 
+0

私の質問のように私はinit.pyに私のアプリのオブジェクトを開始していますし、私はapis.pyどこと呼ばれる別のファイルを持っています私はすべてのAPIを定義しています。だから、私はapis.pyファイルに青写真を登録していないし、そこにオブジェクトを持っていない。私は 'from 'を使ってapis.pyで青写真をインポートするだけです。輸入API。私がapis.pyやinit.pyに含める必要があるものは何ですか?私はセパレートAPI 'api.route( '/ api/help'、methods = ['GET'])にする必要があります。このアプリケーションオブジェクトを使って' app.url_map.iter_rules() 'を使うコードはすでにあります。 – aaj

+0

私は使いたい:whchはdocの文字列を読む: '@ api.route( '/ api/help'、methods = ['GET']) def help(): func_list = {} url_map.iter_rules(): 場合rule.endpoint = '静的':! のdocstring = app.view_functions [rule.endpoint] .__ doc__内の例題の場合のdocstring: func_list [ドキュメント文字列] = rule.rule リターンjsonify(func_list) ' – aaj

+0

okですので、私の答えでモジュールを並べ替えましたが、今の状況に合っていますか?モジュール内にappオブジェクトは必要ありません。 – maxymoo

関連する問題