How to understand the blueprint in flask?

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply: well, understanding the "blueprint" is a difficult option for beginners of Python + Flask who are not familiar with other Web programming implementations.

I have to use the metaphor to introduce the blueprint in a different way, so that you can better understand it. it is estimated that this question is a newbie, so I am so embarrassed.

First, I would like to mention the concept. the official term "Blueprint" is "Blueprint", which is also called "Blueprint" in Chinese translation.
When you go to a restaurant for dinner and order some food, you must have eaten something like a spicy pot. You can order some Miscellaneous dishes, select the taste, and then bring up a bowl. you have a great taste, and you will also feel that this "one pot of food" method is very good, after all, I won't wait for the next one. it's nice to get hot together. In addition, when there is very little food, it is also a bit "forced" to use a single bowl for a menu.
If you want to write a BBS (forum system) on your own, the example above shows that when the function is weak in the initial stage, there may be not many pages and few functions are implemented. The home page may be a list of posts that allow users to post, and do not involve logon and exit, membership levels, permission management, etc. you think, that is what we will talk about later. As a result, you write the website trunk code in a run. py file, so that the website can be successfully run. you think this implementation is very convenient and fast.
If I want you to run it at this time. py, which can be independent into other files, and then run. py import, you want to scold me for "installation force", it's okay to find something. Yes, the "blueprint" is not suitable for building website systems that you want to be simple.

However, your appetite is getting bigger and you have good friends. you are going to have dinner together. So you encountered some problems:
  1. Your friends do not like to eat something, but you do. Your friends like something you don't like.
  2. There are too many people, and there are too many items to order. if you want to finish it, you can pack it for tomorrow. but you still need to pack some dishes. after all, some dishes taste bad overnight, it is easy to break down.
  3. You and your friends both want to eat one thing first and then another. For example, you should first take some food, and then take some food to solve the problem.
If you still eat the spicy pot, you will not be happy.
Similarly, when your BBS system becomes more and more complex and features such as logon and exit, membership level, and permission management become a problem:
  1. The run. py file must be larger and larger, with more and more lines of code. All the routes on your website are written in one file, and the functions are also in one file, which is prone to errors and difficult to locate. Of course, if you have no practical experience, you can't really understand this. Then I will talk about another obvious problem: import and config are generally written in the header. how can you write your new routes in order, so when you program it, you can check the top part of the code file and drag it to the bottom to continue writing. this is enough to be a "fuck.
  2. Large-scale websites must be programmed by many people. if you are scheduled to log on and exit at the moment, and your friends are responsible for permission management, the boss certainly will not allow you to start work after you finish your work, work together! However, only run. py is a file. you and your friends can copy a copy of run. py starts writing, and your code starts writing from 100th lines, and your friends start writing from 100 lines. then you also import some new things in the header. What should I do after writing? There is just a run. py online, which is "bad". Is it hard to "merge" the code? This is even more "fuck. What if 10 people write websites? -- "The eggs are broken 」
Therefore, the first reaction at the moment is, of course, the "spicy pot" cannot be eaten. this "bowl of dishes is a bowl of dishes" approach is the best solution. what do you like to eat, which bowl can be packed directly? you don't need to pick it with chopsticks.
When you use Python + Flask to build a website, it is "inappropriate" to manually extract code into files independently. you need Flask to provide some support, so the "blueprint" appeared.

The "blueprint" allows you to separate different routes and provides some "specifications (standards)" with many benefits. You can ask sellers to install different dishes, just as you can also ask "blueprint" to apply different static resources for different routes, this results in different URLs with different website interfaces (otherwise, front-end CSS will be mixed ).
The "blueprint" makes it easier for you and your friends to get started with the above troubles. how nice is it if you change your A file, change his B file, and submit their respective documents later.
"Blueprint" allows you to use another program. if the program also has logon and exit, you can even directly remove the part of the BBS logon and exit, this is far more efficient than directly using a run. py, you need to know that even if a login exits, you are in a run. in py, the code will be scattered everywhere.

"Blueprint" also has other advantages. in essence, it is to make the program more loosely coupled, more flexible, more reusable, more error detection efficiency, and lower error probability.

As mentioned above, if you have some practical experience, the official documents are more accurate: modular application using blueprints

I hope my answers will help you get around. : P can refer to this, blueprint | Flask tour Blueprint separates modules that implement different functions.

Factor an application into a set of blueprints.
Split a large application into modules that implement different functions.

Register a blueprint on an application at a URL prefix and/or subdomain.
You can call the view function of another blueprint in one blueprint, but you must add the corresponding blueprint name.

Register a blueprint multiple times on an application with different URL rules.
Never used! Write functions in other files, and then import them in the main running file.
It is actually a routing system. I am also a newbie. I can understand it as a big real estate (app) with different classes, and there are many function buildings ).


A blueprint is similar to a class. it also needs to be registered and instantiated, except that the positions in the project are different and the reference methods are different.

There is also a blueprint for the exact project, but it is not another blueprint. it is mainly used for modular routing and there are too many routes. it is packaged and classified using the blueprint. In different situations, for example, the development and production environments may have different requirements. By registering a route to a blueprint without interfering with each other, you can replace the bound blueprint as needed. Large applications are separated into small pieces, and each small part is executed with a blueprint.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.