0

I'm trying to get all the routes for Flask when it's initially loaded but it seems to be super flaky. It'll give me all the routes half the time and the other half the time it'll give me this:

['/static/path:filename HEAD,GET,OPTIONS /static/path:filename']

I'm using this codeblock in the before_first_request and in the init constructor the Flask app. Any ideas how I can make this consistent? I want to do this as soon as the app is fully loaded automatically.

        output = []
        for rule in self.url_map.iter_rules():
            try:
                methods = ','.join(rule.methods)
                line = urllib.parse.unquote("{:50s} {:20s} {}".format(str(rule), methods, rule))
                output.append(line)
            except Exception as e:
                print("error with rule: " + str(rule))
        dct = {"endpoints": output}
Boeggles
  • 59
  • 1
  • 6

1 Answers1

0

you can iterate over the flask dictionary using this app property view_functions

something like this:

app = Flask(__name__)
... #your code here
for str_function, function_object in app.view_functions:
   print(f"the function url is {str_function}")

something I've read from the Flask doc documentation on flask

Carlos
  • 190
  • 8
  • Oh I'm specifically trying to get all the urls defined – Boeggles Dec 12 '21 at 19:37
  • I don't understand, the str_function variable contains the endpoint of all functions it is not what you need? – Carlos Dec 13 '21 at 04:07
  • maybe is this what you are asking? https://stackoverflow.com/questions/13317536/get-list-of-all-routes-defined-in-the-flask-app – Carlos Dec 13 '21 at 04:25