Production Flask: Gunicorn workers, Nginx reverse proxy, environment variables, Docker containers, and the minimal CI/CD pipeline that ships code safely.
~1 hourIntermediateHands-onPrecision AI Academy
Today's Objective
Production Flask: Gunicorn workers, Nginx reverse proxy, environment variables, Docker containers, and the minimal CI/CD pipeline that ships code safely.
01
Building a JSON API
API views
from flask import jsonify
@app.route('/api/posts', methods=['GET'])
def api_posts():
posts = Post.query.filter_by(published=True).all()
return jsonify([{'id': p.id, 'title': p.title, 'body': p.body} for p in posts])
@app.route('/api/posts/', methods=['GET'])
def api_post(id):
post = Post.query.get_or_404(id)
return jsonify({'id': post.id, 'title': post.title, 'body': post.body})
@app.route('/api/posts', methods=['POST'])
@login_required
def api_create_post():
data = request.get_json()
if not data or not data.get('title'):
return jsonify({'error': 'title required'}), 400
post = Post(title=data['title'], body=data.get('body',''), user_id=current_user.id)
db.session.add(post)
db.session.commit()
return jsonify({'id': post.id}), 201
CORS
pip install flask-cors
from flask_cors import CORS
CORS(app, resources={r'/api/*': {'origins': 'https://yourfrontend.com'}})
Completing all five days means having a solid working knowledge of Flask in 5 Days. The skills here translate directly to real projects. The next step is practice — pick a project and build something with what was learned.