Flask源码阅读-第二篇(flask\__init__.py)

Posted sea的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask源码阅读-第二篇(flask\__init__.py)相关的知识,希望对你有一定的参考价值。

源码:

# -*- coding: utf-8 -*-
"""
flask
~~~~~

A microframework based on Werkzeug. It‘s extensively documented
and follows best practice patterns.

:copyright: ? 2010 by the Pallets team.
:license: BSD, see LICENSE for more details.
"""

__version__ = ‘1.0.2‘

# utilities we import from Werkzeug and Jinja2 that are unused
# in the module but are exported as public interface.
from werkzeug.exceptions import abort
from werkzeug.utils import redirect
from jinja2 import Markup, escape

from .app import Flask, Request, Response
from .config import Config
from .helpers import url_for, flash, send_file, send_from_directory,
get_flashed_messages, get_template_attribute, make_response, safe_join,
stream_with_context
from .globals import current_app, g, request, session, _request_ctx_stack,
_app_ctx_stack
from .ctx import has_request_context, has_app_context,
after_this_request, copy_current_request_context
from .blueprints import Blueprint
from .templating import render_template, render_template_string

# the signals
from .signals import signals_available, template_rendered, request_started,
request_finished, got_request_exception, request_tearing_down,
appcontext_tearing_down, appcontext_pushed,
appcontext_popped, message_flashed, before_render_template

# We‘re not exposing the actual json module but a convenient wrapper around
# it.
from . import json

# This was the only thing that Flask used to export at one point and it had
# a more generic name.
jsonify = json.jsonify

# backwards compat, goes away in 1.0
from .sessions import SecureCookieSession as Session
json_available = True


flask\__init__.py本模块顾名思义,主要是做了大量的初始化导入工作,方便后面的直接调用,其中最核心的一句
jsonify = json.jsonify, 在后面的json和接口开发中用得比较多,对json格式的接口交互,
相对 Python的原生json方法,我个人比较推荐用flask的jsonify方法,其直接返回的content-type就是json












































以上是关于Flask源码阅读-第二篇(flask\__init__.py)的主要内容,如果未能解决你的问题,请参考以下文章

Flask之基于route装饰器的路由系统(源码阅读解析)

flask 请求上下文源码(转)

Flask最强攻略 - 跟DragonFire学Flask - 第二篇 Flask 中的 Render Redirect HttpResponse

flask第二篇 三剑客+特殊返回值

Flask 源码(1)

flask源码分析