Flask

Flask logo. Source: Wikipedia 2019.
Flask logo. Source: Wikipedia 2019.

Flask is a simple and minimalist web framework written in Python. It has no database abstraction layer, form validation, or other components that a web app might require. However, Flask can be enhanced with extensions that can add application features as if they were implemented in Flask itself. It's open source under a BSD license.

Flask is easy to set up and learn. One can write a Flask app in as few as seven lines of code and extend it to thousands. Among the big companies that use Flask are LinkedIn, Pinterest, Reddit, and many more.

Flask along with Bootstrap and SQLite can be used to easily develop full-functioning web apps.

Discussion

  • With so many frameworks out there, what's special about Flask?

    Flask uses Python and is lightweight. It consumes minimum resources to get the work done. It's very easy to learn once you have a sound knowledge of Python. Flask enables rapid prototyping of your app and works very efficiently for small applications. It enables you to focus more on other jobs than being stuck with web server management.

    Among the important features of Flask are a built-in web server and debugger, unit testing support, RESTful request dispatching, secure cookies, WSGI compliance, Unicode support and good documentation.

  • How does Flask compare to Django?
    Performance of Flask is better than Django. Source: TechEmpower 2018.
    Performance of Flask is better than Django. Source: TechEmpower 2018.

    Django is a full-featured MVC framework while Flask is a micro-framework. What this means is that Django takes a "batteries-included" approach by providing many packages typically used by web apps. Flask on the other hand let's you bring in packages as required for your project.

    Django is a stricter and more mature framework than Flask. Django has it's own ways of doing things, while Flask gives you all the freedom you want. For example, Django comes with an ORM but with Flask you can use any ORM of your choice.

    Flask is easier to learn due to its minimalism. Beginners looking to learn a Python-based web framework can start with Flask. For simple apps, Flask is more than adequate. For complex apps or to make a polished product, you can consider migrating to Django. Because Django includes lots of useful stuff by default, it might be faster to develop complex apps. For example, Django comes with an administrator interface that can be customized. This can be useful when building a Content Management System (CMS).

  • As a beginner, how do I get started with Flask?

    One can refer to the Flask official documentation. The User's Guide contains a quickstart guide, detailed tutorials and useful patterns in Flask.

    Week 7 of Harvard's CS50 course is useful if you want a proper classroom type tutoring with assignments. Flask Tutorial Playlist by Corey Schafer is also a good starting point.

    Use pip to install Flask: pip install -U Flask

  • Which websites use Flask?

    There's an curated list of sites using Flask. Another list mentions big names: Red Hat, Airbnb, Netflix, Lyft, Reddit, Uber, and Samsung. Other big names include Pinterest, Twilio and LinkedIn.

  • What are some popular extensions used along with Flask?
    Flask + Bootstrap + SQLite. Source: Adapted from Elliot 2015.
    Flask + Bootstrap + SQLite. Source: Adapted from Elliot 2015.

    Flask can be used to implement an Model-View-Controller (MVC) pattern of web framework: Flask (controller), SQLite (model), Bootstrap (view). Jinja2 is popular for rendering HTML templates and is part of the view. Flask can be used with front-end frameworks such as VueJS but you would have to learn JavaScript.

    These are some commonly used extensions:

    • Flask-Cors: A Flask extension for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible.
    • Flask-User: Customizable user authentication, user management, password recovery, and more.
    • Flask-PyMongo: This bridges Flask and PyMongo. Provides some convenient helpers. PyMongo is a MongoDB driver.
    • Flask-SQLAlchemy: Database abstraction layer and Object Relational Mapper (ORM) popular for Flask apps.
  • How do I deploy a Flask application to production?
    Illustrating traditional Flask web app during development and production. Source: Kennedy 2017.
    Illustrating traditional Flask web app during development and production. Source: Kennedy 2017.

    Flask comes with a simple built-in server. This runs on the default port 5000. This is commonly used for development but not recommended for production because it doesn't scale well.

    For production, many cloud providers document procedures on how to deploy Flask on their platforms. If you wish to deploy Flask on your own, mod_wsgi is an essential module for Apache server. If Nginx server is used, it will typically serve static files and the rest will be handed over to Gunicorn server, which becomes the WSGI entry point for the Flask application. In fact, Nginx can reverse proxy a HTTP request to any WSGI-compliant server including Apache, Gunicorn or uWSGI.

Milestones

2004

Pocco is formed as an international group of Python Enthusiasts.

Apr
2010

Coders from Pocco release version 0.1 of Flask. Flask is really a combination of two other projects developed at Pocco: Werkzeug (a web programming toolkit) and Jinja2 (a templating engine). It's initially called Denied–The next generation micro-web-framework.

Sep
2016

Harvard's popular CS50: Introduction to Computer Science course incorporates Flask to teach web development, thus shifting focus from PHP to Python. It becomes available on edX as CS50x from 2017.

Apr
2018

Flask 1.0 is launched. Python versions 2.7 and 3.3 are no longer supported. Thus, Flask requires Python 2.7, Python 3.4 and above, or PyPy.

May
2018

Flask 1.0.2 is released.

Sample Code

  • # A simple app in Flask
     
    from flask import Flask
    app = Flask(__name__)
     
    @app.route("/")
    def hello():
        return "Hello Devopedia!"
     
    if __name__ == "__main__":
        app.run()
     

References

  1. CS50. 2017. "CS50 Explained 2016 - Week 8 - From C to Python, Interpreted Languages." YouTube, July 18. Accessed 2019-01-19.
  2. CS50. 2018. "CS50: Week 7 Notes." Harvard University. Accessed 2019-01-19.
  3. Chandra, Varun. 2016. "Who is behind Flask and what is the story of its creation?" Quora, July 05. Accessed 2019-01-19.
  4. Dwyer, Gareth. 2017. "Flask vs. Django: Why Flask Might Be Better." Code Mentor, February 13. Accessed 2019-01-19.
  5. EDUCBA. 2018. "Django vs Flask." EDUCBA, August 13. Accessed 2019-01-19.
  6. Elliot, Oliver. 2015. "Notes: Making a Simple Website with Flask." June 06. Accessed 2019-01-19.
  7. Fatima, H. 2018. "Flask vs Django: How to Choose the Right Web Framework for Your Web App." Blog, Reseller Club, January 13. Accessed 2019-01-19.
  8. Flask. 2019. "Homepage." Accessed 2019-01-19.
  9. Flask-Cors. 2018. "Flask CORS extension." Flask-Cors, via Read The Docs, v3.0.7. Accessed 2019-01-19.
  10. Flask Docs. 2019. "User's Guide." Flask 1.0, Pocco. Accessed 2019-01-19.
  11. Flask GitHub. 2019. "pallets/flask." Flask, on GitHub, January 18. Accessed 2019-01-19.
  12. Flask-PyMongo. 2018. "Flask PyMongo Extension." Flask-PyMongo, via Read The Docs, v2.2.0. Accessed 2019-01-19.
  13. Flask-User. 2018. "Flask User Extension." Flask-User, via Read The Docs, v1.0. Accessed 2019-01-19.
  14. Kennedy, Patrick. 2017. "Using Docker for Flask Application Development (not just Production!)." Patrick's Software Blog, June 23. Accessed 2019-01-19.
  15. Lord, David. 2018. "Flask 1.0 released." Blog, The Pallets Project, April 26. Accessed 2019-01-19.
  16. PyPi. 2018. "Flask 1.0.2." PyPi, May 02. Accessed 2019-01-19.
  17. Quora. 2015. "What is the largest site created using Flask?" May 21. Accessed 2019-01-19.
  18. Rocha, Bruno. 2018. "rochacbruno/flask-powered." On GitHub, August 10. Accessed 2019-01-19.
  19. Sanders, Rachel. 2014. "Developing Flask Extensions." PyCon 2014, via YouTube, April 23. Accessed 2019-01-19.
  20. TechEmpower. 2018. "Web Framework Benchmarks: Round 17, Flask vs Django." TechEmpower, October 30. Accessed 2019-01-19.
  21. Wikipedia. 2019. "Flask (web framework)." Wikipedia, January 16. Accessed 2019-01-19.

Further Reading

  1. Flask Documentation
  2. Flask by Example, RealPython.com
  3. CS50 Week-7 Notes, Harvard University
  4. Flask on GitHub
  5. Rachel Sanders at PyCon14: Developing Flask Extensions
  6. Reigns, Stephanie. 2018. "Django vs Flask – Best Python Web Framework for Beginners?" Coders Eye, May 01. Accessed 2019-01-19.

Article Stats

Author-wise Stats for Article Edits

Author
No. of Edits
No. of Chats
DevCoins
5
1
932
2
5
869
879
Words
6
Likes
14K
Hits

Cite As

Devopedia. 2019. "Flask." Version 7, January 19. Accessed 2024-06-25. https://devopedia.org/flask
Contributed by
2 authors


Last updated on
2019-01-19 15:52:18

Improve this article

Article Warnings

  • In References, replace these sub-standard sources: educba.com