Python API for Task Management: Create, Retrieve, and Update Tasks
This is a simple Python API for managing tasks, using Flask to handle requests and JSON to exchange data. It provides three primary routes:
- '/add_task': Adds a new task. Accepts a JSON object containing task information.
- '/get_tasks': Retrieves the current list of tasks. Returns a JSON object containing all task information.
- '/update_task': Updates the status of a task. Accepts the task's ID and its new status, updating the corresponding task.
from flask import Flask, request, jsonify
app = Flask(__name__)
tasks = []
@app.route('/add_task', methods=['POST'])
def add_task():
task = request.get_json()
tasks.append(task)
return jsonify({'message': 'Task added successfully'})
@app.route('/get_tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
@app.route('/update_task', methods=['PUT'])
def update_task():
task_id = request.args.get('id')
status = request.args.get('status')
for task in tasks:
if task['id'] == task_id:
task['status'] = status
return jsonify({'message': 'Task updated successfully'})
return jsonify({'message': 'Task not found'})
if __name__ == '__main__':
app.run()
This code can be expanded upon to support more complex functionality based on your specific requirements. You would also need to develop a front-end interface to interact with this API, displaying task lists and enabling status updates.
原文地址: https://www.cveoy.top/t/topic/n03V 著作权归作者所有。请勿转载和采集!