Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pip install flask_restful

pip install flask-restful
Comment

how to import flask restful using pip

pip install Flask-RESTful
Comment

python flask rest api

from flask import Flask
from flask_restful import reqparse, abort, Api, Resource

app = Flask(__name__)
api = Api(app)

TODOS = {
    'todo1': {'task': 'build an API'},
    'todo2': {'task': '?????'},
    'todo3': {'task': 'profit!'},
}


def abort_if_todo_doesnt_exist(todo_id):
    if todo_id not in TODOS:
        abort(404, message="Todo {} doesn't exist".format(todo_id))

parser = reqparse.RequestParser()
parser.add_argument('task')


# Todo
# shows a single todo item and lets you delete a todo item
class Todo(Resource):
    def get(self, todo_id):
        abort_if_todo_doesnt_exist(todo_id)
        return TODOS[todo_id]

    def delete(self, todo_id):
        abort_if_todo_doesnt_exist(todo_id)
        del TODOS[todo_id]
        return '', 204

    def put(self, todo_id):
        args = parser.parse_args()
        task = {'task': args['task']}
        TODOS[todo_id] = task
        return task, 201


# TodoList
# shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
    def get(self):
        return TODOS

    def post(self):
        args = parser.parse_args()
        todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1
        todo_id = 'todo%i' % todo_id
        TODOS[todo_id] = {'task': args['task']}
        return TODOS[todo_id], 201

##
## Actually setup the Api resource routing here
##
api.add_resource(TodoList, '/todos')
api.add_resource(Todo, '/todos/<todo_id>')


if __name__ == '__main__':
    app.run(debug=True)
Comment

PREVIOUS NEXT
Code Example
Python :: what is join use for in python 
Python :: python remove duplicates words from string 
Python :: python convert list to absolute value 
Python :: how to commenbt code in python 
Python :: how to write a file in python 
Python :: python obtain data from pandas dataframe without index name 
Python :: pip install django 
Python :: run a loop in tkinter 
Python :: python 64 bit 
Python :: python sort dict by key 
Python :: select certain element from ndarray python 
Python :: circular array python 
Python :: how to convert a byte array to string in python 
Python :: pandas groupby percentile 
Python :: pandas dataframe from tsv 
Python :: port 5432 failed: Connection timed out (0x0000274C/10060) Is the server running on that host and accepting TCP/IP connections? 
Python :: open file python 
Python :: root template 
Python :: isprime python 
Python :: python size of linked list 
Python :: pygame caption 
Python :: python reduce() 
Python :: reverse geocoding python 
Python :: docx change font python 
Python :: merge dictionaries in python 
Python :: set title matplotlib 
Python :: tkmessagebox not found 
Python :: python get string from decimal 
Python :: python send get request with headers 
Python :: Conversion of number string to float in django 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =