作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在编写一个小型 API,并希望打印所有可用方法的列表以及相应的“帮助文本”(来自函数的文档字符串)。从 this answer 开始,我写了以下内容:
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)
是否有更好 - 更安全 - 的方法?谢谢。
最佳答案
有app.view_functions
。我认为这正是您想要的。
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)
关于python - 列出 Flask 中所有可用的路由,以及相应函数的文档字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17249953/
我在 Java 中遇到异常处理问题,这是我的代码。当我尝试运行此行时出现编译器错误:throw new MojException("Bledne dane");。错误是: exception MojE
我刚刚开始学习asp.net。在你们的支持下,我希望我能从这个论坛学到更多东西。 我的问题是, 我在 asp.net 页面中有一个 TabContainer1,因为每个选项卡面板中有多个类似 (60)
我是一名优秀的程序员,十分优秀!