Flask REST Webservice creation - Part 1
This tutorial is about integratining python flask with the Oracle Jet, which is java service based framework. This tutorial has two parts first is how to create a python flask webservice and second is to develop a jet application using REST service.
Python flask is a light weight framework of python to develop web applications. It also provide quite easy way to write the rest webservices.
Prerequisite
Flask required certain libraries to install over python. Hence first please make sure you have python installed in system. Then you need certain libraries to be installed on the top of the python.
pip is the command which installed with the python and provide a quick and easy way to install python libraries.
use following commands to install libraries required
pip install flask
pip install flask_restful
pip install sqlalchemy
pip install flask_jsonpify
pip install flask_cors
once done please use following command to verify the installed packages.
pip list
Its should provide a list of the packages, which is already been installed.
Code :
Flask provide a quick and easy way to create webservice. This tutorial use sqlite as a database, which is a quite light weight portable database. If you want to use any other database you can use it but then connection ned to be created.
please use following code to create the file
from flask import Flask, request from flask_restful import Resource, Api from sqlalchemy import create_engine from json import dumps from flask_jsonpify import jsonify from flask_cors import CORS db_connect = create_engine('sqlite:///chinook.db') app = Flask(__name__) api = Api(app) CORS(app) class Employees(Resource): def get(self): conn = db_connect.connect() # connect to database query = conn.execute("select * from employees") # This line performs query and returns json result result = {'items': [dict(zip(tuple(query.keys()), i)) for i in query.cursor]} return jsonify(result) #{'employees': [i[0] for i in query.cursor.fetchall()]} # Fetches first column that is Employee ID class Tracks(Resource): def get(self): conn = db_connect.connect() query = conn.execute("select trackid, name, composer, unitprice from tracks;") result = {'data': [dict(zip(tuple(query.keys()), i)) for i in query.cursor]} return jsonify(result) class Employees_Name(Resource): def get(self, employee_id): conn = db_connect.connect() query = conn.execute("select * from employees where EmployeeId =%d " % int(employee_id)) result = {'Employees': [dict(zip(tuple(query.keys()), i)) for i in query.cursor]} return jsonify(result) api.add_resource(Employees, '/employees') # Route_1api.add_resource(Tracks, '/tracks') # Route_2api.add_resource(Employees_Name, '/employees/<employee_id>') # Route_3 if __name__ == '__main__': app.run(port='5002')
Once done please run it. If you are new to sqlite database then please use database file uploaded,
Execution.
Please run the file it will start a light weight rest webservice. you can fetch webservice data using the url http://127.0.0.1:5002/employees.
If you want to see a specific employee then you can use following url
http://127.0.0.1:5002/employees/1
Please use chrome browser for test weservice. Other browser will provide a json file as a responce, which need to first save and check.
Once this service is up and running, mean we have our basic ingredients are ready for the complete usecase. Please see next section to create/run a jet application using this webservice.
link to part 2 please click
No comments:
Post a Comment