Skip to content

Commit f985680

Browse files
committed
add restful api flask tutorial
1 parent e863537 commit f985680

File tree

8 files changed

+68
-0
lines changed

8 files changed

+68
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
245245
- [How to Build a Web Assistant Using Django and OpenAI GPT-3.5 API in Python](https://www.thepythoncode.com/article/web-assistant-django-with-gpt3-api-python). ([code](web-programming/webassistant))
246246
- [How to Make an Accounting App with Django in Python](https://www.thepythoncode.com/article/make-an-accounting-app-with-django-in-python). ([code](web-programming/accounting-app))
247247
- [How to Build a News Site API with Django Rest Framework in Python](https://www.thepythoncode.com/article/a-news-site-api-with-django-python). ([code](web-programming/news_project))
248+
- [How to Create a RESTful API with Flask in Python](https://www.thepythoncode.com/article/create-a-restful-api-with-flask-in-python). ([code](web-programming/restful-api-flask))
248249

249250
- ### [GUI Programming](https://www.thepythoncode.com/topic/gui-programming)
250251
- [How to Make a Text Editor using Tkinter in Python](https://www.thepythoncode.com/article/text-editor-using-tkinter-python). ([code](gui-programming/text-editor))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Create a RESTful API with Flask in Python](https://www.thepythoncode.com/article/create-a-restful-api-with-flask-in-python)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from flask import Flask
2+
from flask_restful import Api
3+
from models import db
4+
import config
5+
from resources import TaskList
6+
7+
# Create the Flask application and the Flask-RESTful API manager.
8+
app = Flask(__name__)
9+
app.config.from_object(config)
10+
# Initialize the Flask-SQLAlchemy object.
11+
db.init_app(app)
12+
# Create the Flask-RESTful API manager.
13+
api = Api(app)
14+
# Create the endpoints.
15+
api.add_resource(TaskList, '/tasks')
16+
17+
if __name__ == '__main__':
18+
# Create the database tables.
19+
with app.app_context():
20+
db.create_all()
21+
# Start the Flask development web server.
22+
app.run(debug=True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SQLALCHEMY_DATABASE_URI = 'sqlite:///tasks.db'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from flask_sqlalchemy import SQLAlchemy
2+
3+
db = SQLAlchemy()
4+
5+
class Task(db.Model):
6+
id = db.Column(db.Integer, primary_key=True)
7+
description = db.Column(db.String(200), nullable=False) # nullable=False means that the column cannot be empty
8+
9+
def __repr__(self):
10+
# This method is used to print the object.
11+
return f'Task {self.id}: {self.description}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask
2+
Flask-RESTful
3+
Flask-SQLAlchemy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from flask_restful import Resource
2+
from flask import request
3+
from models import Task, db
4+
5+
class TaskList(Resource):
6+
def get(self):
7+
# Get all the tasks from the database.
8+
tasks = Task.query.all()
9+
# Convert the tasks to JSON and return a response.
10+
task_list = [{'id': task.id, 'description': task.description} for task in tasks]
11+
return {'tasks': task_list}
12+
13+
def post(self):
14+
# Get the JSON data from the request.
15+
task_data = request.get_json()
16+
# Check if the data is valid.
17+
if not task_data:
18+
return {'message': 'No input data provided'}, 400
19+
description = task_data.get('description')
20+
if not description:
21+
return {'message': 'Description is required'}, 400
22+
# Add the task to the database.
23+
new_task = Task(description=description)
24+
db.session.add(new_task)
25+
# Commit the task to the database.
26+
db.session.commit()
27+
# Return a message to the user.
28+
return {'message': 'Task added', 'task': {'id': new_task.id, 'description': new_task.description}}
29+
8 KB
Binary file not shown.

0 commit comments

Comments
 (0)