Skip to content

flask

This is probably just the right size framework, very basic but extendable with plug ins.

installation

This library depends on other libraries like werkzeug(wsgi) and jinja2(templates). Virtualenv is another tool that is not required but handy for python in general. It creates a separated environment for python in which you can install all packages you need. So you can mix python versions for instance for different projects. An example of how to use :

install
1
2
3
4
5
6
sudo pip install virtualenv
mkdir project
cd project
virtualenv venv
. venv/bin/activate
venv) pip install flask

You should source (.) the activate script not execute it.

routing

This is done by werkzeug, and is simply done by providing a decorator like this:

routing
1
2
3
4
5
6
7
@app.route('/projects/')
def projects():
    return 'The project page'

@app.route('/about')
def about():
    return 'The about page'

The trailing slash is subtle but differs in handling comparable to file systems paths, these commands are the same :

no difference
cd /home/kees/
cd /home/kees

That's how it works in the 'directory' form (with '/'), but in the 'file' form (/about) the distinction is :

differ
cat /home/kees/file.txt
cat /home/kees/file.txt/ 

The second one will fail, and so also gives a 404 error in werkzeug routing.

blueprints

These are almost applications, but they are registered onto an application. They do share the same configuration, also you can register blueprints multiple times on different url's etc

celery

A small warning portion about celery, which is a quit extensive task queue. I used this for the maintenance thread of the soc portal but it turned out to be too extensive. Mainly having to start up two extra processes is what turned me off.

However note that when you mix sqlalchemy + Flask + celery :

visit

You can encounter this error, strangely occurring in only the second time a task runs.

Error

OperationalError('(psycopg2.OperationalError) SSL error:

It has to do with forking after opening the database or something but more pragmatic, when you use forking it happens :

celery
1
2
3
celery -A portal worker -P prefork
# which is the same as the default
celery -A portal worker

When you start it with solo, or even let scheduling everything is fine :

celery
1
2
3
# all ok !
celery -A portal worker -P eventlet -c 10000
celery -A portal worker -P solo

celery beat

Note that if you want the scheduler to run it also needs the beat service and it has to be started in the same directory as the workers, because that's where the configuration is.

beat
celery -A portal beat

Only after you start this command, the workers start to run.