I have a blueprint object "api"

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())) 

And when I type:

 print(list(app.url_map.iter_rules())) 

I get the following result:

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

But I need to write the result in a text document, as the result is higher.

How do I write app.url_map.iter_rules () to a text document?

    1 answer 1

    First, bring in the appropriate form:

     rules = map(repr, app.url_map.iter_rules()) 

    Then write to the file:

     with open('output.txt', 'w') as f: f.write('\n'.join(rules)) 

    Or in the form of json:

     import json with open('output.txt', 'w') as f: f.write(json.dumps(rules))