First Flask Project - Flask Tutorial Series #1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back this video today is going to be the first episode of a new tutorial series here on neural 9 about flask development now flask is a python backend web development framework and in this tutorial series I want to cover the basics and also some intermediate Concepts so how to create a new project what virtual environments are how to work with routes and forums but also stuff like session management uh static files uh user authentication connecting to databases working with blueprints and so on and if this tutorial series uh is popular if you guys like it I'm going to probably make also an advanced flas tutorial Series in addition to this one today we're going to talk about how to create a first project how to create a virtual environment and what flask basically is and how it works so let us get right into [Music] it all right so we're going to start in this first episode today by creating our first simple flask application a simple hello world application just to get to know the process of creating and running a flas app so it's going to be very beginner friendly and simple now as I already mentioned flask is a python web development framework and it's often times also referred to as a micro framework or a microweb framework because it's so simple and minimalistic compared to something like Django when you create a new flask application you don't have any functionality out of the box you don't have an authentication system or anything like that you basically have to implement everything from scratch or you have to install additional packages that implement the functionality we're not going to go into the advantages and disadvantages we're not going to compare here Django versus flask we're going to go into flask but it's important to know that flask is very simple and minimalistic you can basically build an application with a single file you don't have some uh project structure that has to be in place in order to run a simple hell World project we're going to see that this is the case in a second here so the first thing we're going to do is we're going to create a virtual environment and the reason you want to create a virtual environment is because you want to have have an isolated uh project where you only have the packages and also the package versions that are relevant to this particular project there are many reasons you want to do that but one reason is that it's very uh convenient to then create a requirements txt file to then deploy the application to containerize it and so on uh but there are also other benefits so what we're going to do first is we're going to open up a command line so on Windows CMD on Linux and Mac the terminal and we're going to navigate to the directory that we're going to be working in in my case this is the current directory and here now we're going to create our flask uh project directory so we're just going to create a directory I'm going to call it now first app and in this directory here in this first app directory we're going to create a virtual environment for this you don't need to install any python packages this is something that works with python out of the box it's part of the core python uh installation and what we need to do is we need to say python or Python 3 depending on your operating system - mvn and then the name of the virtual environment directory I like to call itvn which makes it a hidden directory and once you have this once this is um created you can see that we have this virtual environment here and depending on your IDE depending on the editor that you're using you will have to configure that now the environment that is being used is this environment because by default it's going to use the environment it always uses uh in my case here in py charm what I have to do is I have to click here um on the python interpreter and I have to oh actually I'm blocking this with my camera let me just move this for a second I have to click on the python version down here I have to add a new interpreter a local interpreter and then basically I have to go to Virtual n environment existing and then I basically have to look for this so I have to in this case I have my prepared projects here uh I have to go to python current VN bin and then I want to use Python 3 this is the executable or the binary I'm interested in I click on this I click okay and now my pie charm uses this virtual environment um in addition to that the terminal that I want to use I also want to activate this environment in so I want to say Source at least on Linux and Mac source and then vnf bin and then activate and now you can see it's using the vnf environment how can you check if this is actually working you can just type pip or pip 3 freeze and you're going to get a list of all the packages that are installed in this case no package is installed now once you have the virtual environment running and activated what you can do or what you have to do is you have to install flask so pip or pip 3 install flask and then this is going to install uh everything that's needed for a basic flask application I can run pip freeze again and now you can see I have a couple of packages here installed in this envir environment so after this what we want to do is we want to create a python file called app.py and this is actually all we're going to need for this video today we don't need any uh directory structure we don't need models we don't need anything like that we can just have a simple python file and this is going to be our whole application of course when you build more complex projects you still have a directory structure you have templates you have Blueprints and so on we're going to talk about this in future episodes but for a simple flask application one file is enough so what we're going to do here now is we're going to import from flask flask with a capital f so from flask import flask and then we're going to create the application by saying app equals flask and we're going to pass here uncore uncore name uncore uncore so this is how you create the application and now what we do with this application is we add end points or routes you could say we're going to talk about routes in the next episode but for this video we're going to just create a simple uh default route a simple index route so what we're going to do is we're going to use the decorator so at app. Route and we're going to specify the rule so basically the path that this uh route is going to to belong to and the default is just slash so app route slash and then below that we want to create a function that is going to return something uh which is what we're going to see the HTML code we're going to see when we go to that endpoint so here we're going to say now def to create a new function index and here you can return something now later on we're going to render HTML templates we're going to do it professionally but for now you can just return any text you can just return hello world for example and this is what you're going to see when you go to that specific endpoint now this is now the endpoint to actually run the application what we're going to do is we're going to say if uncore name uncore equals uncore _ maincore that's just a basic uh python main section um what we're going to do here is we're going to say app.run and we can provide a couple of parameters here the three important parameters that we're going to provide here are the host the port and whether we want to run this in debug mode or not so we can say host equals and what you want to do is you always want to run the application on the Local Host so on the host of The Machine uh not necessarily on Local Host but on the local IP address so if you're running this on a server you can provide the IP address that you can find with the terminal so for example something like 192 168 and so on um or you can provide Local Host if you just want to run this locally or you can provide both by passing 000000 this is going to basically use the local IP address and Local Host so you can keep it simple and just provide this and then you can also provide a port I'm not sure what the default Port is actually see what the default Port is um and then you want to provide debug equals true now you don't want to provide debug equals true when you're actually running this on a server when you're actually deploying this but while you're developing this application you want to run it in debug mode because then you don't have to constantly restart the server you can just keep it running change the code and it's going to update automatically uh and you're also going to see the error messages and stuff like that uh but you want to turn this to false once you deploy the application so um right now I can just run this I think the default Port is yeah 5,000 but we can also change this for example you can say Port equals 5555 if you want to then you can run this and you can see it's running now on Local Host and also on the private IP address and when I click on this it will open up a web browser and this web browser goes automatically to the default route slash and I get hello world as an output here and you can of course also change this to be HTML code so you can say here H1 and then H1 um yeah and and then this is going to when you save this it will update because you're running this in debug mode I can click on it and you can see now I have a heading here so this is the basic idea of a simple flask application what we're going to do now in the next episodes is we're going to add more routs we're going to handle different types of requests we're going to add uh Dynamic routes we're going to add post requests and Status codes and templates and blueprints and all that but this is where it all starts this is the simple basic hello world application uh in flask and of course what you can also do is you can run this in the terminal so you can just say Python 3 app.py this also works and uh just as one thing up front you can now take all the packages let's say this is now your application you want to deploy it somewhere all you have to do for for example to push it to GitHub so that people can uh use it you want to do something like um pip 3 freeze and then you want to pipe this with uh with the greater than sign with the closing angle bracket you want to uh feed this into a requirements.txt file and when you have that you have this requirements txt file and then people can just go ahead and say pip 3 install dasr requirements TX and they can install all the packages that you have installed in this particular virtual environment so that's it for today's video I hope you enjoyed it and I hope you learned something if you like this tutorial series if you want to see more flas content if you maybe want to see also an advanced tutorial series let me know in the comment section down below depending on how much you guys enjoy these videos how much you watch them I'm going to make more videos I'm definitely going to finish this tutorial series but as I said if you guys watch this a lot like this a lot I'm going to make more videos more advanced videos on flask development as well well so thank you very much for watching see you in the next video and bye
Info
Channel: NeuralNine
Views: 19,583
Rating: undefined out of 5
Keywords: python, flask, tutorial, course, tutorial series, neuralnine, flask python, first project, hello world, first flask project, first app, first application, first flask application, flask beginner tutorial, flask course, flask beginner course, flask crash course
Id: o3bCVqF9gI0
Channel Id: undefined
Length: 11min 19sec (679 seconds)
Published: Tue Feb 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.