python学习笔记-flask学习(一)route适配器
生活随笔
收集整理的這篇文章主要介紹了
python学习笔记-flask学习(一)route适配器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以下是一個簡單的能運行的flask示例代碼,從該示例代碼中分析Flask源碼完成了哪些工作。
flask示例代碼如下:
from flask import Flaskapp = Flask(__name__)@app.route('/') def hello_world():return 'Hello World!'@app.route('/user/<name>') def user(name):return '<h1>Hello,%s!<h1>'%nameif __name__ == '__main__':app.run(debug=True)首先調用app = Flask(__name__)構建一個Flask實例。Flask類定義在app.py文件中
"""The flask object implements a WSGI application and acts as the centralobject. It is passed the name of the module or package of theapplication. Once it is created it will act as a central registry forthe view functions, the URL rules, template configuration and much more.The name of the package is used to resolve resources from inside thepackage or the folder the module is contained in depending on if thepackage parameter resolves to an actual python package (a folder withan :file:`__init__.py` file inside) or a standard module (just a ``.py`` file).接下來分析app.route函數完成的工作
app.route是python的一個裝飾器具體的函數代碼如下:
def route(self, rule, **options):def decorator(f):endpoint = options.pop('endpoint', None)self.add_url_rule(rule, endpoint, f, **options)return freturn decorator在這里主要是通過調用add_url_rule函數將app實例與對應的視圖函數關聯起來
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):if endpoint is None:endpoint = _endpoint_from_view_func(view_func)options['endpoint'] = endpointmethods = options.pop('methods', None)# if the methods are not given and the view_func object knows its# methods we can use that instead. If neither exists, we go with# a tuple of only ``GET`` as default.if methods is None:methods = getattr(view_func, 'methods', None) or ('GET',)if isinstance(methods, string_types):raise TypeError('Allowed methods have to be iterables of strings, ''for example: @app.route(..., methods=["POST"])')methods = set(item.upper() for item in methods)# Methods that should always be addedrequired_methods = set(getattr(view_func, 'required_methods', ()))# starting with Flask 0.8 the view_func object can disable and# force-enable the automatic options handling.provide_automatic_options = getattr(view_func,'provide_automatic_options', None)if provide_automatic_options is None:if 'OPTIONS' not in methods:provide_automatic_options = Truerequired_methods.add('OPTIONS')else:provide_automatic_options = False# Add the required methods now.methods |= required_methodsrule = self.url_rule_class(rule, methods=methods, **options)rule.provide_automatic_options = provide_automatic_optionsself.url_map.add(rule)if view_func is not None:old_func = self.view_functions.get(endpoint)if old_func is not None and old_func != view_func:raise AssertionError('View function mapping is overwriting an ''existing endpoint function: %s' % endpoint)self.view_functions[endpoint] = view_func 如果沒有指定endpoint函數_endpoint_from_view_func通過功能函數名指定為endpoint。接下來獲取指定methods。url_rule_class(rule, methods=methods, **options)生成一rule的實例。接著url_map.add(rule)將rule與flask實例關聯起來。在前面flask實例定義了Map對象的實例。add函數的源碼如下: def add(self, rulefactory):for rule in rulefactory.get_rules(self):rule.bind(self)self._rules.append(rule)self._rules_by_endpoint.setdefault(rule.endpoint, []).append(rule)self._remap = True
轉載于:https://www.cnblogs.com/billhust/p/6684911.html
總結
以上是生活随笔為你收集整理的python学习笔记-flask学习(一)route适配器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Objective-C objc_cla
- 下一篇: Android学习笔记进阶十三获得本地全