Learn Flask - 10 Code Examples & CST Typing Practice Test
Flask is a lightweight, WSGI-based web framework for Python. It emphasizes simplicity, flexibility, and minimalism, allowing developers to build web applications and APIs quickly without imposing a specific project structure.
Learn FLASK with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Flask Simple Counter API
from flask import Flask, render_template_string, request
app = Flask(__name__)
count = 0
TEMPLATE = '''
<!DOCTYPE html>
<html>
<head><title>Flask Counter</title></head>
<body>
<h2>Counter: {{ count }}</h2>
<form method='post'>
<button name='action' value='increment'>+</button>
<button name='action' value='decrement'>-</button>
<button name='action' value='reset'>Reset</button>
</form>
</body>
</html>
'''
@app.route('/', methods=['GET', 'POST'])
def counter():
global count
if request.method == 'POST':
action = request.form.get('action')
if action == 'increment': count += 1
elif action == 'decrement': count -= 1
elif action == 'reset': count = 0
return render_template_string(TEMPLATE, count=count)
if __name__ == '__main__':
app.run(debug=True, port=5000)
Demonstrates a simple Flask app with a counter API and HTML interface using routes and global state.
Flask Hello World API
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def hello():
return jsonify({ 'message': 'Hello World' })
if __name__ == '__main__':
app.run(debug=True, port=5000)
A minimal Flask API returning Hello World in JSON.
Flask JSON Echo
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/echo', methods=['POST'])
def echo():
data = request.json
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True, port=5000)
A POST endpoint that echoes back JSON data.
Flask Query Parameter Example
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/greet')
def greet():
name = request.args.get('name', 'Guest')
return jsonify({ 'message': f'Hello {name}' })
if __name__ == '__main__':
app.run(debug=True, port=5000)
Using query parameters to greet users.
Flask Route Parameter Example
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/users/<int:user_id>')
def get_user(user_id):
return jsonify({ 'id': user_id, 'name': f'User {user_id}' })
if __name__ == '__main__':
app.run(debug=True, port=5000)
Returns user info based on route parameter.
Flask Middleware Example
from flask import Flask, request
app = Flask(__name__)
@app.before_request
def log_request():
print(f'{request.method} {request.path}')
@app.route('/')
def index():
return 'Check console for logs'
if __name__ == '__main__':
app.run(debug=True, port=5000)
Logging middleware example using before_request.
Flask Async Example
from flask import Flask, jsonify
import asyncio
app = Flask(__name__)
@app.route('/async')
async def async_route():
await asyncio.sleep(1)
return jsonify({ 'message': 'Async response' })
if __name__ == '__main__':
app.run(debug=True, port=5000)
A route demonstrating async behavior with delay.
Flask Error Handling Example
from flask import Flask, jsonify
app = Flask(__name__)
@app.errorhandler(404)
def not_found(e):
return jsonify({ 'error': 'Not Found' }), 404
@app.route('/')
def index():
return 'Welcome!'
if __name__ == '__main__':
app.run(debug=True, port=5000)
Custom error handler example.
Flask Combined Middleware and Routes
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.before_request
def log_request():
print(f'{request.method} {request.path}')
@app.route('/')
def index():
return jsonify({ 'message': 'Welcome' })
@app.route('/echo', methods=['POST'])
def echo():
return jsonify(request.json)
if __name__ == '__main__':
app.run(debug=True, port=5000)
Combines logging, JSON POST, and routes in Flask.
Flask Template Example
from flask import Flask, render_template_string
app = Flask(__name__)
TEMPLATE = '''
<h1>Hello {{ name }}</h1>
'''
@app.route('/<name>')
def hello(name):
return render_template_string(TEMPLATE, name=name)
if __name__ == '__main__':
app.run(debug=True, port=5000)
Renders an HTML template with Jinja2.
Frequently Asked Questions about Flask
What is Flask?
Flask is a lightweight, WSGI-based web framework for Python. It emphasizes simplicity, flexibility, and minimalism, allowing developers to build web applications and APIs quickly without imposing a specific project structure.
What are the primary use cases for Flask?
RESTful API development. Backend for web/mobile applications. Microservices architecture. Prototyping and MVP development. Serving dynamic web content using templates
What are the strengths of Flask?
Extremely flexible and lightweight. Large ecosystem of extensions. Easy to learn for Python developers. Rapid prototyping and development. Fine-grained control over components
What are the limitations of Flask?
No built-in ORM or admin interface (requires extensions). Not as scalable out-of-the-box as Django. Developers manage more components themselves. Lacks built-in authentication or authorization. Can become messy for very large applications without structure
How can I practice Flask typing speed?
CodeSpeedTest offers 10+ real Flask code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.