# Python Flask Tutorial: Full-Featured Web App Part 11 - Blueprints and Configuration

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=Wfx4YBzg16s
- **Дата:** 04.05.2018
- **Длительность:** 42:42
- **Просмотры:** 194,116

## Описание

In this Python Flask Tutorial, we will be learning how to restructure our application to use blueprints. Blueprints allow us to split up our application into more manageable sections. We will also move our configuration into its own file and create a configuration class. Lastly, we will move the creation of our application into its own function. This is called an Application Factory, and it allows us to easily create multiple instances of our app with different configurations. Let's get started...

The code for this series can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Flask_Blog

Environment Variables (Windows):
https://youtu.be/IolxqkL7cD8

Environment Variables (Mac and Linux):
https://youtu.be/5iWhQWVXosU


✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1

✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer

▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/

#Python #Flask

## Содержание

### [0:00](https://www.youtube.com/watch?v=Wfx4YBzg16s) Intro

hey there how's it going everybody in this video we'll be restructuring our application a bit and learning how to use flash blueprints now we will also move our configuration variables into their own file and move the creation of our application into its own function now the benefits of doing all this will be that we can take our current application which currently has a lot of functionality and a few files and split that up to be more modular now also moving the creation of our application to a function will allow us to create different instances of our application which is extremely useful if we want to have one application for testing and one application for production and that is called an application Factory so let's go ahead and get started and see how we can do this so first of all let's break

### [0:41](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=41s) Blueprints

our application up into blueprints now this is going to allow us to split up the different functionality of our application and to their own sections so for example in our routes module right now if I scroll down through here you can see that we have a lot of different routes all in this one file so this is getting pretty large and also some of these routes aren't really related to each other so for example if we wanted to update our post route then we'd have to sift through a lot of different user routes in order to find what we're looking for and it may not be a big deal for a smaller application like we have here but as your applications grow you're not going to want to maintain a single massive routes file or hundreds of forms in a single file and blueprints are going to be what allows us to separate these out so let's go ahead and see how we can do this now to create a blueprint we're going to want to create a new package in our application that is named after the functionality that it will contain so let's say that I want to make a blueprint for my users and author an authentication functionality so we can create a directory within our application and we'll call this users so within our current application package which is now called flask blog I'm going to create a new directory here and I'm going to call that directory users and we're going to want to do the same thing with posts as well so I'm going to go ahead and create a directory for post as well so call that post and we're also going to have some routes and functionality that don't really belong to either users or posts so for example our home or our about page so let's also create a blueprint for those and we'll just call those main so I'll create another directory here within our current application package and I will call this main and we also want all of those directories that we just created to be packages as well so if you remember in order to tell Python that those are packages we just need to create a double underscore and knit dot pi file within those directories and it doesn't even have to contain anything it can be an empty file and Python will still know that is a package so I'll create a double underscore a knit PI file in the users posts and main directories that we just created so I will do that now so within main double underscore and knit double underscore dot PI and we will create this under post as well so within post we'll create a double underscore Annette dot PI file and within users we will create that as well now I know that it might be hard to see my directory structure that we're creating here in the sidebar I can't really find a way to make that larger but I will display a larger tree structure in the terminal at the end of this video so you can see what this entire directory structure looks like ok so I'm going to leave these Annette dot pi files that we just created empty so I'm going to close those down and now

### [3:31](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=211s) Routes

within each of our packages that we just created we're also going to make a route stop py file that will only contain the routes that have anything to do with that functionality so for example the route stop PI file that we create in our users package will only contain routes for our users and authentication so let's create those so within main here I'll create a file called routes dot py and I will also create this in posts users and let me expand these directories here so that we can see what files we have in here so far now in our users and our post packages we're also going to want to create a form spy file so that we can have our forms separate as well so I'm going to create those so within posts a forms PI file and within users I'm also going to create a form PI file and all of these are empty for now and we're to fill these in just a little bit so I'm going to close down these empty files now we also have some functions

