SlideShare a Scribd company logo
1 of 15
Download to read offline
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

What's hot

Web develop in flask
Web develop in flaskWeb develop in flask
Web develop in flaskJim Yeh
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJames Casey
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js ExpressEyal Vardi
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsSolution4Future
 
Flask With Server-Sent Event
Flask With Server-Sent EventFlask With Server-Sent Event
Flask With Server-Sent EventTencent
 
Django - Python MVC Framework
Django - Python MVC FrameworkDjango - Python MVC Framework
Django - Python MVC FrameworkBala Kumar
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJSSandi Barr
 
PHP と SAPI と ZendEngine3 と
PHP と SAPI と ZendEngine3 とPHP と SAPI と ZendEngine3 と
PHP と SAPI と ZendEngine3 とdo_aki
 
The World of PHP PSR Standards
The World of PHP PSR StandardsThe World of PHP PSR Standards
The World of PHP PSR StandardsRadu Murzea
 
Deploying PHP applications with Phing
Deploying PHP applications with PhingDeploying PHP applications with Phing
Deploying PHP applications with PhingMichiel Rook
 
Django for Beginners
Django for BeginnersDjango for Beginners
Django for BeginnersJason Davies
 
Building a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootBuilding a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootOmri Spector
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django IntroductionGanga Ram
 
Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic LinkingWang Hsiangkai
 
Django로 쇼핑몰 만들자
Django로 쇼핑몰 만들자Django로 쇼핑몰 만들자
Django로 쇼핑몰 만들자Kyoung Up Jung
 

What's hot (20)

Web develop in flask
Web develop in flaskWeb develop in flask
Web develop in flask
 
Php tutorial
Php  tutorialPhp  tutorial
Php tutorial
 
Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Node.js Express
Node.js  ExpressNode.js  Express
Node.js Express
 
Spring boot
Spring bootSpring boot
Spring boot
 
Python RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutionsPython RESTful webservices with Python: Flask and Django solutions
Python RESTful webservices with Python: Flask and Django solutions
 
Flask With Server-Sent Event
Flask With Server-Sent EventFlask With Server-Sent Event
Flask With Server-Sent Event
 
Django - Python MVC Framework
Django - Python MVC FrameworkDjango - Python MVC Framework
Django - Python MVC Framework
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
PHP と SAPI と ZendEngine3 と
PHP と SAPI と ZendEngine3 とPHP と SAPI と ZendEngine3 と
PHP と SAPI と ZendEngine3 と
 
The World of PHP PSR Standards
The World of PHP PSR StandardsThe World of PHP PSR Standards
The World of PHP PSR Standards
 
Web api
Web apiWeb api
Web api
 
Deploying PHP applications with Phing
Deploying PHP applications with PhingDeploying PHP applications with Phing
Deploying PHP applications with Phing
 
Laravel Eloquent ORM
Laravel Eloquent ORMLaravel Eloquent ORM
Laravel Eloquent ORM
 
Django for Beginners
Django for BeginnersDjango for Beginners
Django for Beginners
 
Building a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootBuilding a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring Boot
 
A Basic Django Introduction
A Basic Django IntroductionA Basic Django Introduction
A Basic Django Introduction
 
An Introduction To REST API
An Introduction To REST APIAn Introduction To REST API
An Introduction To REST API
 
Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic Linking
 
Django로 쇼핑몰 만들자
Django로 쇼핑몰 만들자Django로 쇼핑몰 만들자
Django로 쇼핑몰 만들자
 

Viewers also liked

Flask - Python microframework
Flask - Python microframeworkFlask - Python microframework
Flask - Python microframeworkAndré Mayer
 
Flask admin vs. DIY
Flask admin vs. DIYFlask admin vs. DIY
Flask admin vs. DIYdokenzy
 
Python web frameworks
Python web frameworksPython web frameworks
Python web frameworksNEWLUG
 
Building Automated REST APIs with Python
Building Automated REST APIs with PythonBuilding Automated REST APIs with Python
Building Automated REST APIs with PythonJeff Knupp
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBDeveloping RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBNicola Iarocci
 
Flask - Backend com Python - Semcomp 18
Flask - Backend com Python - Semcomp 18Flask - Backend com Python - Semcomp 18
Flask - Backend com Python - Semcomp 18Lar21
 
Nikola, a static blog & site generator python meetup 19 feb2014
Nikola, a static blog & site generator   python meetup 19 feb2014Nikola, a static blog & site generator   python meetup 19 feb2014
Nikola, a static blog & site generator python meetup 19 feb2014Areski Belaid
 
Newfies dialer - autodialer : freeswitch weekly conference 13 march2013
Newfies dialer - autodialer : freeswitch weekly conference 13 march2013Newfies dialer - autodialer : freeswitch weekly conference 13 march2013
Newfies dialer - autodialer : freeswitch weekly conference 13 march2013Areski Belaid
 
Whitepaper newfies-dialer Autodialer
Whitepaper newfies-dialer AutodialerWhitepaper newfies-dialer Autodialer
Whitepaper newfies-dialer AutodialerAreski Belaid
 
Newfies dialer Brief Introduction
Newfies dialer Brief IntroductionNewfies dialer Brief Introduction
Newfies dialer Brief IntroductionAreski Belaid
 
Newfies dialer Auto dialer Software
Newfies dialer Auto dialer SoftwareNewfies dialer Auto dialer Software
Newfies dialer Auto dialer SoftwareAreski Belaid
 
CDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDB
CDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDBCDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDB
CDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDBAreski Belaid
 
What The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIsWhat The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIsBruno Rocha
 
Django para portais de alta visibilidade. tdc 2013
Django para portais de alta visibilidade.   tdc 2013Django para portais de alta visibilidade.   tdc 2013
Django para portais de alta visibilidade. tdc 2013Bruno Rocha
 
Build website in_django
Build website in_django Build website in_django
Build website in_django swee meng ng
 
Writing your first web app using Python and Flask
Writing your first web app using Python and FlaskWriting your first web app using Python and Flask
Writing your first web app using Python and FlaskDanielle Madeley
 

Viewers also liked (20)

Flask - Python microframework
Flask - Python microframeworkFlask - Python microframework
Flask - Python microframework
 
Flask admin vs. DIY
Flask admin vs. DIYFlask admin vs. DIY
Flask admin vs. DIY
 
Python web frameworks
Python web frameworksPython web frameworks
Python web frameworks
 
Flask vs. Django
Flask vs. DjangoFlask vs. Django
Flask vs. Django
 
Building Automated REST APIs with Python
Building Automated REST APIs with PythonBuilding Automated REST APIs with Python
Building Automated REST APIs with Python
 
Developing RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDBDeveloping RESTful Web APIs with Python, Flask and MongoDB
Developing RESTful Web APIs with Python, Flask and MongoDB
 
Lightweight web frameworks
Lightweight web frameworksLightweight web frameworks
Lightweight web frameworks
 
Kyiv.py #17 Flask talk
Kyiv.py #17 Flask talkKyiv.py #17 Flask talk
Kyiv.py #17 Flask talk
 
Flask - Backend com Python - Semcomp 18
Flask - Backend com Python - Semcomp 18Flask - Backend com Python - Semcomp 18
Flask - Backend com Python - Semcomp 18
 
Nikola, a static blog & site generator python meetup 19 feb2014
Nikola, a static blog & site generator   python meetup 19 feb2014Nikola, a static blog & site generator   python meetup 19 feb2014
Nikola, a static blog & site generator python meetup 19 feb2014
 
Newfies dialer - autodialer : freeswitch weekly conference 13 march2013
Newfies dialer - autodialer : freeswitch weekly conference 13 march2013Newfies dialer - autodialer : freeswitch weekly conference 13 march2013
Newfies dialer - autodialer : freeswitch weekly conference 13 march2013
 
Whitepaper newfies-dialer Autodialer
Whitepaper newfies-dialer AutodialerWhitepaper newfies-dialer Autodialer
Whitepaper newfies-dialer Autodialer
 
Flask
FlaskFlask
Flask
 
Newfies dialer Brief Introduction
Newfies dialer Brief IntroductionNewfies dialer Brief Introduction
Newfies dialer Brief Introduction
 
