Flask Snippets for VS Code
⚠️ Disclaimer ⚠️
This is not the official Visual Studio Code extension. This is a community-created extension that has collection of useful code snippets for Flask development in Visual Studio Code.
Features
This extension provides snippets for common Flask patterns, including:
- Basic Flask application setup
- Route definitions
- Template rendering
- Form handling
- JSON request handling
- Error handlers
- Database models and migrations
- Blueprints
- Configuration setup
- Login required decorator
- REST API development
- Form validation with WTForms
- User authentication
- Email functionality
- CORS handling
- JWT authentication
- WebSocket support
- Caching
- Admin interface
- Testing
- CLI commands
- Error pages
- Context processors
- Request/Response middleware
- Redirects and URL generation
- Session management
- File upload handling
- Pagination
- API response wrappers
- Custom middleware
- Rate limiting
- Flash messages
- Cookie management
- Query parameter handling
- Custom validators
- Background tasks with Celery
- Application factory pattern
Installation
- Open VS Code
- Press
Ctrl+P to open the Quick Open dialog
- Type
ext install flask-snippets
- Press Enter
Usage
Type the prefix of the snippet you want to use and press Tab or Enter to insert it. Here are the available snippets:
| Prefix |
Description |
flask-app |
Basic Flask application setup |
flask-route |
Flask route decorator with function |
flask-template |
Flask route with template rendering |
flask-form |
Flask route handling form data |
flask-json |
Flask route handling JSON data |
flask-error |
Flask error handler |
flask-model |
Flask SQLAlchemy model |
flask-blueprint |
Flask Blueprint setup |
flask-config |
Flask configuration setup |
flask-login-required |
Flask login required decorator |
flask-rest |
Flask RESTful API setup |
flask-migrate |
Flask database migration setup |
flask-wtf |
Flask-WTF form class |
flask-login |
Flask-Login setup with user model |
flask-mail |
Flask-Mail setup |
flask-cors |
Flask-CORS setup |
flask-jwt |
Flask-JWT-Extended setup |
flask-socketio |
Flask-SocketIO setup |
flask-cache |
Flask-Caching setup |
flask-admin |
Flask-Admin setup |
flask-test |
Flask test case setup |
flask-cli |
Flask CLI command |
flask-error-pages |
Custom error page handlers |
flask-context |
Context processor for templates |
flask-before |
Before request handler |
flask-after |
After request handler |
flask-redirect |
Redirect to another route |
flask-session |
Session management |
flask-upload |
File upload handler with validation |
flask-pagination |
Pagination for database queries |
flask-response |
Standardized API response wrapper |
flask-middleware |
Custom middleware class |
flask-limiter |
Rate limiting with Flask-Limiter |
flask-flash |
Flash messages for user feedback |
flask-urlfor |
URL generation with url_for |
flask-query |
Query parameter handling |
flask-cookie |
Cookie management (set and get) |
flask-abort |
HTTP error handling with abort |
flask-validator |
Custom validator for WTForms |
flask-celery |
Celery setup for background tasks |
flask-factory |
Application factory pattern |
Example
Type flask-app in a Python file and press Tab to insert a basic Flask application setup:
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
Requirements
- Visual Studio Code 1.60.0 or higher
- Python extension for VS Code (recommended)
- Flask-Limiter
- Celery
- Werkzeug
You can install them using pip:
pip install flask flask-sqlalchemy flask-migrate flask-wtf flask-login flask-mail flask-cors flask-jwt-extended flask-socketio flask-caching flask-admin flask-limiter celery
- Flask
- Flask-SQLAlchemy
- Flask-Migrate
- Flask-WTF
- Flask-Login
- Flask-Mail
- Flask-CORS
- Flask-JWT-Extended
- Flask-SocketIO
- Flask-Caching
- Flask-Admin
You can install them using pip:
```bash
pip install flask flask-sqlalchemy flask-migrate flask-wtf flask-login flask-mail flask-cors flask-jwt-extended flask-socketio flask-caching flask-admin
Contributing
We welcome your feedback and suggestions to improve these snippets.
License
This project is licensed under the MIT License - see the LICENSE file for details.