Transcript for:
Flask Basics and Setup

[Music] hi everyone this is abdul from pythonist i hope you all are well and safe from the chronovirus epidemic stay at home and do all precautions to save you and your family from this video we are going to learn flask a really amazing framework to build rest apis in python let's start with the introduction to the flask flask is a microweb framework written in python it is classified as a micro framework because it doesn't require particular tools or libraries it has no database abstraction layer form validation or any other components where pre-existing third-party libraries provide common functions it is designed to make getting started quick and easy with the ability to scale up to complex applications flask offers suggestions but doesn't enforce any dependencies or project layout it is up to the developer to choose the tools and libraries they want to use there are many extensions provided by the community that make adding new functionality easy enough talking let's get our hands dirty with flosk so first of all we need to set up our virtual environment and install flask so you must need to install virtual and package by running a simple command as peep install virtual and you can also install it by using brew if you're on mac by using the command blue install virtual env then by using this package we can create a new virtual environment by using the command as virtualenv then you have to write the name of the environment and you can also mention the version of python you want to use also make sure that you are in the right directory where you want to create your visual environment great now we need to activate this by using the command as source then write the name of your environment directory slash bin slash activate you can see it's activated awesome now to install the flask you just need to run the simple command as pip install flask when you execute this command pip will not only install flask but also all of its dependencies you can check what packages are installed in the virtual environment at any time using the command pip freeze the output of pip phrase includes detailed version numbers for each installed the package the version numbers that you get are likely going to be different from the ones shown here you can also verify that flask was correctly installed by starting the python interpreter then inside the python interpreter i will import flask as import flask and hit enter if no errors appear we are good to go great let's create a simple flask application so i will create a file named app.py and then after importing the flask as from flask import flask i will create one of its instances as app equal to flask then i will pass the gender name argument all flask functions must create an application instance the web server passes all requests it receives from clients to this object for handling using a protocol called whiskey web server gateway interface the application instance is an object of class flask the only required argument to the flask class constructor is the name of the main module or package of the application for most applications pythons dunder name then the variable is the correct value for this argument we will alter it but later not now if you want to get more details about this argument you can check out the written version of this tutorial i'll post a link in the description below great at this stage we have created a flask app successfully now we need to define a way for the client to send a request and how we can handle that request so here's the concept of routes and view functions comes into the place the association between a url and the function that handles it is called a route and when a url accessed by a client the flask application instance needs to know what code it needs to run for each url requested and this code is called the view function the most convenient way to define a route in a flask application is through the app.route decorator exposed by the application instance so i will say at app dot road and pass the slash as the url part then define a view function as index and return a simple statement inside that so when a client sends a request to the slash slash usually means the domain name or the ip only so it will call the index function and send the return statement back as a response that's how the most basic flask application can be created great we learned what flask is how we can install it and create a basic application with routes and a view function let's meet in the next video and explore more about flosk if you like the content of this video thumbs up and be sure to subscribe to my channel and hit the bell icon so you will never miss any fantastic video in the future thanks for watching