SlideShare a Scribd company logo
Flask? No thanks!
by Alexey Popravka
About me
● 6+ years of Python
● Python developer / Tech Lead at Prom.ua
● Contributor of github.com/aio-libs
● github.com/popravich
What this talk is about?
● What is Flask
● What Flask is not (what is wrong)
● Alternatives
● Questions
What is Flask
“Flask is a microframework for Python based on
Werkzeug, Jinja 2 and good intentions.”
What is Flask
“Flask is a microframework for Python based on
Werkzeug, Jinja 2 and good intentions.”
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
What is Flask
● Jinja2 templates out of the box
from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
<h1>Hello {{ name }}!</h1>
{% else %}
<h1>Hello World!</h1>
{% endif %}
What is Flask
● Jinja2 templates
● Sessions
from flask import Flask, session, escape
app = Flask(__name__)
@app.route('/')
def index():
if 'username' in session:
return 'Logged in as %s' % escape(session['username'])
return 'You are not logged in'
What is Flask
● Jinja2 templates
● Sessions
● Static files serving
url_for('static', filename='style.css')
What is Flask
● Jinja2 templates
● Sessions
● Static files serving
● Debugger
app.run(debug=True)
What is Flask
● Jinja2 templates
● Sessions
● Static files serving
● Debugger
● Many extensions
$ pip search flask | wc -l
1020
What is wrong with: routes
● Preferred way – using decorator
# app/view.py
@app.route('/')
def index():
return “Hello world”
bp = Blueprint()
@bp.route('/')
def hello():
return “Hello world”
What is wrong with: routes
● Preferred way – using decorator
# proj/view.py
from proj import app
@app.route('/')
def index():
return “Hello world”
Must have application instance ->
# proj/__init__.py
from flask import Flask
app = Flask('proj')
We begin with already runnable app.
But, wait!
* Did we have time to configure it?
* Did we have time to configure something else (DB, etc)?
What is wrong with: routes
● Preferred way – using decorator?
# proj/view.py
from proj import app
if app.debug:
@app.route('/docs/')
def show_some_docs():
pass
How to control? ->
What is wrong with: composite apps
Say you need several apps not messing their
code:
# proj/app/__init__.py
from flask import Flask
app = Flask('proj.app')
@app.route('/')
def index():
return render_template(
'index.html')
# proj/admin/__init__.py
from flask import Flask
app = Flask('proj.admin')
@app.route('/')
def index():
return render_template(
'admin_index.html')
What is wrong with: composite apps
How to reuse apps' URLs?
# proj/admin/__init__.py
from flask import Flask
from flask import url_for
app = Flask('proj.admin')
@app.route('/')
def index():
url_for('app.index') #???
return render_template(
'admin_index.html')
??? ->
* Blueprints can help, but application must be only one
What is wrong with: jinja
Abused!
from celery import Celery
from flask import render_template
celery_app = Celery()
@celery_app.task
def send_periodic_email():
to = get_address()
html_body = render_template('mails/annoy.html')
send_mail(to, html_body)
Web Worker == Celery Worker
What is wrong with: global vars
● Magical request object
thread-local proxy object
from celery import Celery
from flask import render_template, session
celery_app = Celery()
@celery_app.task
def send_periodic_email():
to = get_address()
user_name = session['user_name']
html_body = render_template(
'mails/annoy.html',
{user_name: user_name})
send_mail(to, html_body)
Need request instance ->
What is wrong with: global vars
● Magical request object
from flask import Flask, request
from proj.db import session
from proj.redis import redis
# ... import other global instance
app = Flask()
@app.route('/')
def index():
uid = redis.get(request.cookies.get('cid'))
user_row = session.execute(“select * from ...”)
# ...
Alternatives
● Enough using WSGI, start using asyncio/aiohttp
Alternatives
● Can't cope asyncio? At least try using Flask
less self-destructive:
– Get rid of app.route decorator
def index():
return “Hello world”
# ... more views
def setup_routes(app):
app.add_url_rule('/', 'index', index)
app.add_url_rule(...)
from flask import Flask
from . import views
def main():
app = Flask()
views.setup_routes(app)
# more configuration
Alternatives
● Can't cope asyncio? At least try using Flask
less self-destructive:
– Use any kind of dependencies instead of global
(thread-local proxy) variables:
from flask import Flask, current_app
class MyApp:
def __init__(self, *args, redis, db_conn **kw):
super().__init__(*args, **kw)
self.redis = redis
self.db_conn = db_conn
def get_user_view():
redis = current_app.redis
....
Alternatives
● Still want to use Flask — use Werkzeug!
The End
Questions?
What Flask is not — it is not “micro framework” it is a “Werkzeug wrapper”

