Repository: lucrae/flask-cheat-sheet Branch: master Commit: 9ca5ebae130f Files: 2 Total size: 8.0 KB Directory structure: gitextract_bop09grx/ ├── LICENSE └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2018 Lucien Rae Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Flask Cheat Sheet A cheat-sheet for creating web apps with the Flask framework using the Python language. ## Contents - [Creating a Simple App](#creating-a-simple-app) - [Structuring an Application with Blueprints](#structuring-an-application-with-blueprints) - [Creating Object-Based Configuration](#creating-object-based-configuration) - [Using the Jinja2 Template Engine](#using-the-jinja2-template-engine) - [Creating Models with SQLAlchemy](#creating-models-with-sqlalchemy) - [Using Database Migrations](#using-database-migrations) - [Creating a Login Manager](#creating-a-login-manager) - [Connecting to a MySQL database](#connecting-to-a-mysql-database) ## Creating a Simple App - Create a module called `app.py`: ```python from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello, World!' if __name__ == '__main__': app.run() ``` ## Structuring an Application with Blueprints - Example project tree for a project called `project`: ``` run.py project/ __init__.py config.py forms.py models.py admin/ __init__.py routes.py main/ __init__.py routes.py templates/ index.html static/ css/ style.css ``` - In `run.py`: ```python from project import app if __name__=='__main__': app.run() ``` - In `project/__init__.py`: ```python from flask import Flask from project.main.routes import main from project.admin.routes import admin app = Flask(__name__) app.register_blueprint(main, url_prefix='/') app.register_blueprint(admin, url_prefix='/admin') ``` - In `project/main/routes.py`: ```python from flask import Blueprint main = Blueprint('main', __name__) @main.route('/') def index(): return "Hello, World! This is the main page." ``` - In `project/admin/routes.py`: ```python from flask import Blueprint admin = Blueprint('admin', __name__) @main.route('/') def index(): return "Hello, World! This is the admin page." ``` ## Creating Object-Based Configuration - Create `project/config.py`: ```python class BaseConfig(object): SECRET_KEY = os.environ.get('SECRET_KEY') or 'abcdef123456' DEBUG = False TESTING = False class DevelopmentConfig(BaseConfig): DEBUG = True TESTING = True class TestingConfig DEBUG = False TESTING = True ``` - And then in `__init__.py` include: ```python from flask import Flask from threechan import config app = Flask(__name__) app.config.from_object(config.DevelopmentConfig) ``` ## Using the Jinja2 Template Engine - Rendering a template from `main/routes.py`: ```python from flask import Blueprint, render_template @main.route('/') def home(): posts = [ { "body": "Hello, this is a post", "timestamp": "This is a date and time", } ] return render_template("home.html", posts=posts) ``` - Using Jinja2 inside `templates/home.html`: ```html {% for posts in posts %}
Hello, world!
{% endblock %} ``` in **base.html** ```html {% blockcontent %}{% endblock %} ``` - Using `includes`: ```html {% includes '_post.html' %} ``` - Adding a stylesheet from `templates/base.html`: ```html ``` ## Creating Models with SQLAlchemy - In `project/models.py`: ```python from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Post(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(256)) def __init__(self, body): self.body = body def __repr__(self): return "