This repo is a quick primer on using the Flask micro-framework.
The primer is done via a code progression using tags. The tags can be viewed by either execvuting git tag
in the
repository or on the GitHub Tags page. Each tag represents
added functionality in the primer.
To obtain the primer, simply clone the repository.
Git CLI Example:
git clone https://github.com/aenglander/lvpython-flask-talk.git
cd lvpython-flask-talk
Primer sections are designated with Git tags.
Git CLI Example:
$ git tag
1-hello-world
2-variable-routes
3-jinja-templates
4-reverse-routing-for-links
5-static-assets
6-form-session
Switch to the appropriate tag in the repository to show the code for a section.
Git CLI Example:
$ git reset --hard 1-hello-world
HEAD is now at e1be601 Hello World
GitHub Repository Example:
You can view the code changes between primer sections using diffs of the tags.
Git CLI Example:
$ git diff 1-hello-world 2-variable-routes
diff --git a/app.py b/app.py
index 07b952c..f489716 100644
--- a/app.py
+++ b/app.py
@@ -5,5 +5,9 @@ app = Flask(__name__)
def hello_world():
return 'Hello World!'
+@app.route('/hello/<noun>')
+def hello_noun(noun):
+ return 'Hello %s!' % noun
+
if __name__ == '__main__':
app.run(debug=True)
The tag 1-hello-world provides the basic code needed to create an HTTP response for a default route.
The tag 2-variable-routes shows how to access data provided in the URL within the controller method for a route.
The tag 3-jinja-templates shows how to use Jinja templates to return HTML from your routes.
The tag 4-reverse-routing-for-links shows how to dynamically reference route locations within a Jinja template for linking within your site.
The tag 5-static-assets shows how to reference static assets like CSS, JavaScript, and images within your Jinja templates.
The tag 6-form-session shows how to process form data, use flash messages and a cookie based session to interact with the user across requests, and redirect after a POST request using reverse routing inside the controller.