More Related Content

PPTX
Flask – Python
PDF
Flask - Backend com Python - Semcomp 18
PDF
Flask patterns
PDF
Filling the flask
PDF
Flask Basics
KEY
LvivPy - Flask in details
PDF
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
PDF
Rest API using Flask & SqlAlchemy
Flask – Python
Flask - Backend com Python - Semcomp 18
Flask patterns
Filling the flask
Flask Basics
LvivPy - Flask in details
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Rest API using Flask & SqlAlchemy

What's hot (20)

PDF
Flask Introduction - Python Meetup
PDF
Flask RESTful Flask HTTPAuth
PDF
Flask SQLAlchemy
PDF
Laravel Design Patterns
PDF
Bootstrat REST APIs with Laravel 5
PDF
Getting Started-with-Laravel
KEY
Php Unit With Zend Framework Zendcon09
PPT
Learn flask in 90mins
PDF
#30.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
Unit testing after Zend Framework 1.8
PDF
Laravel 5 In Depth
PDF
Laravel 101
KEY
Phpne august-2012-symfony-components-friends
PPTX
Flask restfulservices
PPTX
REST APIs in Laravel 101
PDF
What happens in laravel 4 bootstraping
PDF
Phinx talk
PPTX
Laravel 5
PDF
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PPT
Dance for the puppet master: G6 Tech Talk
Flask Introduction - Python Meetup
Flask RESTful Flask HTTPAuth
Flask SQLAlchemy
Laravel Design Patterns
Bootstrat REST APIs with Laravel 5
Getting Started-with-Laravel
Php Unit With Zend Framework Zendcon09
Learn flask in 90mins
#30.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
Unit testing after Zend Framework 1.8
Laravel 5 In Depth
Laravel 101
Phpne august-2012-symfony-components-friends
Flask restfulservices
REST APIs in Laravel 101
What happens in laravel 4 bootstraping
Phinx talk
Laravel 5
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
Dance for the puppet master: G6 Tech Talk
Ad

Similar to Kyiv.py #17 Flask talk (20)

PPTX
Developing Flask Extensions
KEY
Plack - LPW 2009
KEY
Psgi Plack Sfpm
KEY
Psgi Plack Sfpm
PPTX
Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...
PPTX
React django
PDF
Python para web - Utilizando micro-framework Flask - PUG-MA
PDF
What The Flask? and how to use it with some Google APIs
PDF
Lean Php Presentation
KEY
Intro to PSGI and Plack
ODP
Incredible Machine with Pipelines and Generators
PDF
Don't worry be API with Slim framework and Joomla
PDF
Collaboration hack with slackbot - PyCon HK 2018 - 2018.11.24
PDF
Creating Sentiment Line Chart with Watson
PDF
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
PDF
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
KEY
Plack perl superglue for web frameworks and servers
PDF
ParisJS #10 : RequireJS
PDF
You've done the Django Tutorial, what next?
PDF
Ecto and Phoenix: Doing web with Elixir - Yurii Bodarev
Developing Flask Extensions
Plack - LPW 2009
Psgi Plack Sfpm
Psgi Plack Sfpm
Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...
React django
Python para web - Utilizando micro-framework Flask - PUG-MA
What The Flask? and how to use it with some Google APIs
Lean Php Presentation
Intro to PSGI and Plack
Incredible Machine with Pipelines and Generators
Don't worry be API with Slim framework and Joomla
Collaboration hack with slackbot - PyCon HK 2018 - 2018.11.24
Creating Sentiment Line Chart with Watson
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
Plack perl superglue for web frameworks and servers
ParisJS #10 : RequireJS
You've done the Django Tutorial, what next?
Ecto and Phoenix: Doing web with Elixir - Yurii Bodarev
Ad

