Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flask web development, one drop at a time

Flask web development, one drop at a time

A really quick intro to Flask.

Presented during the Python Web Framework Royal Rumble @ PyCon Sette, 2016. Competitors where Django, Pyramid and TurboGears.

Alex Martelli being in the room and validating his own quote did help to win the cause /me smirks

Nicola Iarocci

April 16, 2016
Tweet

More Decks by Nicola Iarocci

Other Decks in Programming

Transcript

  1. View Slide

  2. HELLO.PY
    from flask import Flask
    app = Flask(__name__)
    @app.route("/")
    def hello():
    return "hello world"
    if __name__ == "__main__":
    app.run()

    View Slide

  3. ABOUT

    View Slide

  4. BSD LICENSED

    View Slide

  5. BUILT BY THE PROS
    Created by Armin Ronacher
    Pygments, Sphinx, Jinja2, Werkzeug, Click

    View Slide

  6. USED BY THE PROS
    Pinterest, Twilio, Linked-in, President Obama
    Campaign, Reddit, Uber, etc.

    View Slide

  7. DEAD SIMPLE
    hello word in 7 lines of code in a single file

    View Slide

  8. EASY TO UNDERSTAND
    just write your ordinary Python code
    no pseudo-language to undestand

    View Slide

  9. MANAGEABLE
    LEARNING CURVE
    learn as you go, don’t feel overwhelmed

    View Slide

  10. NO IMPOSED STRUCTURE
    no need to understand things are the wey they are: they aren’t

    View Slide

  11. SMALL API
    800 LOC, 1500 LOC Tests

    View Slide

  12. WELL DOCUMENTED
    the equivalent of 200 A4 pages of excellent documentation

    View Slide

  13. FEATURES

    View Slide

  14. WERKZEUG
    It's hard to find anything that's simpler, more self-contained, or
    purer-WSGI than Werkzeug — Alex Martelli

    View Slide

  15. JINJA2
    modern, fast, designer-friendly templating
    language inspired by Django templates (!)

    View Slide

  16. DEVELOPMENT SERVER
    app.run()

    View Slide

  17. DEBUGGER
    app.run(debug=True)

    View Slide

  18. UNIT TESTING
    flask.app.test_client()

    View Slide

  19. RESTFUL REQUEST
    DISPATCHING
    modern web applications have beautiful URLs

    View Slide

  20. SIGNED
    SESSION COOKIES
    session allows you to securely store information
    specific to a user from one request to the next

    View Slide

  21. 100% WSGI COMPLIANT

    View Slide

  22. VERY STABLE
    (despite being in beta)

    View Slide

  23. RICH ECOSYSTEM
    Flask-SQLAlchemy, Flask-Admin, Flask-Cache,
    Flask-Cors, Flask-Login, Flask-Mail, etc.

    View Slide

  24. HELLO WORLD

    View Slide

  25. HELLO.PY
    from flask import Flask
    app = Flask(__name__)
    @app.route("/")
    def hello():
    return render_template('hello.html')
    if __name__ == "__main__":
    app.run()

    View Slide

  26. HELLO.HTML
    {% extends 'layout.html' %}
    {% block title %}Greetings{% endblock %}
    {% block body %}
    Hello {{ name }}!
    {% endblock

    View Slide

  27. LAYOUT.HTML


    {% block title %}{% endblock %}


    {% block body %}{% endblock %}

    View Slide

  28. INSTALL AND RUN
    $ pip install Flask
    $ python hello.py
    * Running on http://localhost:5000/

    View Slide

  29. EXTENSIONS RUNDOWN: FLASK-SQLALCHEMY
    from flask import Flask
    from flaskext.sqlalchemy import SQLAlchemy
    app = Flask(__name__)
    db = SQLAlchemy(app)
    class User(db.Model):
    name = db.Column(db.String(40), primary_key=True)
    email = db.Column(db.String(100))
    @app.route('/user/')
    def show_user(name):
    user = User.query.filter_by(name=name).first_or_404()
    return render_template('user.html', user=user)

    View Slide