
The Flask Mega-Tutorial - giis
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
======
randomchars
> If you are on Linux, OS X or Cygwin, install flask and extensions by
> entering the following commands, one after another

I think this is a really bad advice. Instead create a file called
requirements.txt and add the required livraries, one per line.

Then you can install it with

    
    
        flask/bin/pip install - r requirements.txt
    

Edit: More information in the documentation[0]

[0]: <http://www.pip-installer.org/en/latest/requirements.html>

~~~
nfg
You can always regularise things after with:

    
    
        pip freeze > requirements.txt

~~~
blaze33
Or with:

    
    
      pip freeze -r requirements.txt
    

So you can keep your file structure with comments, nicely separating your
dependencies from the dependencies of your dependencies. This way you'll have
a much nicer time the day you need to remove one of them :)

And beware of git urls being replaced by egg names in the process.

------
irahul
I prefer keeping routes in a separate file.

Instead of

    
    
        @app.route('/')
        def index():
            pass
    

I keep a routes array

    
    
        routes = [
            ('/', endpoint='index', view_func=index)
            ...
        ]
    

I find it a lot pleasant to look up all routes at the same place rather than
guessing or searching. Also, I give an explicit name to all endpoints.

I do the same with blueprints. Blueprints have their own urls.py, and
blueprints don't specify the url prefix. For eg, a comment blueprint will
define '/', rather than '/comments'. I leave the url prefix to the main
application.

    
    
        app.register_blueprint(comment, url_prefix='/comment')
    

For migrations, I prefer alembic to sql migrations -
<https://alembic.readthedocs.org/en/latest/tutorial.html> I find Alembic
simpler than SQLAlchemy(without compromising on power). Alembic can
autogenerate migrations based on your model definitions. The way I work is I
add a new model, and then run alembic to autogenerate the migration. Same goes
for altering models.

For settings, I maintain setting.py, dev_settings.py, prod_settings.py.
settings.py contains all the common settings. If environment is not defined, I
do a `from dev_settings import *`, else I do a relevant import for the
environment.

I also prefer having BEFORE_REQUESTS, AFTER_REQUESTS, MIDDLEWARES,
LOG_HANDLERS, TEMPLATE_FILTERS, CONTEXT_PROCESSORS, ERROR_HANDLERS... in
settings, and in main.py, call the relevant functions. That way, I don't have
to hunt down the codebase. For blueprints, I define the same variables in
__init__.py.

Model forms are another time saver(use wtforms, or preferably flask-wtform)

    
    
        from flask.ext.wtf import Form
        from wtforms.ext.sqlalchemy.orm import model_form
        import models
    
    
        PostForm = model_form(models.Post, models.db.session, Form, field_args = {
            'name': {'validators': []},
            'title': {'validators': []},
            'content': {'validators': []},
        })
    

models.Post is flask-sqlalchemy model, models.db.session is sqlalcheymy
session. You don't need to list all model fields for your model form. I find
keeping models and forms in sync a pain. This way, I only keep the validations
in sync(field_args isn't required - that's an example of adding validations).

I use flask-debugtoolbar for basic profiling and debugging. flask-webassets is
awesome for managing assets and dynamic compilation. flask-script for writing
manage.py jobs(python manage.py get_deps, python manage.py db_createall etc
etc)

~~~
SEJeff
Sounds an awful lot like django the way you're using flask

~~~
irahul
I use both Flask and Django(and Sinatra and Rails). I tend to like Flask more
than Django. It's just that big applications tend to have similar structure.

Armin have done a great job with Flask. The framework is quite malleable. I
have written both 1 file flask app with inline routes, as well as 'bake-my-
own-framework' flask apps. I believe that's a good thing.

------
nonpme
The biggest problem I have with Python frameworks is the fact that it's hard
(at least for me) to configure server and just write aplications. I came from
PHP background, where you install few packages, configure apache/nginx (which
is quite fast) and things _just_ work. I'm learning python for some time now,
but I still can't create working python environment... I know django basics,
and use its server for developing, but I don't know how to configure nginx on
my VPS so I can have multiple python webapps (my simple apps, django apps,
flask etc.). Someone has good resources for that (believe me, I read a lot of
tutorials)?

~~~
derda
I haven't yet deployed a big app with flask yet, but for my "playground"
(Intranet App, very low load) I use Gunicorn (<http://gunicorn.org/> ), which
is extremely easy to set-up. For a deployment you would want to put nginx in
front of it to serve static data.

If you just want do develop and test some things the server it comes with is
absolutely enough and helps a lot with the debugging. Its as easy as typing
"python myapp.py".

~~~
nonpme
I'll check out Gunicorn, thanks for the link.

~~~
antihero
Skip it and go with uWSGI ;) Emperor mode is fantastic.

Say you have /srv/mysite/uwsgi.ini and /srv/myothersite/uwsgi.ini (uwsgi
config files for two different sites) you can run uwsgi --emperor
/srv/*/uwsgi.ini and bam, it'll launch processes for all your sites.

------
ctoth
I'd like to quickly plug Flask-Admin here, it's a much nicer experience than
writing constant CRUD code for your administrative interface:
<https://github.com/MrJoes/Flask-Admin/>

The author is quite friendly as well.

------
reinhardt
I'm between Flask and Pyramid for a new project, would love a hands-on
comparison between the two.

------
arianvanp
So I've always wanted to start learnin some python and python web dev. I've
only done webdev in node.js so far, and now I have the question : Django or
Flask?

~~~
89vision
I've really enjoyed working with flask. Django is good, but it's a bit heavy
for some projects

------
buro9
Question: Is there a way to create application modules in the same style of
Django modules (shippable extensions to an application) in Flask?

I've read about Flask blueprints, but that didn't seem to have the same
angle... seemed to be more shippable libraries rather than a module of an
application.

~~~
irahul
Blueprints are the same angle(better IMO). Develop the blueprint as a
standalone module[1](templates, url rules, static files, before handlers...),
and the mount it on the main application.

[1] Not python modules. I say module in the abstraction sense.

For eg, you can create a blueprint comments, define url rules('/', '/new',
':id/edit'...), add authentication to before handler(check before create,
delete, edit), and then mount it on the main application.

app.register_blueprint(comments, url_prefix='/comments')

You can have blueprints which are self contained modules(comments), blueprints
which just add jinja extensions, blueprints which bring in static assets etc
etc. What does Django apps do which blueprints don't?

------
QuarkSpark
just a simple question: Why do we really need Flask, when we have Django?

~~~
randomchars
Why do we need Python when we have PHP? Different people like different
things. Would you prefer if the was only one library for every purpose?

------
Kiro
I thought Flask was a minimal micro framework but this looks as bloated as
Django.

~~~
the1
and it uses a lot more RAM compared to bottle.

~~~
the_mitsuhiko
That's to be expected. Flask builds on top of Werkzeug, itsdangerous and
blinker whereas Bottle reimplements part of what Flask and the dependency
libraries provides itself. However for the vast majority of applications
having strong foundations to build upon is the right thing to do.

~~~
Goranek
Flask is awesome, great job!