Recently uploaded (20)

PDF
Containerization lab dddddddddddddddmanual.pdf
PDF
The Ikigai Template _ Recalibrate How You Spend Your Time.pdf
PDF
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
PDF
simpleintnettestmetiaerl for the simple testint
PPTX
Reading as a good Form of Recreation
PPT
12 Things That Make People Trust a Website Instantly
PPT
Ethics in Information System - Management Information System
PDF
Exploring VPS Hosting Trends for SMBs in 2025
PDF
Understand the Gitlab_presentation_task.pdf
PPTX
Top Website Bugs That Hurt User Experience – And How Expert Web Design Fixes
PDF
Lean-Manufacturing-Tools-Techniques-and-How-To-Use-Them.pdf
PPTX
artificialintelligenceai1-copy-210604123353.pptx
PPTX
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
PDF
BIOCHEM CH2 OVERVIEW OF MICROBIOLOGY.pdf
PDF
Top 8 Trusted Sources to Buy Verified Cash App Accounts.pdf
PDF
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
DOCX
Powerful Ways AIRCONNECT INFOSYSTEMS Pvt Ltd Enhances IT Infrastructure in In...
PPTX
IPCNA VIRTUAL CLASSES INTERMEDIATE 6 PROJECT.pptx
PPTX
t_and_OpenAI_Combined_two_pressentations
PPT
250152213-Excitation-SystemWERRT (1).ppt
Containerization lab dddddddddddddddmanual.pdf
The Ikigai Template _ Recalibrate How You Spend Your Time.pdf
mera desh ae watn.(a source of motivation and patriotism to the youth of the ...
simpleintnettestmetiaerl for the simple testint
Reading as a good Form of Recreation
12 Things That Make People Trust a Website Instantly
Ethics in Information System - Management Information System
Exploring VPS Hosting Trends for SMBs in 2025
Understand the Gitlab_presentation_task.pdf
Top Website Bugs That Hurt User Experience – And How Expert Web Design Fixes
Lean-Manufacturing-Tools-Techniques-and-How-To-Use-Them.pdf
artificialintelligenceai1-copy-210604123353.pptx
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
BIOCHEM CH2 OVERVIEW OF MICROBIOLOGY.pdf
Top 8 Trusted Sources to Buy Verified Cash App Accounts.pdf
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
Powerful Ways AIRCONNECT INFOSYSTEMS Pvt Ltd Enhances IT Infrastructure in In...
IPCNA VIRTUAL CLASSES INTERMEDIATE 6 PROJECT.pptx
t_and_OpenAI_Combined_two_pressentations
250152213-Excitation-SystemWERRT (1).ppt

Kyiv.py #17 Flask talk

  • 1. Flask? No thanks! by Alexey Popravka
  • 2. About me ● 6+ years of Python ● Python developer / Tech Lead at Prom.ua ● Contributor of github.com/aio-libs ● github.com/popravich
  • 3. What this talk is about? ● What is Flask ● What Flask is not (what is wrong) ● Alternatives ● Questions
  • 4. What is Flask “Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.”
  • 5. What is Flask “Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions.” from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
  • 6. What is Flask ● Jinja2 templates out of the box from flask import render_template @app.route('/hello/') @app.route('/hello/<name>') def hello(name=None): return render_template('hello.html', name=name) <!doctype html> <title>Hello from Flask</title> {% if name %} <h1>Hello {{ name }}!</h1> {% else %} <h1>Hello World!</h1> {% endif %}
  • 7. What is Flask ● Jinja2 templates ● Sessions from flask import Flask, session, escape app = Flask(__name__) @app.route('/') def index(): if 'username' in session: return 'Logged in as %s' % escape(session['username']) return 'You are not logged in'
  • 8. What is Flask ● Jinja2 templates ● Sessions ● Static files serving url_for('static', filename='style.css')
  • 9. What is Flask ● Jinja2 templates ● Sessions ● Static files serving ● Debugger app.run(debug=True)
  • 10. What is Flask ● Jinja2 templates ● Sessions ● Static files serving ● Debugger ● Many extensions $ pip search flask | wc -l 1020
  • 11. What is wrong with: routes ● Preferred way – using decorator # app/view.py @app.route('/') def index(): return “Hello world” bp = Blueprint() @bp.route('/') def hello(): return “Hello world”
  • 12. What is wrong with: routes ● Preferred way – using decorator # proj/view.py from proj import app @app.route('/') def index(): return “Hello world” Must have application instance -> # proj/__init__.py from flask import Flask app = Flask('proj') We begin with already runnable app. But, wait! * Did we have time to configure it? * Did we have time to configure something else (DB, etc)?
  • 13. What is wrong with: routes ● Preferred way – using decorator? # proj/view.py from proj import app if app.debug: @app.route('/docs/') def show_some_docs(): pass How to control? ->
  • 14. What is wrong with: composite apps Say you need several apps not messing their code: # proj/app/__init__.py from flask import Flask app = Flask('proj.app') @app.route('/') def index(): return render_template( 'index.html') # proj/admin/__init__.py from flask import Flask app = Flask('proj.admin') @app.route('/') def index(): return render_template( 'admin_index.html')
  • 15. What is wrong with: composite apps How to reuse apps' URLs? # proj/admin/__init__.py from flask import Flask from flask import url_for app = Flask('proj.admin') @app.route('/') def index(): url_for('app.index') #??? return render_template( 'admin_index.html') ??? -> * Blueprints can help, but application must be only one
  • 16. What is wrong with: jinja Abused! from celery import Celery from flask import render_template celery_app = Celery() @celery_app.task def send_periodic_email(): to = get_address() html_body = render_template('mails/annoy.html') send_mail(to, html_body) Web Worker == Celery Worker
  • 17. What is wrong with: global vars ● Magical request object thread-local proxy object from celery import Celery from flask import render_template, session celery_app = Celery() @celery_app.task def send_periodic_email(): to = get_address() user_name = session['user_name'] html_body = render_template( 'mails/annoy.html', {user_name: user_name}) send_mail(to, html_body) Need request instance ->
  • 18. What is wrong with: global vars ● Magical request object from flask import Flask, request from proj.db import session from proj.redis import redis # ... import other global instance app = Flask() @app.route('/') def index(): uid = redis.get(request.cookies.get('cid')) user_row = session.execute(“select * from ...”) # ...
  • 19. Alternatives ● Enough using WSGI, start using asyncio/aiohttp
  • 20. Alternatives ● Can't cope asyncio? At least try using Flask less self-destructive: – Get rid of app.route decorator def index(): return “Hello world” # ... more views def setup_routes(app): app.add_url_rule('/', 'index', index) app.add_url_rule(...) from flask import Flask from . import views def main(): app = Flask() views.setup_routes(app) # more configuration
  • 21. Alternatives ● Can't cope asyncio? At least try using Flask less self-destructive: – Use any kind of dependencies instead of global (thread-local proxy) variables: from flask import Flask, current_app class MyApp: def __init__(self, *args, redis, db_conn **kw): super().__init__(*args, **kw) self.redis = redis self.db_conn = db_conn def get_user_view(): redis = current_app.redis ....
  • 22. Alternatives ● Still want to use Flask — use Werkzeug!
  • 23. The End Questions? What Flask is not — it is not “micro framework” it is a “Werkzeug wrapper”