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

时间:2024-01-11 19:09:38
源码:

# -*- 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