### [4:32](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=272s) Users Blueprint

that we use within our routes I'd like to split out as well so for example if I go down we have safe picture functionality here and also we have a function that sends the reset password email and things like that so those funked functions are all specific to the user's package so let's put them all in their own file also and we'll call that file utils dot pi so within our users package here I'm going to create a new file and we'll call this utils dot pi ok so that'll do it for the files that we are creating for now but now let's actually create a blueprint so let's open our routes top py file within our users package and we'll start there so within our users package I'm going to open up Ralph stop py and now let's create our users blueprint so first of all within here we need to import the blueprint class from flask so I'll say from flask import blueprint and now let's create an instance of this blueprint and it's similar to creating a flask instance so we'll say users is equal to blueprint and then we'll pass in the name of this blueprint so we'll call this users and now we also need to pass in double underscore name so if I open up the init dot PI file of our current application you can see that that's almost like making an instance of a flask object where but instead of only passing in name we're also passing in the name of our blueprint as well ok so now we can start adding our routes but we're no longer going to be using our global app variable to create the routes like we did before instead we're going to create routes specifically for this users blueprint and then register these with our application at a later time so let me show you what I mean so let's grab some routes from our applications existing routes PI file and move them into here so I'm gonna go into our

### [6:20](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=380s) User Routes

existing routes top PI file and let's grab everything that has to do with users so we'll skip the home and about routes so we want the register route and the login round and logout and we'll stop at save picture so we'll cut those out and paste those in to our use routes here and now will keep going so for safe picture we can put that into our user utils that we've created so I'm going to open up that utils pie file and I may as well add those in as we're going to so I will cut out our save picture function and paste that in to this utils file so now let's keep going so we have a count so we want to move that into our user routes post new that's going to go into our post routes so we don't want that so I will paste in our account route here and let's go back and see if we have any more so we'll leave the post there that's another post route another post route okay here we have a user route for a users post page so we will grab that and cut that out and paste it in to our user routes now we have a send resent or send reset email and this function can go into our utils file so we will cut that out so you can see how we're kind of splitting everything up here so that we don't have this one monolithic file that's the point of this video okay so now our last two routes here we can put these and our users are out as well for resetting passwords and resetting the password tokens so I will cut those and paste those in to the bottom of our user routes okay so now we have all of those

### [8:04](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=484s) User Blueprints

user routes and functions moved into our users package and we'll fix all of these imports in just a bit because those will need to be moved around as well but for now in our user routes let me scroll back up to the top here now I said that we weren't going to use this global app variable here to create our routes anymore but instead we're going to create routes specifically for this users blueprint and then register these with our application later so to do this instead of using app dot route right here we're instead going to set this to be equal to users dot route which is this blueprint here and users is whatever that variable name is that you used for your blueprint so now you can do a find and replace in your editor within sublime text I'm going to use this multi cursor to select all of so I'll grab all of these app dot routes and I'm gonna change them all to users dot routes and scroll back up to the top okay so now we're gonna have to go through this same process with our post package so let's open up our post routes dot py file and it's empty for now from our users I'm going to grab just these first couple of lines here and paste this in to our post route py file but instead of calling this users I'm gonna change this over to posts and the name of our blueprint i will also set as posts and now just like we did with our user routes let's also go back to our old route spy file and let's grab all of the routes that have to do with our post and move them in to that post package so I will grab this post route here and I'll keep grabbing these as long as they have to do with a post and we're already down to the bottom here so I will cut out all of those and now in our original routes top pie file were just left with our home route and our about route but I will paste our post routes into our post blueprint here and now again instead of using this app dot route decorator we're instead going to change this so I'm gonna highlight all of these and we want this to be equal to the name of our blueprint dot route so this will be post dot route so once we change all of those then we can scroll back up to the top here and that looks good and now let's do all of this again for the main package that we've created so I will again copy these top couple of lines here where we're importing the blueprint and creating our blueprint and I'm going to open up the routes that py file in the main package that we created and I will paste that in and I will change this over from being post to being main ok and now let's move the routes from our existing routes top py file over into main and we only have two less we only have our home page and our about page so I will cut those out and paste these in to our main routes PI file and now again I'm going to change these over from being at app dot route to being Maine dot route okay so now we are finished splitting up

