SlideShare a Scribd company logo
2
Most read
3
Most read
Flask, for people who like to have
       a little drink at night
                Areski Belaid
            <areski@gmail.com>
              21th March 2013

            slideshare.net/areski/
Flask Introduction

What is Flask?
Flask is a micro web development framework
for Python

What is MicroFramework?
Keep the core simple but extensible

“Micro” does not mean that your whole web
application has to fit into one Python file
Installation
Dependencies: Werkzeug and Jinja2

      $ sudo pip install virtualenv
      $ virtualenv venv
      $ . venv/bin/activate
      $ pip install Flask

If you want to work with databases you will need:

      $ pip install Flask-SQLAlchemy
QuickStart
A minimal Flask application looks something like this:
1.    from flask import Flask
2.    app = Flask(__name__)

3.    @app.route('/')
4.    def hello_world():
        return 'Hello World!'

5.    if __name__ == '__main__':
          app.debug = True
          app.run()

Save and run it with your Python interpreter:
      $ python hello.py
      * Running on http://127.0.0.1:5000/
This is the end...




   You can now write a Flask application!
URLs
The route() decorator is used to bind a function to a URL:
    @app.route('/')
    def index():
      return 'Index Page'

    @app.route('/hello')
    def hello():
      return 'Hello World'

We can add variable parts:
    @app.route('/user/<username>')
    def show_user_profile(username):
      # show the user profile for that user
      return 'User %s' % username

    @app.route('/post/<int:post_id>')
    def show_post(post_id):
      return 'Post %d' % post_id
HTTP Method
By default, a route only answers GET requests, but this can be changed by
providing the methods argument to the route() decorator:

    @app.route('/login', methods=['GET', 'POST'])
    def login():
      if request.method == 'POST':
          do_the_login()
      else:
          show_the_login_form()

We can ask Flask do the hard work and use decorator:
   @app.route ( ’/login ’ , methods =[ ’ GET ’ ])
   def show_the_login_form ():
   ...
   @app.route ( ’/login’ , methods =[ ’ POST ’ ])
   def do_the_login ():
   ...
Rendering templates
To render a template you can use the render_template() method:

         from flask import render_template

         @app.route('/hello/')
         @app.route('/hello/<name>')
         def hello(name=None):
           return render_template('hello.html', name=name)


Let's say you want to display a list of blog posts, you will connect to your DB and
push the “posts” list to your template engine:

         @app.route('/posts/')
         def show_post():
              cur = g.db.execute('SELECT title, text FROM post')
              posts = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
                   return render_template('show_post.html', posts=posts)
Rendering templates (next)
The show_posts.html template file would look like:

         <!doctype html>
         <title>Blog with Flask</title>
         <div>
         <h1>List posts</h1>
         <ul>
         {% for post in posts %}
               <li><h2>{{ post.title }}</h2>{{ post.text|safe }}
         {% else %}
               <li><em>Unbelievable, there is no post!</em>
         {% endfor %}
         </div>
More and more and more...
  ○   Access request data

  ○   Cookies

  ○   Session

  ○   File Upload

  ○   Cache

  ○   Class Base View

  ○   …



                Flask has incredible documentation...
Flask vs Django
                                  Flask               Django

     Template                     Jinja2                Own

     Signals                     Blinker                Own

     i18N                         Babel                 Own

     ORM                           Any                  Own

     Admin                     Flask-Admin           Builtin-Own




* Django is large and monolithic
     Difficult to change / steep learning curve

* Flask is Small and extensible
     Add complexity as necessary / learn as you go
Lots of extensions
http://flask.pocoo.org/extensions/


    ●   YamlConfig
    ●   WTForm
    ●   MongoDB flask
    ●   S3
    ●   Resful API
    ●   Admin
    ●   Bcrypt
    ●   Celery
    ●   DebugToolbar
Admin
https://pypi.python.org/pypi/Flask-Admin

Very simple example, how to use Flask/SQLalchemy and create an admin
https://github.com/MrJoes/Flask-Admin/tree/master/examples/sqla
Conclusion

- Flask is a strong and flexible web framework

- Still micro, but not in terms of features

