In this post, we are going to learn Flask, a really amazing framework to build rest APIs in Python, let start with the introduction to the Flask.
Introduction: Let’s start with the introduction to the flask.
- Flask is a micro web framework written in Python it is classified as a micro framework
- It doesn't require particular tools or libraries.
- It has no database abstraction layer for validation.
- 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 on a 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 makes adding new functionality easy enough talking.
Setup Virtual Environment:
let's get our hands dirty with Flask, so first of all, we need to set up our virtual environment and install Flask. So, you just need to install virtualenv package by running a simple command:
pip install virtualenv
Then by using this package we can create a new virtual environment by using the command:
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 virtual environment.
Activate Virtual Environment:
Now, we need to activate this by using the command:
source/myenv/bin/activate
Installation Flask:
Now to install the flask you just need to run the simple command as:
pip install flask
When you execute this command people 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 frees 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.
Create a Simple Flask Application:
Let's create a simple flask application so I will create a file named app that py and then after importing the flask as from flask import flask. I will create one of its instances that has an app equal to floss then, I will pass the Dendera name argument all philosophy functions must create an application instance the webserver passes all requests it receives from clients to this object for handling using a protocol called whiskey web server gateway interface an 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 under name then the variable is the correct value for this argument.
How does Flask Application work?
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 place, The association between a URL and the function that handles it is called a route and when a URL is 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 is exposed by the application instance. So I will say @app.route and pass the slash as the URL path and then define a view function as an index and return a simple statement inside that. So, when a client sends a request to the ‘/’, 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.
More from
Web Development
category
Introduction to Cython
No posts found in this category!