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

E3550767c858c787c35c280047ff789c?s=128

Nicola Iarocci

April 16, 2016
Tweet

Transcript

  1. None
  2. HELLO.PY from flask import Flask app = Flask(__name__) @app.route("/") def

    hello(): return "hello world" if __name__ == "__main__": app.run()
  3. ABOUT

  4. BSD LICENSED

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

    Jinja2, Werkzeug, Click
  6. USED BY THE PROS Pinterest, Twilio, Linked-in, President Obama Campaign,

    Reddit, Uber, etc.
  7. DEAD SIMPLE hello word in 7 lines of code in

    a single file
  8. EASY TO UNDERSTAND just write your ordinary Python code no

    pseudo-language to undestand
  9. MANAGEABLE LEARNING CURVE learn as you go, don’t feel overwhelmed

  10. NO IMPOSED STRUCTURE no need to understand things are the

    wey they are: they aren’t
  11. SMALL API 800 LOC, 1500 LOC Tests

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

    documentation
  13. FEATURES

  14. WERKZEUG It's hard to find anything that's simpler, more self-contained,

    or purer-WSGI than Werkzeug — Alex Martelli
  15. JINJA2 modern, fast, designer-friendly templating language inspired by Django templates

    (!)
  16. DEVELOPMENT SERVER app.run()

  17. DEBUGGER app.run(debug=True)

  18. UNIT TESTING flask.app.test_client()

  19. RESTFUL REQUEST DISPATCHING modern web applications have beautiful URLs

  20. SIGNED SESSION COOKIES session allows you to securely store information

    specific to a user from one request to the next
  21. 100% WSGI COMPLIANT

  22. VERY STABLE (despite being in beta)

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

  24. HELLO WORLD

  25. HELLO.PY from flask import Flask app = Flask(__name__) @app.route("/") def

    hello(): return render_template('hello.html') if __name__ == "__main__": app.run()
  26. HELLO.HTML {% extends 'layout.html' %} {% block title %}Greetings{% endblock

    %} {% block body %} <h1>Hello {{ name }}!</h1> {% endblock
  27. LAYOUT.HTML <!doctype html> <head> <title>{% block title %}{% endblock %}</title>

    </head> <body> {% block body %}{% endblock %} </body>
  28. INSTALL AND RUN $ pip install Flask $ python hello.py

    * Running on http://localhost:5000/
  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/<name>') def show_user(name): user = User.query.filter_by(name=name).first_or_404() return render_template('user.html', user=user)