- You can and should build Web applications with Flask
Hope you enjoyed it!
       Questions?

    slideshare.net/areski/

    github.com/areski/

    twitter.com/areskib




Contact email : areski@gmail.com

More Related Content

PPTX
Python/Flask Presentation
Parag Mujumdar
 
PPTX
Hibernate ppt
Aneega
 
PDF
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Edureka!
 
PDF
Quick flask an intro to flask
juzten
 
PPTX
Flask – Python
Max Claus Nunes
 
PDF
Web develop in flask
Jim Yeh
 
PPTX
Flask
Mamta Kumari
 
PPTX
Node.js Express
Eyal Vardi
 
Python/Flask Presentation
Parag Mujumdar
 
Hibernate ppt
Aneega
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Edureka!
 
Quick flask an intro to flask
juzten
 
Flask – Python
Max Claus Nunes
 
Web develop in flask
Jim Yeh
 
Node.js Express
Eyal Vardi
 

What's hot (20)

PDF
Flask Basics
Eueung Mulyana
 
PPTX
Spring Boot
Jiayun Zhou
 
PDF
Le Wagon - 2h Landing
Boris Paillard
 
PDF
Introduction to Redux
Ignacio Martín
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PPTX
React js
Oswald Campesato
 
PDF
Introduction to ReactJS
Hoang Long
 
PDF
HTTP Request and Response Structure
BhagyashreeGajera1
 
PDF
Le Wagon - UI components design
Boris Paillard
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PPT
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
PDF
Flask for cs students
Jennifer Rubinovitz
 
PDF
Le Wagon - Javascript for Beginners
Sébastien Saunier
 
PPTX
Django Architecture Introduction
Haiqi Chen
 
PPTX
An Introduction to the DOM
Mindy McAdams
 
PPTX
jQuery
Dileep Mishra
 
PPTX
Developing New Widgets for your Views in Owl
Odoo
 
PPT
PHP - Introduction to PHP Fundamentals
Vibrant Technologies & Computers
 
PDF
Railway Oriented Programming
Scott Wlaschin
 
Flask Basics
Eueung Mulyana
 
Spring Boot
Jiayun Zhou
 
Le Wagon - 2h Landing
Boris Paillard
 
Introduction to Redux
Ignacio Martín
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
Introduction to ReactJS
Hoang Long
 
HTTP Request and Response Structure
BhagyashreeGajera1
 
Le Wagon - UI components design
Boris Paillard
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PHP POWERPOINT SLIDES
Ismail Mukiibi
 
Flask for cs students
Jennifer Rubinovitz
 
Le Wagon - Javascript for Beginners
Sébastien Saunier
 
Django Architecture Introduction
Haiqi Chen
 
An Introduction to the DOM
Mindy McAdams
 
Developing New Widgets for your Views in Owl
Odoo
 
PHP - Introduction to PHP Fundamentals
Vibrant Technologies & Computers
 
Railway Oriented Programming
Scott Wlaschin
 
Ad

Viewers also liked (20)

PPT
Flask - Python microframework
André Mayer
 
PDF
Flask admin vs. DIY
dokenzy
 
PDF
Python web frameworks
NEWLUG
 
PPTX
Flask vs. Django
Rachel Sanders
 
PDF
Building Automated REST APIs with Python
Jeff Knupp
 
PDF
Developing RESTful Web APIs with Python, Flask and MongoDB
Nicola Iarocci
 
PDF
Lightweight web frameworks
Jonathan Holloway
 
PDF
Kyiv.py #17 Flask talk
Alexey Popravka
 
PDF
Flask - Backend com Python - Semcomp 18
Lar21
 
PDF
Nikola, a static blog & site generator python meetup 19 feb2014
Areski Belaid
 
PDF
Newfies dialer - autodialer : freeswitch weekly conference 13 march2013
Areski Belaid
 
PDF
Whitepaper newfies-dialer Autodialer
Areski Belaid
 
PPTX
Flask
Elita Lobo
 
PDF
Newfies dialer Brief Introduction
Areski Belaid
 
PDF
Newfies dialer Auto dialer Software
Areski Belaid
 
