Flask is a lightweight microframework written in python. This framework is based on two things. One is Werkzeug and another is Jinja. Werkzeug is basically a WSGI (Web Server Gateway Interface) library which handles http request, response and Jinja is a templating engine using which we can write python like expression syntax in the html code.
Flask-Restful is a library using which we can quickly develop Restful APIs.
So let us start.
First let us create a directory called flak-rest-api. Now navigate to that directory and open command prompt. Next we have to install Flask and Flask-Restful packages. I will be using PIP (package management system) to install the packages.
If you are using Python version 3.4 or later then PIP is already installed but incase if you do not have pip installed in your system then you can download and install it from this URL https://pypi.org/project/pip/.
Next you have to install virtualenv. Virtualenv creates a directory which contains all the necessary executables to use the packages that your Python project would need. Now enter the below command in your terminal and install virtualenv.
Now we will create a file called requirements.txt where we mention the packages that are required for our project and will install those packages via PIP command.
So the requirements.txt will be like below:
And install this packages using below command.
Once the above installation is done then we are ready develop our RESTful APIs.
Let us create a new directory called myapp and create below files:
Now open the flak-rest-api/myapp/__init__.py and paste the below code:
Next open the flak-rest-api/myapp/RestController.py and paste the below code:
Next open the flak-rest-api/server.py and paste the below code:
To run the server go to command prompt and run the below command:
Now let us test the APIs using RESTED plugin of Firefox.
GET API
POST API
PUT API
DELETE API
Flask-Restful is a library using which we can quickly develop Restful APIs.
So let us start.
First let us create a directory called flak-rest-api. Now navigate to that directory and open command prompt. Next we have to install Flask and Flask-Restful packages. I will be using PIP (package management system) to install the packages.
If you are using Python version 3.4 or later then PIP is already installed but incase if you do not have pip installed in your system then you can download and install it from this URL https://pypi.org/project/pip/.
Next you have to install virtualenv. Virtualenv creates a directory which contains all the necessary executables to use the packages that your Python project would need. Now enter the below command in your terminal and install virtualenv.
pip install virtualenv
Once it is done then we have to activate that using the below command.
Python project would need. Now enter the below command in your terminal and install virtualenv.
virtualenv flak-rest-api
Now we will create a file called requirements.txt where we mention the packages that are required for our project and will install those packages via PIP command.
So the requirements.txt will be like below:
Flask==0.10.1
Flask-RESTful==0.3.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
itsdangerous==0.24
six==1.9.0
And install this packages using below command.
flak-rest-api\Scripts\pip install -r requirements.txt
Once the above installation is done then we are ready develop our RESTful APIs.
Let us create a new directory called myapp and create below files:
- flak-rest-api/myapp/__init__.py
- flak-rest-api/myapp/RestController.py
- flak-rest-api/server.py
Now open the flak-rest-api/myapp/__init__.py and paste the below code:
# Import the Flask module
from flask import Flask
# Import Api from the Flask-RESTful module
from flask_restful import Api
# Now create and initialise flask app
app = Flask(__name__)
api = Api(app)
# Import the RestController from myapp and register it with flask app
from myapp.RestController import RestController
app.register_blueprint(RestController)
Next open the flak-rest-api/myapp/RestController.py and paste the below code:
# Import the api from myapp
from myapp import api
# Import Blueprint, jsonify, request from flask module
from flask import Blueprint, jsonify, request
# Import Resource from flask_restful
from flask_restful import Resource
RestController = Blueprint("RestController", __name__)
class Apis(Resource):
# GET method
def get(self):
return jsonify({"message": "This is a GET method."})
# POST method
def post(self):
headers = request.headers
body = request.get_json(force=True)
return jsonify({"message": "This is a POST method."})
# PUT method
def put(self):
return jsonify({"message": "This is a PUT method."})
# DELETE method
def delete(self):
return jsonify({"message": "This is a DELETE method."})
api.add_resource(Apis, "/v1/api")
Next open the flak-rest-api/server.py and paste the below code:
# Import the api from myapp
from myapp import app
# Run the flask server
app.run(host="127.0.0.1", port=8080, debug=False)
To run the server go to command prompt and run the below command:
python server.py
Now let us test the APIs using RESTED plugin of Firefox.
GET API
POST API
PUT API
DELETE API
ReplyDeleteGreat Article
Final Year Projects for CSE in Python
FInal Year Project Centers in Chennai
JavaScript Training in Chennai
JavaScript Training in
Chennai
Master Systems are programs that are "Specialists" in a particular field and can answer inquiries identified with just that field. artificial intelligence training in pune
ReplyDelete