Mostrando entradas con la etiqueta flask. Mostrar todas las entradas
Mostrando entradas con la etiqueta flask. Mostrar todas las entradas

Python virtualenv for Google App Engine and Flask

Ok, this was so easy I couldn't believe it. First Download the App Engine SDK. Then install virtualenv if you don't already have. Create a new folder, for example, /opt/environments/gae and do some virtualenv magic:

$ cd /opt/environments
$ mkdir my-gae
$ virtualenv my-gae

New python executable in my-gae/bin/python
Installing distribute....done.
Installing pip....done.

Great, now I installed the SDK inside the virtualenv folder:

$ cd my-gae
$ unzip ~/Downloads/google_appengine_1.7.7.zip
$ mv google_appengine google_appengine_1.7.7
$ ln -s google_appengine_1.7.7 gae

You should be left with something like this:


├── bin
├── gae -> google_appengine-1.7.7
├── google_appengine-1.7.7
├── include
├── lib
└── local

This will allow us to update our GAE SDK version without much hassle. Now let's configure the SDK for our new virtualenv. First, edit the file "bin/activate" to look like this:

PATH="$VIRTUAL_ENV/bin:$PATH"
PATH="$VIRTUAL_ENV/gae:$PATH"   # New line for GAE
export PATH

Next time you run "source bin/activate" that line will add the SDK dev_appserver.py executable to our path.

Finally, we need to add our path configuration for the GAE path. Create a new file lib/python2.7/site-packages/gae.pth with the following:

$ cat lib/python2.7/site-packages/gae.pth
/opt/environments/my-gae/gae
import dev_appserver; dev_appserver.fix_sys_path()

Let's see if everything works.

$ cd /opt/environments/my-gae
$ source bin/activate
(my-gae)
$ dev_appserver.py
usage: dev_appserver.py [-h] [--host HOST] [--port PORT]
                        [--admin_host ADMIN_HOST] [--admin_port ADMIN_PORT]
...
dev_appserver.py: error: too few arguments
(my-gae)

$ python -c "from google import appengine"
(my-gae)

We're done with the GAE SDK, now let's go with Flask, which is a bit of a pain, specially Flask-Babel. The easiest thing to do is to use one of the already set up projects like flask-appengine-template or http://gae-init.appspot.com/ which include everything you need.



Stars filter for Jinja2 + Bootstrap

Here's a simple Jinja2 filter to add stars to a template:

Jinja2 template datetime filters in Flask

Well, I'm really liking Flask for the moment. It's light, convenient and well documented. Also, Jinja2 is quite powerful and things are building up pretty fast.

A filter alters a variable in a template. It's mostly for formatting purpose. In the template, it's separated from the variable by a pipe character (|). See Template Designer Documentation for details. There's a bunch of builtin filters, covering a wide range of cases. But sometimes, you may need more. Fortunately, it's pretty easy to add new Jinja2 filters in Flask.

In Jinja2 a filter is a block of code that is applied on user demand on a given variable in a template.

 See Template Designer Documentation for details. There's a bunch of builtin filters, for many different things. In our task at hand, we don't have a filter to localize dates, so let's build one.

First, you need to have Flask-Babel up and running. Next we will create our filter:


On your Jinja2 templates, you only need a datetime and apply the date filter:

{{ obj.date | date }}

Or pass it a different format

{{ obj.date | date(_('%%Y.%%m.%%d')) }}

The only "magical" parts here is the gettext('%%m/%%d/%%Y') and _('%%Y.%%m.%%d'). Now you can add to your different .po files the different formats to use for each language.


Hello World Flask and NetBeans

Let me explain how easy it's to get started with developing a Python backend with tools as NetBeans, virtualenv and Flask.

First, download the latest version of NetBeans 7.3 PHP (this is the lightest version). After installing it, install the Python plugin repository:

http://deadlock.netbeans.org/hudson/job/nbms-and-javadoc/lastStableBuild/artifact/nbbuild/nbms/updates.xml.gz

(You might also want to remove the PHP related plugins)

After restarting NetBeans we're ready to setup our Python virtual environment. In Debian based distributions install with:

$ sudo apt-get install python-virtualenv

Next we setup our first environment flask-env:

$ mkdir python-environments
$ cd python-environments
$ virtualenv flask-env
$ cd flask-env
$ source bin/activate
$ mkdir apps

Inside flask-env you should get something like:


.
└── flask-env
    ├── apps
    ├── bin
    ├── include
    ├── lib
    └── local

This is fairly the same structure you'll see in Python's site-package folder.

To setup NetBeans to use the new virtualenv we'll need to create a new "Python platform" which you'll find in Tools -> Python Platforms. Simply create a New platform and point it to the virtualenv python binary in python-environments/flask-env/python/bin/python

Finally, let's setup Flask for our Hello World example. Go back to your console were you executed source and execute:

(flask-env)$ easy_install Flask

Create a new Python proyect on NetBeans in the apps folder of your virtual environment and specify the new Python platform you just created (you can set it as default).




Copy/paste the Hello World example from Flask site, click on Run and point your browser to http://localhost:5000/