Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to Flask
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Sébastien Fievet
February 23, 2017
Technology
63
0
Share
Introduction to Flask
Workshop given for a Swisscom Digital Lab meetup at EPFL
Sébastien Fievet
February 23, 2017
More Decks by Sébastien Fievet
See All by Sébastien Fievet
Django Round-Up – Meetup Django CH #28
zyegfryed
0
61
Django Round-Up – Meetup Django CH #25
zyegfryed
0
56
Django Round-Up – Meetup Django CH #23
zyegfryed
0
51
Django Round-Up – Meetup Django CH #21
zyegfryed
0
68
Django Round-Up – Meetup Django CH #20
zyegfryed
0
72
Django Round-Up – Meetup Django CH #19
zyegfryed
2
87
[Django] URL prefix with runserver
zyegfryed
0
1.5k
[Django] Generating PDF with PDFForm
zyegfryed
0
91
[Django] RESTful API
zyegfryed
1
220
Other Decks in Technology
See All in Technology
Amazon S3 Filesについて
yama3133
2
210
Standards et agents IA : un tour d’horizon de MCP, A2A, ADK et plus encore
glaforge
0
170
AIが書いたコードを信じられない問題 〜レビュー負荷を下げるために変えたこと〜 / The AI Code Trust Gap: Reducing the Review Burden
bitkey
PRO
7
1.3k
20260428_Product Management Summit_Loglass_JoeHirose
loglassjoe
0
1.5k
最近の技術系の話題で気になったもの色々(IoT系以外も) / IoTLT 花見予定会(たぶんBBQ) @都立潮風公園バーベキュー広場
you
PRO
1
240
[OpsJAWS 40]リリースしたら終わり、じゃなかった。セキュリティ空白期間をAWS Security Agentで埋める
sh_fk2
3
240
Rapid Start: Faster Internet Connections, with Ruby's Help
kazuho
2
600
QGISプラグイン CMChangeDetector
naokimuroki
1
410
AI駆動1on1〜AIに自分を育ててもらう〜
yoshiakiyasuda
0
120
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
16k
Pure Intonation on Browser: Building a Sequencer with Ruby
nagachika
0
120
レビューしきれない?それは「全て人力でのレビュー」だからではないでしょうか
amixedcolor
0
330
Featured
See All Featured
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.2k
BBQ
matthewcrist
89
10k
Designing for Timeless Needs
cassininazir
0
200
Utilizing Notion as your number one productivity tool
mfonobong
4
290
How to train your dragon (web standard)
notwaldorf
97
6.6k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.4k
Tell your own story through comics
letsgokoyo
1
900
Docker and Python
trallard
47
3.8k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
180
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.4k
Everyday Curiosity
cassininazir
0
200
Transcript
Introduction to Flask Sébastien Fievet Swisscom Digital Lab at EPFL
Prerequisites
$ python --version Python 2.7.12 https://www.python.org/downloads
$ pip --version pip 9.0.1 https://pip.pypa.io/en/stable/installing/
$ pip install virtualenv $ virtualenv --version 13.1.2 https://virtualenv.pypa.io/en/stable/installation/
$ git clone \ https://github.com/zyegfryed/intro_to_flask.git $ cd intro_to_flask $ virtualenv
.venv $ source .venv/bin/activate
Hello Flask!
–Deny “What… Why Flask?”
–http://flask.pocoo.org/ “Flask is a microframework for Python based on Werkzeug,
Jinja 2 and good intentions.”
Diving In https://github.com/zyegfryed/intro_to_flask/tree/01_intro
$ pip install Flask http://flask.pocoo.org/docs/0.12/installation/
# app.y from flask import Flask app = Flask(__name__) @app.route('/')
def hello_world(): return 'Hello, World!'
$ git checkout 01_intro $ export FLASK_APP=app.py $ flask run
$ curl http://127.0.0.1:5000/ Hello, World!
Chatbot
Diving In https://github.com/zyegfryed/intro_to_flask/tree/02_chatbot
$ pip install fbmessenger https://github.com/rehabstudio/fbmessenger
# app.py @app.route('/webhook', methods=['GET', 'POST']) def webhook(): if request.method ==
'GET': if request.args.get('hub.verify_token') == \ os.environ.get('FB_VERIFY_TOKEN'): return request.args.get('hub.challenge') raise ValueError('FB_VERIFY_TOKEN does not match.')
# app.py @app.route('/webhook', methods=['GET', 'POST']) def webhook(): […] elif request.method
== 'POST': messenger.handle(request.get_json(force=True)) return ''
# app.py from fbmessenger import BaseMessenger from fbmessenger import elements
class Messenger(BaseMessenger): def message(self, message): element = elements.Text('Received: {0}’.format( message['message']['text'])) self.send(element.to_dict()) […]
# app.py messenger = \ Messenger(os.environ.get(‘FB_PAGE_TOKEN’))
$ ngrok --version ngrok version 2.1.18 https://ngrok.com/download
Messenger https://developers.facebook.com/docs/messenger- platform/guides/setup
$ git checkout 02_chatbot $ export FB_VERIFY_TOKEN=… $ export FB_PAGE_TOKEN=…
$ flask run - - - - - - - - - — - - - - - - - - - - - - $ ngrok http 5000
Async
Diving In https://github.com/zyegfryed/intro_to_flask/tree/03_async
# app.py from redis import StrictRedis redis = StrictRedis() @job('default',
connection=redis) def messenger_handle(message): messenger.handle(message) @app.route('/webhook', methods=['GET', 'POST']) def webhook(): […] elif request.method == 'POST': messenger_handle.delay(request.get_json(force=True)) return ''
$ redis-server --version Redis server v=3.2.8 https://redis.io/download
$ pip install rq http://python-rq.org/
$ git checkout 03_async $ export FB_PAGE_TOKEN=… $ rqworker default
- - - - - - - - - — - - - - - - - - - - - - $ redis-server
Links • https://github.com/zyegfryed/intro_to_flask • https://www.python.org/downloads/ • https://pip.pypa.io/en/stable/installing/ • http://flask.pocoo.org/ •
http://flask.pocoo.org/docs/0.12/quickstart/ • https://developers.facebook.com/docs/messenger-platform/guides/setup • https://github.com/rehabstudio/fbmessenger • https://redis.io/download • http://python-rq.org/ • https://github.com/rehabstudio/fbmessenger