Labs
RESTful Labs
Flask-RESTful
Flask-RESTful is an extension for Flask that adds support for quickly building REST APIs. It is a lightweight abstraction that works with your existing ORM/libraries. Flask-RESTful encourages best practices with minimal setup. If you are familiar with Flask, Flask-RESTful should be easy to pick up. Flask-RESTful Documentation
It’s time to write your first REST API. This guide assumes you have a working understanding of Flask, and that you have already installed both Flask and Flask-RESTful. Here
root@board:~# pip install flask-restful
Downloading/unpacking Flask
Downloading Flask-0.10.1.tar.gz (544kB): 544kB downloaded
Downloading/unpacking flask-restful
Downloading Flask-RESTful-0.3.5.tar.gz (102kB): 102kB downloaded
Downloading/unpacking aniso8601>=0.82 (from flask-restful)
Downloading aniso8601-1.1.0.tar.gz (49kB): 49kB downloaded
Downloading/unpacking python-dateutil (from aniso8601>=0.82->flask-restful)
Downloading python-dateutil-2.5.1.tar.gz (235kB): 235kB downloaded
Installing collected packages: Flask flask-restful, aniso8601, python-dateutil
Successfully installed Flask flask-restful aniso8601 python-dateutil
Cleaning up...
root@board:~#
root@board:~# vi mainflask.py
#!/usr/bin/python
from flask import Flask
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)
class DataSensorRestApi(Resource):
def get(self):
data = 'This is data from a sensor'
return data
api.add_resource(DataSensorRestApi, '/datasensor')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
root@board:~# python mainflask.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 331-202-890
Connect to your boardipaddress:5000/datasensor in a web browser...
Once connected your board will display under the terminal:
127.0.0.1 - - [28/Dec/2015 15:07:38] "GET /datasensor HTTP/1.1" 200 -
127.0.0.1 - - [28/Dec/2015 15:07:40] "GET /datasensor HTTP/1.1" 200 -
Once connected your browser will display:
"This is data from a sensor"
Last updated