Flask Python Interview Questions and Answers
BASIC LEVEL
1. What is Flask? How is it different from Django?
Flask is a lightweight, micro web framework in Python used to build web applications. Flask gives
flexibility and doesn't enforce a particular project structure.
Difference:
- Flask: Microframework (minimal, no ORM or admin by default)
- Django: Full-stack framework (includes ORM, admin panel, authentication)
2. What are the features of Flask?
- Lightweight and easy to set up
- Built-in development server and debugger
- Jinja2 templating
- RESTful request handling
- Extensible with plugins (e.g., Flask-SQLAlchemy, Flask-Login)
3. What is a Flask route? How do you define it?
@app.route('/hello')
def hello():
return "Hello, World!"
4. How do you start a Flask application?
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Home Page'
if __name__ == '__main__':
app.run(debug=True)
5. What is the use of debug=True?
Enables debug mode which:
- Automatically reloads app on code changes
- Displays detailed error messages
6. What is app.run() used for?
It runs the Flask development server to start your web app.
7. What are HTTP methods in Flask?
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return 'Logging in...'
return 'Login Form'
8. How do you pass variables in routes?
@app.route('/user/<username>')
def user_profile(username):
return f"Hello, {username}"
9. How do you return JSON from a Flask view?
from flask import jsonify
@app.route('/data')
def data():
return jsonify({'name': 'John', 'age': 30})
INTERMEDIATE LEVEL
10. How do you handle forms in Flask?
@app.route('/submit', methods=['POST'])
def submit():
name = request.form['name']
return f"Received: {name}"
11. What is request.form, request.args, request.json?
- request.form: Data from HTML form (POST)
- request.args: Query string parameters (GET)
- request.json: JSON body in a request (POST/PUT)
12. What is Flasks application and request context?
- Application context (current_app, g): App-level data
- Request context (request, session): Per-request data
13. How do you serve static files in Flask?
Place files in /static directory and use:
<img src="{{ url_for('static', filename='logo.png') }}">
14. What is Flask-SQLAlchemy?
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80))
15. What is Jinja2?
A templating engine used by Flask to render HTML with Python code.
<!-- template.html -->
Hello, {{ name }}
16. How do you implement sessions in Flask?
from flask import session
session['username'] = 'admin'
username = session.get('username')
17. How do you upload a file in Flask?
@app.route('/upload', methods=['POST'])
def upload_file():
file = request.files['file']
file.save(f"uploads/{file.filename}")
return 'File uploaded!'
18. Difference between GET and POST?
- GET: Appends data to URL; used for fetching data
- POST: Sends data in request body; used for creating/submitting data
19. What are Blueprints in Flask?
from flask import Blueprint
admin_bp = Blueprint('admin', __name__)
@admin_bp.route('/dashboard')
def dashboard():
return 'Admin Dashboard'
# In main app
app.register_blueprint(admin_bp, url_prefix='/admin')
ADVANCED LEVEL
20. How do you structure a large Flask app?
/myapp
/static
/templates
/blueprints
/auth
__init__.py
routes.py
app.py
models.py
21. How does Flask handle concurrency?
Flask is not inherently asynchronous. For concurrency:
- Use gunicorn with workers
- Use gevent/eventlet for async support
22. How to write tests for Flask?
def test_home(client):
response = client.get('/')
assert response.status_code == 200
23. What are middlewares in Flask?
@app.before_request
def before():
print("Before every request")
24. How do you create a RESTful API in Flask?
@app.route('/api/user/<int:id>', methods=['GET'])
def get_user(id):
return jsonify({'id': id, 'name': 'Ali'})
25. How to secure Flask apps?
- Use HTTPS
- Input validation (e.g., WTForms)
- Enable CSRF protection
- Use Flask-Login for authentication
- Sanitize user input
26. What is CORS and how to enable it in Flask?
pip install flask-cors
from flask_cors import CORS
CORS(app)
27. How to deploy Flask in production?
- Use Gunicorn with NGINX
- Dockerize Flask app
- Platforms: Heroku, AWS, Render, Railway, etc.