Last active
June 6, 2016 16:28
-
-
Save cadyherron/0d876d28f9a33db6668475557b331c70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Start your server with ./run.py | |
'g' is Flask's global file | |
Images, stylesheets, JS files go into app/static | |
============== | |
CONTROLLER: | |
============== | |
Route handlers are written as Python functions | |
* Question: should routes go in separate route file, or views.py? | |
============== | |
VIEWS: | |
============== | |
Dynamic variables go inside {{ ... }} | |
Control statements go inside {% ... %} | |
Control statements require {% endif %}, {% endfor %}, etc. to terminate | |
Can use existing filters: {{title|upper}} | |
Can also add custom filters: | |
@main.template_filter('trim_upper') | |
def string_trim_upper(value): | |
return value.strip().upper() | |
Template inheritance: | |
- "block" is like "yield" | |
- each block needs a unique name | |
- blocks cannot be conditional (put the conditional inside the block instead) | |
base.html (parent template): | |
{% block content %}{% endblock %} | |
index.html (child template, name is "content"): | |
{% extends "base.html" %} | |
{% block content %} | |
<h1>Hello, {{ user.nickname }}!</h1> | |
{% endblock %} | |
FORMS: | |
(1) instantiate a form as a classe in 'forms.py' file: | |
class PostForm(Form): | |
post = StringField('post', validators=[DataRequired()]) | |
(2) create the form in the html file | |
(3) add form logic to views.py (or similar) | |
Can create sub-templates/partials, like for posts: | |
{% include 'post.html' %} | |
============== | |
MODELS: | |
============== | |
Each model is defined in 'models.py', under a class with the resource name | |
class User(db.model): | |
id = db.Column(db.Integer, primary_key=True) | |
nickname = db.Column(db.String(64), index=True, unique=True) | |
============== | |
SQLALCHEMY: | |
============== | |
Can either use SQLALchemy command line tools or API, or a custom script to run migrations | |
user = models.User.query.get(123) | |
models.User.query.order_by('nickname desc').all() | |
Manually add a user: | |
>>> u = models.User(nickname='susan', email='[email protected]') | |
>>> db.session.add(u) | |
>>> db.session.commit() | |
Can profile query speeds using 'get_debug_queries' function | |
============== | |
BOOTSTRAP: | |
============== | |
/app | |
/static | |
/css | |
bootstrap.min.css | |
bootstrap-responsive.min.css | |
/img | |
glyphicons-halflings.png | |
glyphicons-halflings-white.png | |
/js | |
bootstrap.min.js | |
Then in our base template: | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
... | |
<link href="/static/css/bootstrap.min.css" rel="stylesheet" media="screen"> | |
<link href="/static/css/bootstrap-responsive.min.css" rel="stylesheet"> | |
<script src="http://code.jquery.com/jquery-latest.js"></script> | |
<script src="/static/js/bootstrap.min.js"></script> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
... | |
</head> | |
... | |
</html> | |
============== | |
TESTS: | |
============== | |
Can just 'import unittest' and write tests in 'tests.py' (better: create a unittest folder) | |
Useful tool is 'coverage', which will evaluate test coverage percentage | |
============== | |
PROFILING: | |
============== | |
One Flask profiler is called 'Werkzeug' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment