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

Introduction to Flask

Introduction to Flask

Workshop given for a Swisscom Digital Lab meetup at EPFL

Sébastien Fievet

February 23, 2017
Tweet

More Decks by Sébastien Fievet

Other Decks in Technology

Transcript

  1. $ git checkout 01_intro $ export FLASK_APP=app.py $ flask run

    $ curl http://127.0.0.1:5000/ Hello, World!
  2. # 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.')
  3. # app.py @app.route('/webhook', methods=['GET', 'POST']) def webhook(): […] elif request.method

    == 'POST': messenger.handle(request.get_json(force=True)) return ''
  4. # 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()) […]
  5. $ git checkout 02_chatbot $ export FB_VERIFY_TOKEN=… $ export FB_PAGE_TOKEN=…

    $ flask run - - - - - - - - - — - - - - - - - - - - - - $ ngrok http 5000
  6. # 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 ''
  7. $ git checkout 03_async $ export FB_PAGE_TOKEN=… $ rqworker default

    - - - - - - - - - — - - - - - - - - - - - - $ redis-server
  8. 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