### [11:10](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=670s) Forms Setup

our routes and if we go back to our old routes file we're only left with the imports and we will clean up the imports later in this video but for now we're going to need to go through this process again with our forms now I know that this can seem a bit tedious moving this stuff around but it really is a good habit to learn how to break up these larger applications into smaller cohesive sections and we'll see how easy it is to add a new section in the next video when we add custom air pages okay so I'm going to open up our blueprint forms file for our users and currently this is empty and we're going to move all of the forms that are specific to users and authentication from our old forms into this directory so if I open up our old forms file then we're going to want to grab all of our forms that have to do with our users or authentication so I'll grab the registration form and the login form update account info I'll take that too I'll stop at the post form so I'll cut out what we have so far and paste this in to our user forms file and now go back to our forms and see if we have anything else so yeah we have our reset or request reset form and our reset password form so almost all these are user forms here so I will paste those in and now let's also do the same thing for our posts so I will go to our post package and open up our forms py file which is empty right now and then open up our old form stop pie file and now we only have this one form that we are going to paste into the post forms file and I'll give this a little bit room at the top for whenever we add some imports okay so now let's fix these imports

### [12:57](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=777s) Fixing Imports

now the way that I like to do this is to use my editor to tell me what imports are missing or are unused the way that I have sublime text set up it will give me a notice if an import isn't used in a module or if one is missing now if you don't have an editor that does this then you'll just have to go through and find what you need to add but I usually just go through my new modules and import whatever is missing and also take away whatever I don't need now I went ahead and wrote these down and some snippets beforehand so that you don't have to watch me slowly go through and add and remove all these one by one now if you don't feel like pausing the video and copying these imports down and each file then remember I do have these code snippets available in the description section below as well so I'm going to pull up those snippets and then put these into their relative files so I can see here that these imports belong to the main route stop py file so I will put those in there so in our main route stop py file here at the top I will add these in and actually in my snippets I'm including the blueprint import as well so we can actually get rid of that single line because we're importing the blueprint here at the end okay so let me grab the rest of these so in our post forms dot py file these are going to be our imports so I will open up our post forms and paste in those imports there and now in our post route snap py file these are going to be the imports so I will pull up our post routes top py file and replace that top import with our new ones here and let's just keep going there's only a couple more here so in our user forum step py file I will put these into our user packages form dot py file so at the top here I will add those in and in our user routes dot py file these are our cleaned-up imports for that so user routes paste in those imports and then lastly we have our user utils imports so I will copy those and open up our user utils and paste those in and I'm gonna do a save all to make sure that I've saved all of those files that we just edited now I've got to be

### [15:30](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=930s) Deleting Imports

honest fixing imports like that is probably the biggest pain in terms of moving over into blueprints but once you have it done it really is nice or having everything split up into their own cohesive sections now some of those imports actually changed as well so it's not exactly as simple as just moving them around to the correct place sometimes you actually have to change where they came from so for example let me show you if I open up our user routes dot py file here so in our users blueprint these are our routes and you can see here now that whenever I'm importing our user forms instead of importing from flask blog dot forms like we did before now we are using flash blog dot users dot forms because now we are in our flask blog package and our users package within that flash blog package and then our form stop py file and you can also see here that instead of our safe picture and send reset email being functions that are mixed in with our routes now they are actually coming from this utils module here so flash blog dot users dot utils ok so now with all that done we can now delete our old forms and routes file that we were using before because now we've moved everything out of those and into their own blueprints so within our main application package here I'm going to get rid of our previous forms file and delete that and our previous routes file so I'll delete that ok so now let's open our flash blog packages and knit dot PI file where we were importing those routes before so if I open this up and up here in the top I'm just going to close down these old files that we just deleted because they are still open there okay so in our applications a knit PI file we can see that at the bottom we are importing our routes but we just

