 Right, so hey guys and welcome back to another Python tutorial. So in this tutorial We're going to be learning about how to use Flask and create our very own web server or back-end for our website using the Flask Framework, so what we're going to do first off is actually go ahead and create a new folder So that we can store all our website files So I'm going to call my folder my website and then in there We're going to be storing all the different files that are going to be used by a website Cool. So the first thing you want to have in here is actually your web server, which by naming convention According to the docs is app.py so this is going to be your main file that the Flask reads first as soon as the server is started cool So once you have your main file in there, what you want to do is you want to open this file in your favorite text editor I'm going to open up with your studio code and in there. I'm just going to drag this file So before you actually get started with anything. We actually need to go ahead and install We need to go ahead and install Flask So go ahead and type in cmd and then you can type in pip install with a capital F Flask and then double equals 1.1.1 so that we have the same version So give it a second and then once it's given you a successful installation You can carry on with a tutorial obviously. I don't need to wait because it's already installed So what you want to do first off is go in your app.py and do from Flask, which is the module We just installed import the Flask Clask which is with a capital F Now what you want to do next is create an app variable Which is going to be the object that stores the Flask class once it's initialized So app equals Flask and then in there you want to pass in underscore underscore name What this does is it pretty much provides the Flask the instance of the Flask Class, which is going to be an object soon It provides it with the name of this module or the name of this file so that it can run successfully so what you want to do next is actually go and Set a route for your website So when the user goes on our website, so I'm going to open up Chrome here just to show you a quick example So when the user goes on our website So let's say for example find His use gidino.com. That's my website being hosted on a custom web server. Obviously, I have encoded it So when the user is directed to a website or types in a link the first thing that the first request that he or she makes is For a route with a forward slash. So this is the request that they make so that request so whenever the web server sees a get request for a Forward slash route. It just knows that this is for the home page and it needs to serve the home page or the first content to the user Now to do so what we want to do is actually go ahead and use So we use at and then we do Dot route because we're creating a route and then in brackets we need to specify what route we're creating so as I said the first route the user is always redirected to by the browser or by the Link they're using is forward slash. So that's the default one which should technically take the user to our home page So what we want to do with this app that route is provided with a function that it runs every time the user has Reached this route. So the default route So whenever the user types in forward slash or reaches the default route We want to create an index function, which is going to be one and then in this index function We're just going to return and say hello world Cool. So what's going on now is as soon as the user types in our link to this web server and then requests for the forward slash Or the initial request for this forward slash This index function is going to be run and the user will see hello world text on their screen Now obviously there's a lot of background stuff going on which is being handled by flask Which is why we have a clean Interface where we can just type in return hello world If not for flask, we would have a load of lines just to make this possible So now you guys might say how do we actually run our server and start and view the outcome of this? Right, so as soon as we're done with that what we want to do is go ahead and open up cmd So type in cmd so that we can actually start our server and then what you want to type in there is we want to Make sure that we're in the same folder as where this app.py file is so cd Desktop and then cd my website is the name of the directory in which my app.py Server is which is going to be run by flask now first of what you want to do is set some Environment variables, which are going to be used by flask. So type in set flask App and then equals that to the name of your app, which is app.py in my case So I'm going to press enter so flask knows which app to use as its web server or the main file And then you can just type in flask prompt and then technically that should just start working now As I said before or as I or I'm saying now The flask server runs by default on your local machine on the address one two seven point zero point zero point one and Add the port five thousand now if I go to Chrome really quickly Let's go to Chrome and let's type that address in now instead of typing one two seven point zero point zero point one and five thousand You can also type in okay. Let's see why this is not loading. I think it's frozen. Okay. It got frozen for a second So as you see right here when I type in that address one two seven point zero point zero point one five thousand The first request I'm making to my web server is the forward slash or the forward slash get request Which is why it returns hello world, which is what I've programmed it to do now We could also go ahead and type in localhost five thousand and it will have the same result because technically Typing in localhost or one two seven point zero point zero point one is pretty much the same thing So as you see right here our first route is working fine Now what we're gonna do is we're gonna go ahead and assume that we have another page on this website So let's just rename this to home page and then this will be our home page And then all we're gonna do is copy this code and then paste it down here Let's say we have an about page as well So we're gonna do forward slash about this time and then instead of returning home page We're gonna return about just so that we know that we're on the about page So let's save the up and go back to our browser Now let's refresh the page and type in forward slash about now when I press enter you guys may have guessed It would say about but obviously doesn't Now and this is not because we have an error in our code But this is mainly because we haven't restarted the flask server now by default when debugging is off Which is the default function on flask servers So when debugging is off the server doesn't automatically look for file changes in your Python file We need to manually go ahead and turn off the server and then turn it on again to actually notice any changes So this would be very jarring if you're coding and then you have to keep closing the server and starting it again So the simple fix to this is just to enable debugging mode so that it can automatically look for changes in your Python file and Updated update it automatically So what you want to do is go ahead and type in flask not flask set flask underscore Debug and then equals that to one which means true So we want to make sure that debugging is on press enter and then type in flask run So now as you see right here the debugging mode the debug mode on our flask server is on So it will pretty much look for any changes that have been made to our Main app.py file. So we don't need to start the server each time we make any changes. So let's refresh this Let's frozen again So we have a Error Okay, so basically it's a common mistake that we made which is pretty much forgetting to rename the function now This function index exists two times, which is why it was giving us the error So rename the function to about and what we're going to be doing is we're going to refresh the page Okay, refresh the page and as you see when we're on our Default forward slash root it shows up as home page and if I do forward slash about it's going to show me the About page now just for the sake of this tutorial, let's go ahead and add another route And let's assume that we had another page called contact So I'm going to go ahead and change this to contact and Then I'm going to change the function name this time so that we don't have the same error again, and then lastly will change return contact Now just to prove that it's automatically refreshed when I refresh the page Okay, what's wrong this time? So obviously this is all fine Shouldn't really give us the error. Let's just refresh Type in okay, so when we press localhost 5000, I think it was just cached. That's why it gave me the arrow I probably pressed the back arrow or something like that So when I refresh it shows me the home page when I am at the default route When I do forward slash about it will show me the about page and when I do forward slash Contact it will show me the contact page. Now. Obviously. I haven't had to refresh the server even once So now we basically know how to show some information based on roots now just for good practice we're going to learn about how blueprints work in Flask now blueprints pretty much avoid us from having to use a single file or Pretty much just this file to have all the roots and the functions in this file so instead of doing this we can actually use a class in Flask called blueprints and Then link all of these roots in that file and then import that file in here So it may sound confusing now, but it's going to make sense in a moment So what you want to do is go ahead and create a new file. So press on control.n I think it is yes control.n and then press on save save this file as Let's just call it app Blueprint because this is the blueprint of our app So it's pretty much going to store all the roots and the functions that are going to be one for the different roots that we have on our website So in here you want to do from Flask import capital B blueprint and then in here we're going to do app blueprint Blueprint equals Blueprint and then in here we need to provide the name of our blueprint that we're going to have I'm going to call it the same so app blueprint and then the actual name of this module so underscore underscore name Cool. So now that the requirements are satisfied. We can actually go ahead and do our routing in here So instead of having to do all of our routing in here We're going to grab all the routing that we did in this file. So I'm going to go control x Save and then I'm going to paste it in here. Oh, no, what did I do? Control X and Then save it in here. Cool. So instead of actually having to do that app.route because app.route is not defined anymore. What we're going to have to do is change that to app Blueprint.route so that it uses the blueprint variable that we just created that holds the Flask blueprint I mean object and then it's able to root is there. Now since this file is already ready You might say how do we actually link this app.app.blueprint.py file to app.py file Now the answer is pretty simple We're going to be importing it because technically any py file can be imported as a custom module or an existing module So what we want to do is make an import for the app.app underscore blueprint file that we just created. So we do from app blueprint import app blueprint So we're saying from this module called app.blueprint. We want to actually import app blueprint as this object right here So what we want to do next is go ahead and register a blueprint for this app So we do app.register app.register and then we pass in the app blueprint Variable or the class that we just imported which is for coming from here So I have an error actually so it's not app.register It's app.register underscore blueprint because we're registering a blueprint and this is just a method that is available to Flask So let's save that up refresh I need to get this Going so refreshed and as you see home page works Since the home page works the other pages should work as well. Let's try it out and let's try contact Cool, so all of the pages are working fine So that basically means that we've used a better approach and a cleaner approach of using a modular programming method where we've split the Routing and the running of functions into a different module and we're still able to use the functionality as before So obviously using it this way makes it a lot more easier to manage when you have a large code base If it's obviously a small project, you can just carry on with the first method that was shown to you Now instead of just returning normal text. We can actually return HTML text So what we're gonna do is actually return HTML attributes or HTML elements Using this routing that we have going on right here. So instead of returning just home page I'm gonna actually create an HTML tag. So an a H1 tag and then I'm gonna send that instead of just passing in a normal Text, so let's go ahead and do that for the about as well. So make this H1 and Close H1. So this is just normal HTML markup language We're creating in a header element, which is going to hold the text of Contact so it's pretty much going to make the text bold whatever text is inside and makes it bold and then it makes it a bit larger so let's refresh and as you see right here the Text we were returning is no longer just a normal string. It is actually an HTML element We can prove that by going ahead and Selecting the file and then inspecting it So if you inspect on the contact as you see right here, it's not a normal text element It is actually an HTML text element which says H1 and then you have the text in there cool So that was it for today's tutorial guys. Hope you have learned something new through this tutorial Hope I didn't overwhelm you with way too much information If you guys would like to support the channel you can do so by either signing up as a patron using the patreon link in the Description you can also purchase a super chat emoji or a highlighted message when this video premieres as it really helps Also considered joining my discord channel and adding up my socials for a load of fun And guys I will see your beautiful faces in part two of using flask using python peace