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

Flask: for the bold and true

Flask: for the bold and true

Learn the differences between Django and Flask, learn about Flask and its numerous extensions, and get inspired to develop a Flask application.

Avatar for Daniel Imhoff

Daniel Imhoff

May 12, 2016

Other Decks in Programming

Transcript

  1. Why Flask? • Microframework • Lightweight (~800 lines of code)

    • Simple & elegant • Flexible • Extensible ( http://flask.pocoo.org/extensions ) • Doesn’t get in your way
  2. • Django is great! ◦ … if you want to

    build an MV* web application with SQL backend ◦ … for monoliths • Django doesn’t make much sense ◦ … if you want to choose your own ORM, Form builder, serializer, etc. ◦ … for single-page apps (AngularJS, React, etc.) ◦ … for SOA & RESTful APIs ◦ … if you like to DIY Why not Django?
  3. vs.

  4. WSGI (Web Server Gateway Interface) • PEP 3333 • Python

    web interface • Specifies how a Python app should talk to the web
  5. Jinja2 • Python templating language • Fast (with optional ahead-of-time

    compilation) • Template inheritance • Blocks • Required by Flask
  6. from flask import Flask app = Flask(__name__) @app.route(‘/’) def hello_world():

    return “Hello World!” if __name__ == ‘__main__’: app.run()
  7. @app.route(‘/api/users’, methods=[‘POST’]) def api_create_user(): user = create_user(request.form) return jsonify(user), 201

    @app.route(‘/api/users/<int:user_id>’) def api_get_user(user_id): user = get_user(user_id) return jsonify(user)
  8. <!doctype html> <head> <title> {% block title %}{% endblock %}

    </title> <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon" /> </head> <body> {% block content %}{% endblock %} </body>
  9. {% extends “base.html” %} {% block title %}Welcome Page{% endblock

    %} {% block content %} <h1>Welcome</h1> <p>To my website</p> {% endblock %}
  10. Flask-Marshmallow • Backed by marshmallow • Serialization/deserialization using schemas defined

    in Python • Great for RESTful web services • Model schemas with marshmallow-sqlalchemy
  11. webargs • HTTP request argument parsing ◦ headers ◦ query

    parameters ◦ form-data ◦ form-urlencoded ◦ JSON • Requires marshmallow
  12. Additional resources • Flask Web Development by Miguel Grinberg ◦

    github.com/miguelgrinberg/flasky • #pocoo on irc.freenode.net • github.com/humiaozuzu/awesome-flask