### [17:23](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=1043s) Registering Blueprints

deleted that file so now how do we get our routes that we just added to our blueprints to work so to do this we're going to import those blueprint objects from each of those packages and register them with our route so to do this we can replace our old routes import here since we're no longer using that and we can say from flask blog and let's first do our users so we'll do users dot routes and from that routes top py file we're going to import users now this users here this is the name of the variable in our users route that is the instance of our blueprint class so if I switch over to our user route we can see here that this is what we are importing this blueprint instance here so it switched back to our Annette PI and now we can register that blueprint by saying app dot register underscore blueprint and then pass in that blueprint that we imported so now we can do this with our posts and our main blueprints as well so I'm going to copy this line here and paste this in twice and we want to import from flash blog dot post routes import the post blueprint and from flash blog main dot routes import the main blueprint and now let's register both of those as well so down here where we are registering this blueprints I will register the post and main ok so we have

### [18:52](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=1132s) Searching for Blueprints

I will register the main ok so we have one more thing to do here before we can run our application and see if this changed to using blueprints has worked and this is another step that will seem tedious right now but it will pay off with a more maintainable application once we're finished so in our application so far we have been using the URL for function to link to all of our routes and well now we need to change all of those to be the URL for the relative blueprints so it used to just be the function name of the route but now it's the blueprint name followed by the function name so we need to do a search on our whole application and change all of these now the editor that you're using should have a find functionality to search through all of the files and if you're following along in sublime text and have the same sublime setup that I have then you can right click on our project directory here and then we can copy our path and now I'm going to go up here to find and choose the option find in files and then

### [19:54](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=1194s) Adding a Folder

in the where location I'm going to paste in the path that we just copied well it doesn't seem to be working for some reason right now but we can just add this location manually if we click over here on the side then we can click on add folder and then I'm going to add our application folder which I have called flash blog so I'll add that and now I'll do a search in our files layer here for URL underscore 4 so this will search our entire project directory for any URL for text and we can go in and click on this and it will take us to that file where this text is located and then we can change this to what it needs to be so for example here this one is a URL for our home page but this is now going to be our blueprint followed by the route function so this is going to be main dot home and we're gonna have to do this for all of these so if I go back now and click on our second result then we can see that this is aur URL for post but this is now going to be our post blueprint followed by dot post route so there are a lot of these and you're going to need to make these changes in the templates too and for the sake of time I'm just going to go ahead and skip through me changing the rest of these and we'll pick up the video once again when I finish this so I'm gonna go through the entire project and change all of these URL for links right now and I'll pick up the video once I am done ok so I just went through and changed all of the URL for functions to our new values now if it wasn't clear exactly what I was doing there then let me say this again so in our new packages so we have our main package here with our main routes and our user packages with our user routes we want to go through and change all of the URL for functions to be equal to so for example let me bring up my layout dot PI file here and show you what I had to do in this file so all of these URL for functions we can see that we have our home route and our about route and those come from our main blueprint so we had to put main before those route names and down here our new post route that comes from our post blueprint so we had to put a post dot new route our new post and also with our other links here our account and logout and login and register pages the all come from our users blueprint so we had to do users dot and then the function name of that route so again that is tedious to do but like I said I have all of this code available for download if you'd like to download that and make sure that you haven't missed anything okay now there's one last value

### [22:27](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=1347s) Changing Login Manager

