A Flask Mega-Tutorial: Part I

Welcome to what may be the first of many in a series on Flask, which I’ve sunk my teeth in to just last weekend. I may note that in operations, I have worked with Python before, and more specifically some Flask also. This means I’m familiar with many of the concepts, and yet it is a learning curve.

Inspired by the existing The Flask Mega-Tutorial by Miguel Grinberg, I hope that by the end of this blog post, I have sparked enough interest in following through on this roll, and you can see why I’m doing a mega-tutorial specifically along the lines of my own learning curve as well.

This particular mega-tutorial, deliberately “a” mega-tutorial rather than “the” mega-tutorial, is based on my own learning curve. I have developed a sense of specific needs for my application — which might hypothetically make it to production some day — and so far I’m just spending weekends and evenings on it, partly out of curiosity, and because I have an interest in hacking.

That said, this episode is all about the basics. Get yourself a GIT repository for an application we shall call ppppp (why? ’cause that name doesn’t conflict):

$ mkdir ppppp.git
$ cd ppppp.git
$ git init

I recommend you consider configuring GIT so you get prompt indications similar to mine, but much more on that in another post. Suffice it to illustrate that this is my regular prompt:

[kanarip@kanarip ~]$

And this is my prompt when I navigate in to the project I’m currently working on (with Flask, no surprise there, I suppose):

[kanarip@kanarip someapp.git (master *+%)]$

For the sake of preserving that little horizontal space we have available, though, I shall just continue to refer to the prompt with $.

Add a virtual environment. This creates a directory layout with Python and Pip installed, and some convenience scripts for you to load that environment.

$ virtualenv venv
$ source venv/bin/activate

Note that you should NOT add this virtual environment to your GIT repository. I have it inside the GIT repository though, because I can add the directory venv/ to .gitignore:

$ echo venv/ >> .gitignore
$ git add .gitignore
$ git commit .gitignore -m "Add .gitignore"

You are now ready to pull in the following packages:

(venv)$ pip install Flask Flask-Assets Flask-Babel \
    Flask-Cache Flask-Fixtures Flask-Migrate \
    Flask-RESTful Flask-Script Flask-SQLAlchemy \
    Flask-SQLAlchemy-Cache Flask-Themes Flask-WTF \
    cssmin jsmin celery country-currencies coverage \
    openexchangerates pycountry pygeoip Sphinx \
    SQLAlchemy-i18n nose

Do I have your attention now?

UPDATE: Part II is now available.

UPDATE^2: In writing Part III on testing, I discovered some typos. Yey for testing!