Newfies dialer Auto dialer Software
Newfies dialer Auto dialer SoftwareNewfies dialer Auto dialer Software
Newfies dialer Auto dialer Software
 
CDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDB
CDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDBCDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDB
CDR-Stats : VoIP Analytics Solution for Asterisk and FreeSWITCH with MongoDB
 
What The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIsWhat The Flask? and how to use it with some Google APIs
What The Flask? and how to use it with some Google APIs
 
Django para portais de alta visibilidade. tdc 2013
Django para portais de alta visibilidade.   tdc 2013Django para portais de alta visibilidade.   tdc 2013
Django para portais de alta visibilidade. tdc 2013
 
Build website in_django
Build website in_django Build website in_django
Build website in_django
 
Writing your first web app using Python and Flask
Writing your first web app using Python and FlaskWriting your first web app using Python and Flask
Writing your first web app using Python and Flask
 

Similar to Flask Introduction - Python Meetup

LvivPy - Flask in details
LvivPy - Flask in detailsLvivPy - Flask in details
LvivPy - Flask in detailsMax Klymyshyn
 
Building Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJSBuilding Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJSAntonio Peric-Mazar
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Ondřej Machulda
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Racksickill
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's CodeWildan Maulana
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practicesmanugoel2003
 
ElggCamp Santiago> For Developers!
ElggCamp Santiago> For Developers!ElggCamp Santiago> For Developers!
ElggCamp Santiago> For Developers!Condiminds
 
ElggCamp Santiago - Dev Edition
ElggCamp Santiago - Dev EditionElggCamp Santiago - Dev Edition
ElggCamp Santiago - Dev EditionBrett Profitt
 
Zen and the Art of Claroline Module Development
Zen and the Art of Claroline Module DevelopmentZen and the Art of Claroline Module Development
Zen and the Art of Claroline Module DevelopmentClaroline
 
Symfony2 Introduction Presentation
Symfony2 Introduction PresentationSymfony2 Introduction Presentation
Symfony2 Introduction PresentationNerd Tzanetopoulos
 
Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...
Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...
Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...Akira Tsuruda
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfonyFrancois Zaninotto
 
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...Dilouar Hossain
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptLaurence Svekis ✔
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress DevelopmentAdam Tomat
 

Similar to Flask Introduction - Python Meetup (20)

LvivPy - Flask in details
LvivPy - Flask in detailsLvivPy - Flask in details
LvivPy - Flask in details
 
Building Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJSBuilding Single Page Application (SPA) with Symfony2 and AngularJS
Building Single Page Application (SPA) with Symfony2 and AngularJS
 
Intro to Laravel 4
Intro to Laravel 4Intro to Laravel 4
Intro to Laravel 4
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
 
Introduce Django
Introduce DjangoIntroduce Django
Introduce Django
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's Code
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
ElggCamp Santiago> For Developers!
ElggCamp Santiago> For Developers!ElggCamp Santiago> For Developers!
ElggCamp Santiago> For Developers!
 
ElggCamp Santiago - Dev Edition
ElggCamp Santiago - Dev EditionElggCamp Santiago - Dev Edition
ElggCamp Santiago - Dev Edition
 
Zen and the Art of Claroline Module Development
Zen and the Art of Claroline Module DevelopmentZen and the Art of Claroline Module Development
Zen and the Art of Claroline Module Development
 
Symfony2 revealed
Symfony2 revealedSymfony2 revealed
Symfony2 revealed
 
Symfony2 Introduction Presentation
Symfony2 Introduction PresentationSymfony2 Introduction Presentation
Symfony2 Introduction Presentation
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...
Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...
Flask-RESTPlusで便利なREST API開発 | Productive RESTful API development with Flask-...
 
Simplify your professional web development with symfony
Simplify your professional web development with symfonySimplify your professional web development with symfony
Simplify your professional web development with symfony
 
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...Laravel development (Laravel History, Environment Setup & Laravel Installatio...
Laravel development (Laravel History, Environment Setup & Laravel Installatio...
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development
 

Recently uploaded

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 

Flask Introduction - Python Meetup

  • 1. Flask, for people who like to have a little drink at night Areski Belaid <areski@gmail.com> 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 : areski@gmail.com