that we need to change and this one is easy to miss because it's not within a URL for function but if we look in our Annette dot PI module here we can see that we have this login manager login view set to this login route here now this is how the login manager knows where to redirect us when a page requires a login and that also needs to be set differently now so instead of just login this needs to have the blueprint before it so this is in the user's blueprint so we can say users dot login ok so now that we've made all these changes let's bring up our site and make sure that it still works the way that we expect so I will go to our terminal here now I'll clear the screen and run our development server so python run dot pi ok so we don't get any errors so that is good so now let's open up our application and now let's reload this and just kind of click around to see if these routes look like they're working ok so we're on the home page here and the pagination still seems to be working so that's good if we go to a new post page account page can we change our account picture and it looks like we can so that is good and just to test a little bit more let me also try to update a post here so I will change this my latest post to be my latest blog post and we can see that updated successfully so it looks like our website functionality is working well now I'd like to point out at this point that we haven't done any unit testing on our application yet and I'd like to make a video in the near future showing how we can write unit tests for this application but it's times like these where unit testing is so useful because instead of just opening our application and clicking around to make sure everything seems to be working you could actually run extremely detailed tests in just you know less than a second to make sure that every bit of functionality and your application is working exactly as you expect it to so I'll definitely be sure to do a video in the near future showing how we can test applications like this and speaking of testing there are a couple more changes that I want to make to our application before we finish this video and these changes will have a lot of benefits one of which is that it will make our application a lot easier to test so I want to show how we can put the configuration values for this application into their own file and also how we can move the creation of our application into a function so that we can create instances of our application with different configurations so first of all let's move all of our configurations into a config file so I'll go back to our application here and we will create a new file within our main package here well not our main package but our flash blog package and we will call this config dot PI so I will create that and now within config dot pi we want to take all of the configuration values and put them here but we're actually going to make this class base so we need to first create a class and this will allow us to have all of our configuration contained in a single object and will allow us to do cool things like you know using and Hairy inheritance to create new configurations and things like that so I'll create a class here and we'll call this config so class config and within this config class we want to paste all of our current configurations and those are in currently in our net dot pi file here so I'm going to grab anything that is has app config and I'll paste those into our configuration class so I will cut those values out there and paste those in and now I will go and grab these mail server values here and paste these into our configuration as well now let me get the indentation correct but

### [26:19](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=1579s) Changing AppConfig

now within this config class we don't want these to be within this app dot config we just want them to be constant variables with the same names as the keys so I will remove these app config sections here and also the ending parts of these two where we just have what those key name one key names were we just want those to be constant variables there so you want to be sure that the quotes were also removed around those variable names and then the values are just going to remain the same okay so now that we have those set up now let's use some best practices and keep some of the secret information out of our source code and move these to environment variables now I've already done this with my gmail username and password but let's also do this for our secret key and our database URI now it doesn't look like there is any secret information in our database URI right now because we're using SQL Lite but when we are using something like Postgres then the user name and the password are going to be in the connection string so that is why we want to get in the habit of moving these two environment variables now I'm on a Mac but I have videos on how to set environment variables for Mac Linux and Windows so if you're on Windows and don't know how to do this then I will leave a link to that video in the description section below but I'm going to go ahead and move these values to environment variables and to do this on a Mac I'm first going to copy these values and now I'm going to pull up my terminal here and shut down our development server and clear the screen and now I'm going to edit the dot Bash underscore profile file in my home directory and I'm going to use sublime text but you can use anything so I'm going to use sublime and edit my bash underscore profile and I have this set up to open in sublime with that command but you can open it manually if need be so now I'll paste in those values and my bias profile may look different than yours so if it is then don't worry about that also you'll notice that my gmail user name and password isn't in here I set those in a private dot file so that I didn't show these on video ok so now within our dot file here which is a bash file we need to export those values and also remove the space between the variables and the equal sign so I will say export for both of these and we need to remove the spaces between the variables and the values ok and that is all we need to do to set those and our bash profile so now back here and our config now we can actually grab those from our bash profile so duo s environ get and now pass in the values that we set those in our environment variables and we just called those the exact same thing that they are listed here so we will say get secret key and get SQL database URI now we actually have an imported this OS module yet in order to use that so here at the top of the file we need to import that so import OS and then save that and so we could set default values here too so if a database URL isn't set then we could just specify that we want an SQLite database by default but in this case I'm going to assume that these environment variables are going to be set and not to provide any default okay so now back in our Annette dot pi file how do we now

