The Final Showdown

FLASK

DJANGO

https://media.readthedocs.org/pdf/django/latest/django.pdf

1872 pages

 

https://media.readthedocs.org/pdf/flask/latest/flask.pdf

362 pages

>> len(flask_docs) * 5.1712 == len(django_doc)

https://djangopackages.org/

https://www.djangoproject.com/weblog/2006/aug/07/guidointerview/

Modularity
by Design

INSTALLED_APPS = [
    
    # Django core
    'django.contrib.auth',
    'django.contrib.sessions',
    
    # our apps
    'users',
    'ratings',
    'places',
    'analytics'
    
    # other utils
    'catalog.apps.CatalogConfig', 
]
│   ├── users/
│   │   ├── admin.py
│   │   ├── models.py
│   │   ├── serializers.py
│   │   ├── settings.py
│   │   ├── signals.py
│   │   ├── urls.py
│   │   └── views.py
│   │   ├── tests/
│   │   │   ├── ...
│   │   ├── migrations/
│   │   │   ├── ...
|   |
│   └── ratings/
│   |   ├── ...
│   └── places/
│   |   ├── ...
│   └── analytics/
│       ├── ...
|
├── CONTRIBUTING.md
├── manage.py
├── Pipfile
├── Pipfile.lock
├── README.md
├── requirements.txt
├── run-server.sh
├── run-tests.sh
└── setup.cfg

DRY

less code to write = more projects finished

from django.db import models

class Band(models.Model):
    """A model of a rock band."""
    name = models.CharField(max_length=200)
    can_rock = models.BooleanField(default=True)


class Member(models.Model):
    """A model of a rock band member."""
    name = models.CharField("Member's name", max_length=200)
    instrument = models.CharField(choices=(
            ('g', "Guitar"),
            ('b', "Bass"),
            ('d', "Drums"),
        ),
        max_length=1
    )
    band = models.ForeignKey("Band")

You have to weed out the security.

http://flask.pocoo.org/community/poweredby/

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

https://blog.miguelgrinberg.com/post/
the-flask-mega-tutorial-part-i-hello-world

http://flask.pocoo.org/extensions/

from quart import Quart, websocket

app = Quart(__name__)

@app.route('/')
async def hello():
    return 'hello'

@app.websocket('/ws')
async def ws():
    while True:
        await websocket.send('hello')

app.run()
from celery import Celery

def make_celery(app):
    celery = Celery(app.import_name, backend=app.config['CELERY_RESULT_BACKEND'],
                    broker=app.config['CELERY_BROKER_URL'])
    celery.conf.update(app.config)
    TaskBase = celery.Task
    class ContextTask(TaskBase):
        abstract = True
        def __call__(self, *args, **kwargs):
            with app.app_context():
                return TaskBase.__call__(self, *args, **kwargs)
    celery.Task = ContextTask
    return celery

Minimal == Extensible

YAGNI

# http://flask.pocoo.org/snippets/10/

from urlparse import urljoin
from flask import request
from werkzeug.contrib.atom import AtomFeed


def make_external(url):
    return urljoin(request.url_root, url)


@app.route('/recent.atom')
def recent_feed():
    feed = AtomFeed('Recent Articles',
                    feed_url=request.url, url=request.url_root)
    articles = Article.query.order_by(Article.pub_date.desc()).limit(15).all()

    for article in articles:
        feed.add(article.title, unicode(article.rendered_text),
                 content_type='html',
                 author=article.author.name,
                 url=make_external(article.url),
                 updated=article.last_update,
                 published=article.published)
    return feed.get_response()

(easy) Modularity by necessity


simple_page = Blueprint('simple_page', __name__,
                        template_folder='templates')
app = Flask(__name__)
app.register_blueprint(simple_page)

The Final Showdown

By Vincent Buscarello

The Final Showdown

  • 283