16

I'm writing a small API, and wanted to print a list of all available methods along with the corresponding "help text" (from the function's docstring). Starting off from this answer, I wrote the following:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api', methods = ['GET'])
def this_func():
    """This is a function. It does nothing."""
    return jsonify({ 'result': '' })

@app.route('/api/help', methods = ['GET'])
    """Print available functions."""
    func_list = {}
    for rule in app.url_map.iter_rule():
        if rule.endpoint != 'static':
            func_list[rule.rule] = eval(rule.endpoint).__doc__
    return jsonify(func_list)

if __name__ == '__main__':
    app.run(debug=True)

Is there a better -- safer -- way of doing this? Thanks.

Community
  • 1
  • 1
iandexter
  • 350
  • 2
  • 3
  • 11

3 Answers3

35

There is app.view_functions. I think that is exactly what you want.

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api', methods = ['GET'])
def this_func():
    """This is a function. It does nothing."""
    return jsonify({ 'result': '' })

@app.route('/api/help', methods = ['GET'])
def help():
    """Print available functions."""
    func_list = {}
    for rule in app.url_map.iter_rules():
        if rule.endpoint != 'static':
            func_list[rule.rule] = app.view_functions[rule.endpoint].__doc__
    return jsonify(func_list)

if __name__ == '__main__':
    app.run(debug=True)
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
falsetru
  • 357,413
  • 63
  • 732
  • 636
6

Here's mine:

@app.route("/routes", methods=["GET"])
def getRoutes():
    routes = {}
    for r in app.url_map._rules:
        routes[r.rule] = {}
        routes[r.rule]["functionName"] = r.endpoint
        routes[r.rule]["methods"] = list(r.methods)

    routes.pop("/static/<path:filename>")

    return jsonify(routes)

Gives:

{
    "/": {
        "functionName": "index",
        "methods": [
            "HEAD",
            "OPTIONS",
            "GET"
        ]
    },
    "/gen": {
        "functionName": "generateJobs",
        "methods": [
            "HEAD",
            "OPTIONS",
            "GET"
        ]
    },
    "/jobs": {
        "functionName": "getJobs",
        "methods": [
            "HEAD",
            "OPTIONS",
            "GET"
        ]
    },
    "/jobs/submit": {
        "functionName": "postJob",
        "methods": [
            "POST",
            "OPTIONS"
        ]
    },
    "/jobs/update/<id>": {
        "functionName": "updateJob",
        "methods": [
            "POST",
            "OPTIONS"
        ]
    },
    "/routes": {
        "functionName": "getRoutes",
        "methods": [
            "HEAD",
            "OPTIONS",
            "GET"
        ]
    }
}
copeland3300
  • 581
  • 7
  • 11
3
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/api/help', methods=['GET'])
def help():
    endpoints = [rule.rule for rule in app.url_map.iter_rules() 
                 if rule.endpoint !='static']
    return jsonify(dict(api_endpoints=endpoints))

if __name__ == '__main__':
       app.run(debug=True)