### [29:47](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=1787s) AppConfig from Object

tell our application that we want to use those configuration values that we just set and to do that we can simply do this by using the app config dot from object method but first we're going to have to import that config object that we want to use for the configuration so here at the top I will say from flask blog dot config and we will import that config class that we just created and now right under the creation of our flask instance where we create this app variable we can say app dot config dot from object and then we can pass in that object as our configuration and real quick you can see that we have a little dot here in the gutter of my sublime text and that is just my editor telling me that we now have an unused import and that is because we're no longer using this OS module in here because we move that to our configuration okay so just to make sure that didn't break anything let's reload our page in the browser and make sure that it works so I'll bring up our terminal here and rerun our application don't get any errors so if I reload the page then if we see anything on the home page and that's a good sign because it means that it found our database from that configuration a okay so that seems to be working well okay now there is one last thing that I'd like to do before we wrap up this video now like I said before I'd like to move the creation of our app into a function because of the reasons that I mentioned before it will allow us to create different instances of our application with different configurations so to do this I'm going to go back to our own it dot PI file here and down here at the bottom I'm going to create a function and I'm going to call this create underscore app so create a function here create underscore app and this function is going to take an argument for what configuration object we want to use for our application and we'll set this to be our config class that we just created by default so up in our parameters here I'll say config underscore class is equal to config and this config object here is what we imported from our config PI class which is that configuration class that we created in that module okay so now we're going to move the creation of our application inside of this create app function now this isn't as simple as just moving everything inside of this function the extensions that we're using will remain outside of the function so I'm going to grab everything but the extensions and move those in to the function so I'll grab the creation of our app here and I will not touch these extensions for now so I'll paste in the creation of our app and I will also do our blueprints so I will grab those blueprints and paste those in as well make sure that the indentation looks good and also I'll remove this line here now the reason that we don't want to move these extensions into the function is because we want them to be created outside of the function but we still want to initialize these extensions inside of the function with the application now I know that might sound confusing so let me just read the justification for this straight from the flask documentation so the flask documentation says that this is so that the extension object does not initially get bound to the application using this design pattern no application specific state is stored on the extension object so one extension object can be used for multiple apps okay so that's what it says in the docs if that doesn't make complete sense to you then it's not a big deal basically we're going to initialize the extensions at the top of our file but without the app variable so let's do that now so I will remove the app from the initialization of all of these and then inside the create app function will use the Annette app method to pass the application to all of those extensions so I'm going to copy the four extensions that we have here and paste these in to our create app function and now for each of these extensions we are going to run the anit underscore app method and that is where we are going to pass in this app okay and lastly at the bottom of our create app function we need to return the application that we created so we will return app okay so that will do it for the create app function but you may notice one big thing about this now we no longer have an app variable that we can import from this flask blog package and we use that in several places in our application because we got rid of that app variable out here and now we only have this create app function so flask has an answer for this and that is to replace all of our instances where we used the app variable with a flask import called current app so unfortunately yes that does mean that we're going to have to do another Find and Replace on our entire application but this one should be a lot faster since the app variable isn't used that much so I'm going to pull back up the search and do another search for our app variable so if I go up here to find and find in files then this still has my project directory filled in for where we want to search and I'm just going to search for app so if I scroll down to the bottom really fast then we can see that this isn't in too many places so now I'll scroll up here to the top so if I look down through the files that contain I match this is our an it PI file and we don't need to worry about that because that's the module that we're actually coming from we're not actually importing that app there next we have models dot py if we go down a little further here we can see that we have this users utils PI and other than that we just have this run dot pi and we're going to ignore run PI for now and we'll come back to it later so really the only changes that we're going to make are to our models dot pi so I'll open that up and to our users blueprint utils the first thing that we'll do in both of these modules is remove this app import because that no longer exists we got rid of that and put it inside of that create app function so I'm going to remove it from this util stop I I'm also going to remove it from our models dot pi and instead we're going to want to import current app from flask and that's actually from our flask package so we can say from flask import current underscore app and I will also do this in our utils file here we already have a flask import here so we could just add on to that and now everywhere where we use that app variable before now we're instead going to use current app so within our utils here it looks like we were using this down on line 14 for this app dot root path let me make sure it's nowhere else in the application and it's not so we can replace this app with current app dot root path and I'll save that and then we'll do the same thing in models so in models if I scroll down here it looks like we are using the secret key here and also the secret key and here in our token methods so I will replace both of those with current app okay so those are the only places where

