Python 究竟什么是 Flask 蓝图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24420857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:35:13  来源:igfitidea点击:

What are Flask Blueprints, exactly?

pythonflaskwsgi

提问by JoshieSimmons

I haveread the official Flask documentationon Blueprints and even oneor twoblog posts on using them.

阅读官方瓶文档的蓝图,甚至一个2上使用他们的博客文章。

I've even used them in my web app, but I don't completely understand what they are or how they fit into my app as a whole. How is it similar to an instance of my app but not quite? The documentation is comprehensive but I seek a layman explanation or an enlightening analogy to spark it for me. I was sufficiently perplexed when a colleague asked me to explain a Flask blueprint to them that I elected to ask here.

我什至在我的 Web 应用程序中使用过它们,但我并不完全了解它们是什么或它们如何作为一个整体融入我的应用程序。它与我的应用程序实例有何相似但又不完全相同?文档很全面,但我寻求一个外行的解释或一个有启发性的类比来为我激发它。当一位同事让我向他们解释我选择在这里询问的 Flask 蓝图时,我感到非常困惑。

采纳答案by Sean Vieira

A blueprint is a template for generating a "section" of a web application. You can think of it as a mold:

蓝图是用于生成 Web 应用程序“部分”的模板。你可以把它想象成一个模具:

A medallion mold with a gold medallion freshly removed from it

一个奖章模具,上面有刚从上面取下的金奖章

You can take the blueprint and apply it to your application in several places. Each time you apply it the blueprint will create a new version of its structure in the plaster of your application.

您可以将蓝图应用到您的应用程序的多个位置。每次应用它时,蓝图都会在应用程序的灰泥中创建其结构的新版本。

# An example
from flask import Blueprint

tree_mold = Blueprint("mold", __name__)

@tree_mold.route("/leaves")
def leaves():
    return "This tree has leaves"

@tree_mold.route("/roots")
def roots():
    return "And roots as well"

@tree_mold.route("/rings")
@tree_mold.route("/rings/<int:year>")
def rings(year=None):
    return "Looking at the rings for {year}".format(year=year)

This is a simple mold for working with trees - it says that any application that deals with trees should provide access to its leaves, its roots, and its rings (by year). By itself, it is a hollow shell - it cannot route, it cannot respond, until it is impressed upon an application:

这是一个用于处理树木的简单模型——它说任何处理树木的应用程序都应该提供对它的叶子、根和年轮的访问(按年)。就其本身而言,它是一个空壳——它不能路由,不能响应,直到它被应用程序打动:

from tree_workshop import tree_mold

app.register_blueprint(tree_mold, url_prefix="/oak")
app.register_blueprint(tree_mold, url_prefix="/fir")
app.register_blueprint(tree_mold, url_prefix="/ash")

Once it is created it may be "impressed" on the application by using the register_blueprintfunction - this "impresses" the mold of the blueprint on the application at the locations specified by url_prefix.

一旦它被创建,它就可以通过使用该register_blueprint函数在应用程序上“留下印记” ——这将“印记”在应用程序上由url_prefix.

回答by Tran Cuong

As pointed out in a comment by @Devasish, this article provides a good answer:

正如@Devasish评论中指出的,这篇文章提供了一个很好的答案:

http://exploreflask.com/en/latest/blueprints.html

http://exploreflask.com/en/latest/blueprints.html

Quoting grom the article:

引用 grom 文章:

An example of this would be Facebook. If Facebook used Flask, it might have blueprints for the static pages (i.e. signed-out home, register, about, etc.), the dashboard (i.e. the news feed), profiles (/robert/about and /robert/photos), settings (/settings/security and /settings/privacy) and many more. These components all share a general layout and styles, but each has its own layout as well

这方面的一个例子是 Facebook。如果 Facebook 使用 Flask,它可能有静态页面的蓝图(即注销的主页、注册、关于等)、仪表板(即新闻提要)、配置文件(/robert/about 和 /robert/photos)、设置(/settings/security 和 /settings/privacy)等等。这些组件都共享通用布局和样式,但每个组件也有自己的布局

This is a very good interpretation, especially the part "if Facebook used Flask". It gives us a concrete situation to visualize how Blueprint actually works.

这是一个很好的解释,尤其是“如果Facebook使用Flask”那部分。它为我们提供了一个具体的情况来可视化蓝图的实际工作方式。

回答by LFMekz

I too just stumbled up this myself and was confused after reading a few of the documentation sources. At first I thought it was like C# Implementation style where you define some stuff but dont have to worry about it later. However, I stumbled up this page which puts it in very very laymens (and quite hilarious current day events) terms. https://hackersandslackers.com/flask-blueprints/

我自己也偶然发现了这一点,并在阅读了一些文档来源后感到困惑。起初我认为它就像 C# 实现风格,你定义了一些东西,但以后不必担心。然而,我偶然发现了这个页面,它用非常非常外行(以及非常搞笑的当前事件)术语来描述它。https://hackersandslackers.com/flask-blueprints/

Essentially one benefit that is mentioned in the link and provides me an idea of real world usage is that I can effectively logically organize/dividethe app into several parts that only need to be concerned with it's own affairs. So it provides some designed encapsulation.

从本质上讲,链接中提到的一个好处是我可以有效地将应用程序逻辑地组织/划分为几个只需要关心它自己的事务的部分。所以它提供了一些设计好的封装。

Edit: I'm currently using it to segment out my webapps code. It was good decision too because I found the lead designer wants to make the frontend in Vue.js. Which I havent use yet but looking at it's project files would look far more messy and probably naming collision prone.

编辑:我目前正在使用它来分割我的 webapps 代码。这也是一个很好的决定,因为我发现首席设计师想要在 Vue.js 中制作前端。我还没有使用它,但查看它的项目文件会看起来更加混乱,并且可能容易命名冲突。

回答by Henshal B

For bigger projects, all your code shouldn't be in the same file. Instead you can segment or split bigger codes into separate file, mostly based on functionality. Like bricks forming a wall.

对于较大的项目,您的所有代码不应位于同一个文件中。相反,您可以将较大的代码分段或拆分为单独的文件,主要基于功能。像砖砌成一堵墙。

Hope this helped. Since this is a theoretical question, posting no codes.

希望这有帮助。由于这是一个理论问题,因此不发布代码。