PDF
CDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDB
Areski Belaid
 
PPT
Learn flask in 90mins
Larry Cai
 
PDF
What The Flask? and how to use it with some Google APIs
Bruno Rocha
 
PDF
Django para portais de alta visibilidade. tdc 2013
Bruno Rocha
 
PDF
Build website in_django
swee meng ng
 
Flask - Python microframework
André Mayer
 
Flask admin vs. DIY
dokenzy
 
Python web frameworks
NEWLUG
 
Flask vs. Django
Rachel Sanders
 
Building Automated REST APIs with Python
Jeff Knupp
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Nicola Iarocci
 
Lightweight web frameworks
Jonathan Holloway
 
Kyiv.py #17 Flask talk
Alexey Popravka
 
Flask - Backend com Python - Semcomp 18
Lar21
 
Nikola, a static blog & site generator python meetup 19 feb2014
Areski Belaid
 
Newfies dialer - autodialer : freeswitch weekly conference 13 march2013
Areski Belaid
 
Whitepaper newfies-dialer Autodialer
Areski Belaid
 
Flask
Elita Lobo
 
Newfies dialer Brief Introduction
Areski Belaid
 
Newfies dialer Auto dialer Software
Areski Belaid
 
CDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDB
Areski Belaid
 
Learn flask in 90mins
Larry Cai
 
What The Flask? and how to use it with some Google APIs
Bruno Rocha
 
Django para portais de alta visibilidade. tdc 2013
Bruno Rocha
 
Build website in_django
swee meng ng
 
Ad

Similar to Flask Introduction - Python Meetup (20)

PDF
Flask intro - ROSEdu web workshops
Alex Eftimie
 
PDF
Introduction to Flask Micro Framework
Mohammad Reza Kamalifard
 
PPTX
Flask-Python
Triloki Gupta
 
PDF
Python Web Applications With Flask Handon Your Flask Skills2024 Jeffrey Leon ...
keyroreagan
 
KEY
LvivPy - Flask in details
Max Klymyshyn
 
PPTX
Flask Application ppt to understand the flask
vijoho5545
 
PPTX
Intro to flask
Mohamed Essam
 
PPTX
Intro to flask2
Mohamed Essam
 
PDF
Flask Web Development 1st Edition Miguel Grinberg
cjvsgfu2766
 
PDF
BUILDING MODERN PYTHON WEB FRAMEWORKS USING FLASK WITH NEIL GREY
CodeCore
 
PPTX
Flask and Introduction to web frameworks
dipendralfs
 
PDF
Python and Flask introduction for my classmates Презентация и введение в flask
Nikita Lozhnikov
 
PDF
Web Server and how we can design app in C#
caohansnnuedu
 
PDF
Flask patterns
it-people
 
PDF
CollegeDiveIn presentation
Karambir Singh Nain
 
PDF
Python master class part 1
Chathuranga Bandara
 
PDF
An Introduction to Tornado
Gavin Roy
 
PDF
django_introduction20141030
Kevin Wu
 
PPTX
Building a local web application with Flask
Hoffman Lab
 
Flask intro - ROSEdu web workshops
Alex Eftimie
 
Introduction to Flask Micro Framework
Mohammad Reza Kamalifard
 
Flask-Python
Triloki Gupta
 
Python Web Applications With Flask Handon Your Flask Skills2024 Jeffrey Leon ...
keyroreagan
 
LvivPy - Flask in details
Max Klymyshyn
 
Flask Application ppt to understand the flask
vijoho5545
 
Intro to flask
Mohamed Essam
 
Intro to flask2
Mohamed Essam
 
Flask Web Development 1st Edition Miguel Grinberg
cjvsgfu2766
 
BUILDING MODERN PYTHON WEB FRAMEWORKS USING FLASK WITH NEIL GREY
CodeCore
 
Flask and Introduction to web frameworks
dipendralfs
 
Python and Flask introduction for my classmates Презентация и введение в flask
Nikita Lozhnikov
 
Web Server and how we can design app in C#
caohansnnuedu
 
Flask patterns
it-people
 
CollegeDiveIn presentation
Karambir Singh Nain
 