### [37:20](https://www.youtube.com/watch?v=Wfx4YBzg16s&t=2240s) Run App

we need to change those app variables so that's a lot easier than our URL for function that we did earlier now I also said that we would look at this run dot PI file here and that we would come back to this so let's open up our run dot pi and since this is the script to actually run our application we're not going to use current app here instead we're gonna make one very small change in order to run our app so instead of importing app from our Flash blog package instead we want to import the create app function that we created there instead so back in run dot pi from flash bog import create app and now we can simply create our application by saying app is equal to create app and make sure to actually run that function and remember that we could have passed in a configuration there as an argument but it's using the config class that we created as its default so if we want to use that then we don't have to pass anything in so now that we've created that app variable then we can simply leave the run statement there as it is now so now we should be able to run our application and if we made all these changes correctly then it should all still work so let's go back to our command line and rerun our development server so we don't get any errors so that's good and let's pull up our application ok so again let me click around here and make sure that everything still looks good and I will update a blog post again well I'm not logged in here so I can't do that so I will log in so that is still working and now I'll update a blog post here so my latest post ok and that is working well so it seems like our application is still running smoothly even though we made some pretty drastic changes to the structure of our application in this video so I think that is going to do it for this video I did tell you that I'd print out the tree structure for our project here at the end of the video so let me open up my terminal and do that now so first let me open up the terminal and shut down our development server here and clear the screen and also let me maximize the terminal here so that we can see all of this and I actually paused the video and went in and removed those PI cache directories that get created so that they weren't taking up space and our tree structure so now if we say tree then I will try to resize this so that we can fit the whole structure in here and looks like I need to go a little bit more hopefully this isn't getting too small ok and so I think that fits everything there so here is our entire directory structure for application so far hopefully you can see this better than you were able to in the sidebar of sublime text but I'll leave this open while we wrap up the video so you can see here that we've got a pretty large application at this point now you'll notice that all of our templates are still in the same templates folder here we didn't move the templates into their specific blueprints although we could have done that so blueprints do have the ability to have their own static and template folders as well but I'm more of a fan of leaving all the templates in a single directory and if need be then we can create subfolders within the templates directory to make those more manageable and we'll actually see what this looks like in our next video when we create some custom error pages so we have restructured this application a couple of times now but this will be the last time so first we had a single module that we ran and then we restructured it into using packages and now we restructured it again into using blueprints so you might be wondering why I didn't just show you how to use blueprints right off the bat instead of restructuring this project multiple times and the truth is because this is how an application evolves in the real world so sometimes if you have a simple site then running a flask application with a single file and a few templates is really all you need but as it grows it makes sense to move those into packages and finally if your packages start getting hard to maintain then you can split those up into blueprints so it's good to know each step of the process depending on the complexity of your application so for example if I was making a website for a local restaurant that only had some menus and company hours and stuff like that then breaking this up into blueprints could definitely be overkill but if you have a much larger application with forms and databases and all that then it is a good idea to make it as manageable as possible and blueprints are the way to go for that so I hope in this video that you got a good sense for how to use blueprints and how you can separate out your app creation and configuration and in the next video we'll show how easy it is to add to these blueprints now that we have it set up just by adding some custom air pages but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in description section below be sure to subscribe for future videos and thank you all for watching

---
*Источник: https://ekstraktznaniy.ru/video/12283*