究竟什么是烧瓶蓝图?

问题:究竟什么是烧瓶蓝图?

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

我什至在我的Web应用程序中使用了它们,但是我不完全了解它们是什么,或者它们如何适合我的整个应用程序。它与我的应用程序实例有何相似之处,但又不太相似?文档虽然很全面,但是我想寻求外行的解释或启发性的类比给我以启发。当一位同事要求我向他们解释我选择在这里询问的Flask蓝图时,我感到非常困惑。

I have read the official Flask documentation on Blueprints and even one or two blog posts on using them.

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.


回答 0

蓝图是用于生成Web应用程序“部分”的模板。您可以将其视为模具:

您可以获取该蓝图,并将其应用于多个位置。每次您应用它时,该蓝图都会在您的应用程序的石膏中创建其结构的新版本。

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

这是处理树木的简单模型-它说与树木打交道的任何应用程序都应提供对其叶子,根和环的访问(按年)。就其本身而言,它是一个中空的外壳-它无法路由,无法响应,直到被应用程序打动为止:

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")

一旦创建它,​​就可以通过使用register_blueprint功能将其“打动”在应用程序上-这在所指定的位置“打动”应用程序上的蓝图模具url_prefix

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

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_blueprint function – this “impresses” the mold of the blueprint on the application at the locations specified by url_prefix.


回答 1

正如@Devasish评论中指出的那样,本文提供了一个很好的答案:

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

从文章引用:

一个例子就是Facebook。如果Facebook使用Flask,它可能具有静态页面(即,登出的首页,注册,关于等),仪表板(即新闻提要),个人资料(/ robert / about和/ robert / photos)的蓝图,设置(/ settings /安全性和/ settings / privacy)等等。这些组件都具有通用的布局和样式,但是每个组件也都有自己的布局

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

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

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

Quoting from the article:

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

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.


回答 2

我自己也偶然发现了这个问题,在阅读了一些文档资料后感到困惑。起初,我认为它就像C#/ Java实现风格,您可以定义一些东西,而不必担心以后定义它。但是,我偶然发现了该页面,将其置于非常普通的外行(以及当下颇为热闹的当下事件)术语。https://hackersandslackers.com/flask-blueprints/

从本质上讲,链接中提到的一项好处是使我可以有效地逻辑组织/划分,从而使我对它在现实世界中的使用情况有一个清晰的认识。应用程序为几个部分,这些部分只需要关心它自己的事务,就。因此,它提供了一些设计好的封装。

编辑:我目前正在使用它来细分我的webapps代码。这也是一个很好的决定,因为我发现首席设计师希望在Vue.js中成为前端。我还没有使用过,但是查看它的项目文件看起来会更加混乱,并且可能提供很多命名冲突的倾向。

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#/Java Implementation style where you define some stuff but dont have to worry about it defining it til 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/

Essentially one benefit that is mentioned in the link and provides me a clear idea of it’s real world usage is that I can effectively logically organize/divide the 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 used yet but looking at it’s project files would look far more messy and probably provide many naming collision prone.


回答 3

对于较大的项目,您的所有代码都不应位于同一文件中。相反,您可以根据功能将较大的代码分段或拆分为单独的文件。就像砖砌墙一样。

希望这会有所帮助。由于这是一个理论性的问题,因此请不要发布任何代码。

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.