Python master class part 1
Chathuranga Bandara
 
An Introduction to Tornado
Gavin Roy
 
django_introduction20141030
Kevin Wu
 
Building a local web application with Flask
Hoffman Lab
 

Recently uploaded (20)

PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 

Flask Introduction - Python Meetup

  • 1. Flask, for people who like to have a little drink at night Areski Belaid <[email protected]> 21th March 2013 slideshare.net/areski/
  • 2. Flask Introduction What is Flask? Flask is a micro web development framework for Python What is MicroFramework? Keep the core simple but extensible “Micro” does not mean that your whole web application has to fit into one Python file
  • 3. Installation Dependencies: Werkzeug and Jinja2 $ sudo pip install virtualenv $ virtualenv venv $ . venv/bin/activate $ pip install Flask If you want to work with databases you will need: $ pip install Flask-SQLAlchemy
  • 4. QuickStart A minimal Flask application looks something like this: 1. from flask import Flask 2. app = Flask(__name__) 3. @app.route('/') 4. def hello_world(): return 'Hello World!' 5. if __name__ == '__main__': app.debug = True app.run() Save and run it with your Python interpreter: $ python hello.py * Running on http://127.0.0.1:5000/
  • 5. This is the end... You can now write a Flask application!
  • 6. URLs The route() decorator is used to bind a function to a URL: @app.route('/') def index(): return 'Index Page' @app.route('/hello') def hello(): return 'Hello World' We can add variable parts: @app.route('/user/<username>') def show_user_profile(username): # show the user profile for that user return 'User %s' % username @app.route('/post/<int:post_id>') def show_post(post_id): return 'Post %d' % post_id
  • 7. HTTP Method By default, a route only answers GET requests, but this can be changed by providing the methods argument to the route() decorator: @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': do_the_login() else: show_the_login_form() We can ask Flask do the hard work and use decorator: @app.route ( ’/login ’ , methods =[ ’ GET ’ ]) def show_the_login_form (): ... @app.route ( ’/login’ , methods =[ ’ POST ’ ]) def do_the_login (): ...
  • 8. Rendering templates To render a template you can use the render_template() method: from flask import render_template @app.route('/hello/') @app.route('/hello/<name>') def hello(name=None): return render_template('hello.html', name=name) Let's say you want to display a list of blog posts, you will connect to your DB and push the “posts” list to your template engine: @app.route('/posts/') def show_post(): cur = g.db.execute('SELECT title, text FROM post') posts = [dict(title=row[0], text=row[1]) for row in cur.fetchall()] return render_template('show_post.html', posts=posts)
  • 9. Rendering templates (next) The show_posts.html template file would look like: <!doctype html> <title>Blog with Flask</title> <div> <h1>List posts</h1> <ul> {% for post in posts %} <li><h2>{{ post.title }}</h2>{{ post.text|safe }} {% else %} <li><em>Unbelievable, there is no post!</em> {% endfor %} </div>
  • 10. More and more and more... ○ Access request data ○ Cookies ○ Session ○ File Upload ○ Cache ○ Class Base View ○ … Flask has incredible documentation...
  • 11. Flask vs Django Flask Django Template Jinja2 Own Signals Blinker Own i18N Babel Own ORM Any Own Admin Flask-Admin Builtin-Own * Django is large and monolithic Difficult to change / steep learning curve * Flask is Small and extensible Add complexity as necessary / learn as you go
  • 12. Lots of extensions http://flask.pocoo.org/extensions/ ● YamlConfig ● WTForm ● MongoDB flask ● S3 ● Resful API ● Admin ● Bcrypt ● Celery ● DebugToolbar
  • 13. Admin https://pypi.python.org/pypi/Flask-Admin Very simple example, how to use Flask/SQLalchemy and create an admin https://github.com/MrJoes/Flask-Admin/tree/master/examples/sqla
  • 14. Conclusion - Flask is a strong and flexible web framework - Still micro, but not in terms of features - You can and should build Web applications with Flask
  • 15. Hope you enjoyed it! Questions? slideshare.net/areski/ github.com/areski/ twitter.com/areskib Contact email : [email protected]