Intro to Flask Blueprints

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone it's an the--from pretty printed here in today's video I want to introduce you to flask blueprints so blueprints in flask are a great way of organizing your project once it gets past a certain size so once you feel like a single file isn't enough and you want to grow and expand small files blueprints are probably the thing that you want to look into using so blueprints allow you to group together routes that have some commonality and an example of that would be like your profile section the authentication stuff may be a forum maybe a store all those things can be thought of as a collection of specific routes in your app whereas they're separate from the other potential blueprints you can have so there's a difference between the forum and the store for example and you can separate the code you didn't have all the store stuff and one blueprint and all the forum stuff and another blueprint so that's what I'll be showing you how to do in this video so I'm gonna create a simple example but it's going to illustrate how to use it you should be able to adapt it for more code so what I'm gonna do is I'm gonna start by creating a directory for my entire project and I'm gonna call this my app and I already have flask installed by the way now just CD into that directory actually now I'll keep it like this because I want to export the flask app to be my app so when I run flask run it will pick up my app and of course it doesn't find anything because I haven't written any code yet but I'm gonna use my app so instead of my app to start this I'm gonna need a dunder init file and what I want to do here is I want to just import flask and create a create app function so from flask import flask and I'll create a create function so create app and then I'll instantiate the flask object and I'll return that app okay so now that I have that I can start thinking about my blueprints so in this example I'm going to create two different blueprints so the first blueprint will be a blueprint for let's say the site so the part of your app that people actually see when they use it in their in their browser and then I'll have another blueprint for the API so imagine that our site in our API like they use the same database there's a lot of common utility functions between them but of course there's a different output for the site you have HTML that gets outputted and for the API is just JSON data so to create those blueprint so what I want to do is I want to create two directories one for the site and one for the API so to do that I'll make the directories so make directory sites and this should be my app site and then I'll do the same for the API all right so inside of those what I'm gonna do as a way of pretending to organize this is I'm going to put a couple of files one per directory and this will be where all my routes are so for example if I have ten routes for my API I'll put them all in this file that I'm about to create and if I have five for my site I'll put them in the file and I'll name this file routes top PI but you can name it views you can name it anything you want that just make sense you so views controllers routes whatever name but I'll use route sense and flask they're called routes so let me go ahead and create those so sites and then routes dot PI and then I'll do the same thing for the API so this should be API and then routes top pi okay so now I can open up one and this is where I'm going to create the blueprint so to create a blueprint you have to import the blueprint class from flask so from flask import blueprint capital B and to get started using a blueprint you have to instantiate it so it's actually gonna be pretty similar to how you use the regular flask app so to do this you're going to give it a name so this blueprint will be named site and I'll call this site again so you can pass in a name to the blueprint and then you have a site here and then you pass in the name of the module so dunder name and then once you have this site object or this blueprint object you can start using it like you would a route so typically you would do something like app routes and then you would specify like the endpoint for the route but what I want to do is I want to say site route so you use the name of the blueprint here to have the route decorator and then you can assign the endpoints just like you do normally so I'll call this index and our turn welcome to the to the homepage okay so I'll put this in header tags not really too important what gets returned but you know just imagine you have more routes than this so all the routes that are specific to the site would go in this one file and then I'll do the same thing for the API so I open up routes so from flask import blueprint and I'll call this one API so blueprints give it the name API passin dunder name but in addition to this I want to pass in a URL prefix and this will allow me to go to certain endpoints that will only be for that blueprint so I want to call this slash API so I'll go to my domain slash API and then everything that follows is going to be from this blueprint because I'm using that URL prefix and if I creates a routes and it shouldn't be app should be API if I create a routes and let's say I call this get data we'll see when I run the example what URL I have to go to to get this so I'll call the function get data as well and I'll return an empty dictionary so I'll just say key and then value okay so I have these two blueprints ready so how do I get them to work so what I need to do is I want to import them in here so it's going to be from I'm gonna have to sew from API routes import API and then from site that routes import sites and then what I can do is I can register those blueprints so to register a blueprint that's when you use the app objects so inside of this create app function I'll do app register underscore blueprint and I'll do that for each one so one for API and one for sites now let's see if everything is working I'll do flask run I get this import err no module name API I should be using the relative dot so just dot so I can go inside of the app itself I'll save that and let's try running it again okay so my server is running and if I go over to my browser and navigate to my local host and port 5,000 that's where the server is running we see I have welcome to the home page so this is on the index and if I want to get the API remember I have to use the API URL prefix so slash API and then I can use get data and that will return the data that I have just key value so if I just go back there to demonstrate we have this URL prefix if we change that to anything else we'll have to change the URL so anything that comes after this URL or this endpoint will then have access to the routes inside of this particular blueprint so nothing in the site can't interfere with this you don't necessarily need to have a URL prefix it just depends on how you want to organize your endpoints but for this an API it makes sense to have a difference prefix for the API compared to just the regular site so really when you're using this is just you know exceeding us so if you want to add more routes in here you can if you want to create a new module and if you want to create more blueprints you can you just have to import them in here and then register them on the app and you can start using them so I hope you were able to understand blueprints a little bit I think they're a pretty basic concept but you know they can be a little unapproachable at first but once you see an example of how they work I think they become a lot more clear so in addition to talking about blueprints I also talked about using flats equal kameez so if you want to join my free course on classical alchemy on my website just go to pretty brunette comm slash flask SQL and I'll put a link in the description below as well that you can click on and that will give you access to this course on how to use flask sequel alchemy and do various things run various queries so you can have that knowledge when you build your flask apps so that's it for this video if you like this video please give me a thumbs up and if you haven't subscribed to my channel already please subscribe so thank you for watching and I will talk to you next time you
Info
Channel: Pretty Printed
Views: 23,033
Rating: undefined out of 5
Keywords: flask, modular code, blueprints, intro to flask blueprints, flask blueprint, flask blueprints, why use flask blueprints
Id: pjVhrIJFUEs
Channel Id: undefined
Length: 9min 14sec (554 seconds)
Published: Fri Jun 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.