Flask


Flask is a Python web framework built with a small core and easy-to-extend philosophy.

Official Flask logo. Flask Artwork License.

Why is Flask a good web framework choice?

Flask is considered more Pythonic than Django because Flask web application code is in most cases more explicit. Flask is easy to get started with as a beginner because there is little boilerplate code for getting a simple app up and running.

For example, here’s a valid “hello world” web application with Flask (the equivalent in Django would be significantly more code):

from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':
    app.run()

Flask was also written several years after Django and therefore learned from the Python community’s reactions as the framework evolved. Jökull Sólberg wrote a great piece articulating to this effect in his experience switching between Flask and Django.

Flask is an implementation of the web frameworks concept. Learn how these parts fit together in the web development chapter or view all topics.

Flask resources

Open source Flask example projects

Flask project templates

Flask framework learning checklist

  1. Install Flask on your local development machine.
  2. Work through the 18-part Flask tutorial listed first under “Flask resources” above.
  3. Read through Flask Extensions Registry to find out what extensions you’ll need to build your project.
  4. Start coding your Flask app based on what you learned from the 18 part Flask tutorial plus open source example applications found below.
  5. Move on to the deployment section to get your initial Flask project on the web.
Anúncios

Other Python Web Frameworks


Python has a significant number of newer and less frequently-used web frameworks that are still worth your time to investigate. The list on this page does not include the following web frameworks that have their own dedicated pages:

TurboGears

TurboGears, born as a full stack layer on top of Pylons, is now a standalone web framework that can act both as a full stack library (like Django) or as a micro framework.

TurboGears logo.

web.py

web.py is a Python web framework designed for simplicity in building web applications.

web2py

Web2py is a batteries-included philosophy framework with project structure based on model-view-controller patterns.

CherryPy

CherryPy is billed as a minimalist web framework, from the perspective of the amount of code needed to write a web application using the framework. The project has a long history and made a major transition between the second and third release.

Muffin

Muffin is a web framework built on top of the asyncio module in the Python 3.4+ standard library. Muffin takes inspiration from Flask with URL routes defined as decorators upon view functions. The Peewee ORM is used instead of the more common SQLAlchemy ORM.

Ray

Ray is a framework for building RESTful APIs, similar to Falcon. The introductory postprovides some initial code to get started with creating endpoints, adding authentication and protecting against malicious clients.

Other web framework resources

Other frameworks learning checklist

  1. Read through the web frameworks listed above and check out their project websites.
  2. It’s useful to know what other web frameworks exist besides Django and Flask. However, when you’re just starting to learn to program there are significantly more tutorials and resources for Django and Flask on the web. My recommendation is to start with one of those two frameworks then expand your knowledge from there.

Django


Django is a widely-used Python web application framework with a “batteries-included” philosophy. The principle behind batteries-included is that the common functionality for building web applications should come with the framework instead of as separate libraries.

Official Django logo. Trademark Django Software Foundation.

For example, authentication, URL routing, a template engine, an object-relational mapper(ORM), and database schema migrations (as of version 1.7) are all included with the Django framework. Compare that included functionality to the Flask framework which requires a separate library such as Flask-Login to perform user authentication.

The batteries-included and extensibility philosophies are simply two different ways to tackle framework building. Neither philosophy is inherently better than the other one.

Django is an implementation of the web frameworks concept. Learn how these pieces fit together in the web development chapter or view the table of contents for all topics.

Why is Django a good web framework choice?

The Django project’s stability, performance and community have grown tremendously over the past decade since the framework’s creation. Detailed tutorials and good practices are readily available on the web and in books. The framework continues to add significant new functionality such as database migrations with each release.

I highly recommend the Django framework as a starting place for new Python web developers because the official documentation and tutorials are some of the best anywhere in software development. Many cities also have Django-specific groups such as Django District, Django Boston and San Francisco Django so new developers can get help when they are stuck.

There’s some debate on whether learning Python by using Django is a bad idea. However, that criticism is invalid if you take the time to learn the Python syntax and language semantics first before diving into web development.

Django books and tutorials

There are a slew of free or low cost resources out there for Django. Since Django was released over 10 years ago and has had a huge number of updates since then, when you’re looking for an up-to-date Django book check out the list below or read this post showing current Django books as of Django 1.9.

Django videos

Are you looking for Django videos in addition to articles? There is a special section for Django and web development on the best Python videos page.

Django migrations

  • Paul Hallett wrote a detailed Django 1.7 app upgrade guide on the Twilio blog from his experience working with the django-twilio package.
  • Real Python’s migrations primer explores the difference between South’s migrations and the built-in Django 1.7 migrations as well as how you use them.
  • Andrew Pinkham’s “Upgrading to Django 1.7” series is great learning material for understanding what’s changed in this major release and how to adapt your Django project. Part 1, part 2 and part 3 and part 4 are now all available to read.
  • Django migrations without downtimes shows one potential way of performing on-line schema migrations with Django.

Channels in 1.9+

Channels are a new mechanism in Django 1.9 provided as a standalone app. They may be incorporated into the core framework in 2.0+. Channels provide “real-time” full-duplex communication between the browser and the server based on WebSockets.

Django testing

Django with Angular (Djangular) resources

Django ORM resources

Django comes with its own custom object-relational mapper (ORM) typically referred to as “the Django ORM”. Learn more about the Django ORM on the Python object-relational mappers page that includes a section specifically for the Django ORM as well as additional resources and tutorials.

Static and media files

Deploying and handling static and media files can be confusing for new Django developers. These resources along with the static content page are useful for figuring out how to handle these files properly.

Open source Django example projects

  • Browser calls with Django and Twilio shows how to build a web app with Django andTwilio Client to turn a user’s web browser into a full-fledged phone. Pretty awesome!
  • Txt 2 React is a full Django web app that allows audiences to text in during a presentation with feedback or questions.
  • Openduty is a website status checking and alert system similar to PagerDuty.
  • Courtside is a pick up sports web application written and maintained by the author of PyCoder’s Weekly.
  • These two Django Interactive Voice Response (IVR) system web application repositories part 1 and part 2 show you how to build a really cool Django application. There’s also an accompanying blog post with detailed explanations of each step.
  • Taiga is a project management tool built with Django as the backend and AngularJS as the front end.

Django project templates

Project templates, not to be confused with a template engine, generate boilerplate code for a base Django project plus optional libraries that are often used when developing web applications.

Django learning checklist

  1. Install Django on your local development machine.
  2. Work through the initial “polls” tutorial.
  3. Build a few more simple applications using the tutorial resources found in the “Django resources” section.
  4. Start coding your own Django project with help from the official documentation and resource links below. You’ll make plenty of mistakes which is critical on your path to learning the right way to build applications.
  5. Read 2 Scoops of Django to understand Django good practices and learn better ways of building Django web applications.
  6. Move on to the deployment section to get your Django project on the web.

What do you need to learn next for your Django app?

My app runs but looks awful. How do I style the user interface?

How do I integrate existing web APIs into my application?

I’ve built a Python web app, now how do I deploy it?

Web frameworks


A web framework is a code library that makes a developer’s life easier when building reliable, scalable and maintainable web applications.

Why are web frameworks useful?

Web frameworks encapsulate what developers have learned over the past twenty years while programming sites and applications for the web. Frameworks make it easier to reuse code for common HTTP operations and to structure projects so other developers with knowledge of the framework can quickly build and maintain the application.

Web frameworks are a concept implemented by Django, Flask, Bottle, Pyramid, Morepath and several other libraries. Learn how the parts fit together in the web development chapter or view all topics.

Common web framework functionality

Frameworks provide functionality in their code or through extensions to perform common operations required to run web applications. These common operations include:

  1. URL routing
  2. HTML, XML, JSON, and other output format templating
  3. Database manipulation
  4. Security against Cross-site request forgery (CSRF) and other attacks
  5. Session storage and retrieval

Not all web frameworks include code for all of the above functionality. Frameworks fall on the spectrum from executing a single use case to providing every known web framework feature to every developer. Some frameworks take the “batteries-included” approach where everything possible comes bundled with the framework while others have a minimal core package that is amenable to extensions provided by other packages.

For example, the Django web application framework includes an Object-Relational Mapping (ORM) layer that abstracts relational database read, write, query, and delete operations. However, Django’s ORM cannot work without significant modification on non-relational databases such as MongoDB.

Some other web frameworks such as Flask and Pyramid are easier to use with non-relational databases by incorporating external Python libraries. There is a spectrum between minimal functionality with easy extensibility on one end and including everything in the framework with tight integration on the other end.

Comparing web frameworks

Are you curious about how the code in a Django project is structured compared with Flask? Check out this Django web application tutorial and then view the same application built with Flask.

There is also a repository called compare-python-web-frameworks where the same web application is being coded with varying Python web frameworks, templating engines andobject-relational mappers.

While you’re learning about web frameworks you should also study web application deployment and web APIs.

Do I have to use a web framework?

Whether or not you use a web framework in your project depends on your experience with web development and what you’re trying to accomplish. If you are a beginner programmer and just want to work on a web application as a learning project then a framework can help you understand the concepts listed above, such as URL routing, data manipulation and authentication that are common to the majority of web applications.

On the other hand if you’re an experienced programmer with significant web development experience you may feel like the existing frameworks do not match your project’s requirements. In that case, you can mix and match open source libraries such as Werkzeug for WSGI plumbing with your own code to create your own framework. There’s still plenty of room in the Python ecosystem for new frameworks to satisfy the needs of web developers that are unmet by Django, Flask, Pyramid, Bottle and many others.

In short, whether or not you need to use a web framework to build a web application depends on your experience and what you’re trying to accomplish. Using a web framework to build a web application certainly isn’t required, but it’ll make most developers’ lives easier in many cases.

Web framework resources

  • When you are learning how to use one or more web frameworks it’s helpful to have an idea of what the code under the covers is doing. This post on building a simple Python framework from scratch shows how HTTP connections, routing, and requests can work in just 320 lines of code. This post is awesome even though the resulting framework is a simplification of what frameworks such as Django, Flask and Pyramidallow developers to accomplish.
  • Check out the answer to the “What is a web framework and how does it compare to LAMP?” question on Stack Overflow.
  • Frameworks is a really well done short video that explains how to choose between web frameworks. The author has some particular opinions about what should be in a framework. For the most part I agree although I’ve found sessions and database ORMs to be a helpful part of a framework when done well.
  • What is a web framework?” is an in-depth explanation of what web frameworks are and their relation to web servers.
  • Django vs Flask vs Pyramid: Choosing a Python Web Framework contains background information and code comparisons for similar web applications built in these three big Python frameworks.
  • This fascinating blog post takes a look at the code complexity of several Python web frameworks by providing visualizations based on their code bases.
  • Python’s web frameworks benchmarks is a test of the responsiveness of a framework with encoding an object to JSON and returning it as a response as well as retrieving data from the database and rendering it in a template. There were no conclusive results but the output is fun to read about nonetheless.
  • What web frameworks do you use and why are they awesome? is a language agnostic Reddit discussion on web frameworks. It’s interesting to see what programmers in other languages like and dislike about their suite of web frameworks compared to the main Python frameworks.
  • This user-voted question & answer site asked “What are the best general purpose Python web frameworks usable in production?“. The votes aren’t as important as the list of the many frameworks that are available to Python developers.

Web frameworks learning checklist

  1. Choose a major Python web framework (Django or Flask are recommended) and stick with it. When you’re just starting it’s best to learn one framework first instead of bouncing around trying to understand every framework.
  2. Work through a detailed tutorial found within the resources links on the framework’s page.
  3. Study open source examples built with your framework of choice so you can take parts of those projects and reuse the code in your application.
  4. Build the first simple iteration of your web application then go to the deploymentsection to make it accessible on the web.

Which web framework do you want to learn about?

What is the Django web framework?

I want to learn about the Flask web framework.

What other Python web frameworks exist?

Pylons project


From Wikipedia, the free encyclopedia
Pylons Project
Pylons Project logo on transparent background.png
Type of site
Python frameworkdevelopment
Available in English
Owner Open source
Editor Anonymous
Website pylonsproject.org
Commercial No
Registration None
Launched December 27, 2010
Current status Active

Pylons Project is an open source organization that develops a set of web application technologies written in Python. Initially the project was a single web framework called Pylons, but after the merger with the repoze.bfg framework under the new name Pyramid, the Pylons Project now consists of multiple related web application technologies.

Pylons Web Framework[edit]

Adicionar ao Google AgendaPylons Framework
Pylonsfw.png
Developer(s) Ben Bangert, James Gardner
Initial release September 2005; 11 years ago[1]
Stable release
1.0.2[2] / July 21, 2015; 22 months ago
Development status maintenance-only mode
Written in Python
Operating system Cross-platform
Type Web application framework
License BSD license
Website pylonsproject.org/about-pylons-framework.html

Pylons Framework is an open source Web application framework written in Python. It makes extensive use of the Web Server Gateway Interface standard to promote reusability and to separate functionality into distinct modules.[3]It is strongly influenced by Ruby on Rails: two of its main components, Routes and WebHelpers, are Python reimplementations of Rails features.

Structure[edit]

Pylons is well known for having a near-complete stack of third-party tools, eschewing the “not–invented–here” phenomenon.

Installation, dependencies, and setup[edit]

The official installation method of Pylons is through EasyInstall via the Python Package Index (PyPI), and most of the additional tools are typically installed the same way. EasyInstall also handles package dependencies when relevant. Some distributions could also package Pylons and Paste, but it is likely that any distribution’s packages would lag the official distribution. Pylons may also be installed by hand by renaming its .egg file to .zip and extracting the contents.

Paste is used for project setup, testing, and deployment. Using the common INI configuration format, Paste allows for multiple “profiles”, so that developers can run development and deployment setups from the same codebase without revealing sensitive parts of Pylons, such as the interactive debugger, to production users.

URL dispatch[edit]

Currently the only widely used URL dispatcher for Pylons is Routes, a Python reimplementation of Ruby on Rails‘ URL dispatching, although any WSGI-compatible URL dispatcher can be used. While Routes is a separate library, it was developed for use in Pylons and its development remains closely in sync with Pylons.

HTML generation[edit]

Another piece of Rails adapted for Pylons is WebHelpers, which provides URL mapping based on the Routes configuration. WebHelpers also provides some utility functions for generating JavaScript code making use of the script.aculo.us and Prototype libraries.

FormEncode and FormBuild are used for HTML form validation and generation; there has been some use of Mako for form generation using Mako’s inheritance model.

Templating[edit]

Myghty was the default Pylons templating language, but as of version 0.9.6 it has been replaced by Mako.[4] Both templating languages are text-based (as opposed to XML-based), and support includes, inheritance and embedding arbitrary Python code.

Because of Pylons’ loosely coupled layers, other templating languages can be used as well. Genshi, an XML-based templating language, can be used in lieu of either Mako or Myghty.[5]

Database abstraction and object-relational mapping[edit]

Pylons has no default database library. Both SQLObject and SQLAlchemy are known to be used.

Merger with repoze.bfg and birth of Pyramid Web Framework[edit]

Pylons has developed into the Pylons Project, and the old code from Pylons 1.0 is now in maintenance-only mode. However, pursuant to the project’s merger with repoze.bfg since November 2010, newer versions of Pylons are actually different from the original Pylons 1.0. Pylons developers initially planned to rewrite certain portions of the code, but they observed that the new code was approximating repoze.bfg, which led to the merger of Pylons and repoze.bfg.[6][7] This led to repoze.bfg (a part of the Repoze Python-based web framework) to become rebranded and relaunched as the Pyramid web framework.[8]

Pyramid[edit]

Adicionar ao Google AgendaPyramid
Pyramid web framework logo on transparent background.png
Developer(s) Chris McDonough, Ben Bangert, Michael Merickel, Bert JW Regeer, Steve Piercy
Initial release July 8, 2008; 8 years ago[9]
Stable release
1.8.4[10] / June 11, 2017; 13 days ago
Written in Python
Operating system Cross-platform
Type Web application framework
License BSD License
Website trypyramid.com

Pyramid is an open source web framework written in Python and is based on WSGI. It is a minimalistic web framework inspired by Zope, Pylons and Django.[11]

Originally called “repoze.bfg”, Pyramid gathered attention mostly in the Zope[12] and Plone community as the Open Society Institute‘s KARL project migrated from Plone to BFG.[13] In 2010 it was announced that the Pylons framework will move over to using BFG as a base in version 1.5.[14] As a result of the inclusion of BFG into the Pylons project, BFG was renamed Pyramid.[15]

Features[edit]

Pyramid is a minimalistic, platform-independent web framework. It is persistence agnostic and is integrated both with SQL databases via SQLAlchemy and with the Zope Object Database, as well as other NoSQL databases, such as CouchDB.[11]

Pyramid allows developers to define routes using regular expressions that map to objects. Like its fellow framework Zope, Pyramid also allows hierarchical object traversal, where each part of a URL is an object containing other objects, in a way that is similar to folders in a filesystem.[16]

See also[edit]

References[edit]

  1. Jump up^ History of Pylons
  2. Jump up^ Pylons Changelog
  3. Jump up^ The mod_wsgi project includes both Pylons and Pyramid in its list of supported major Python web frameworks.
  4. Jump up^ Haas, Christoph Beginning Pylons. Retrieved July 5, 2007
  5. Jump up^ Genshi Wiki Pylons with Genshi Retrieved July 5, 2007
  6. Jump up^ Pylons Project FAQ. URL: http://pylonsproject.org/about-pylons-project.html
  7. Jump up^ Notes on the Pylons and repoze.bfg merger. URL: http://be.groovie.org/post/1558848023/notes-on-the-pylons-repoze-bfg-merger
  8. Jump up^ About Pyramid. URL: https://trypyramid.com/
  9. Jump up^ “Pyramid Change History”. The Pyramid Web Framework. Pylons Project. Retrieved 10 March 2017.
  10. Jump up^ “Pyramid Change History”.
  11. ^ Jump up to:a b Chris McDonough. “repoze.bfg introduction”. Retrieved 2010-09-21.
  12. Jump up^ Gary Poster (2010-09-17). “Zope Summit, DZUG, repoze.bfg”. Retrieved 2010-09-21.
  13. Jump up^ Thomas Moroz, Chris Rossi & Calvin Hendryx-Parker (2009-10-28). KARL – large-scale Knowledge Management. Budapest, Hungary: Plone Conference 2009. Retrieved 2010-09-23.
  14. Jump up^ Ben Bangert (2010-09-18). “Emails explaining the Pylons 2 structure”. Retrieved 2010-09-21.
  15. Jump up^ “repoze.bfg is now Pyramid”. Retrieved 2011-01-03.
  16. Jump up^ Wichert Akkerman (2009-10-28). Lessons from other frameworks. Budapest, Hungary: Plone Conference 2009. Event occurs at 25:50. Retrieved 2010-09-21.

Further reading[edit]

External links[edit]

Quick Tutorial for Pyramid


Pyramid is a web framework for Python 2 and 3. This tutorial gives a Python 3/2-compatible, high-level tour of the major features.

This hands-on tutorial covers “a little about a lot”: practical introductions to the most common facilities. Fun, fast-paced, and most certainly not aimed at experts of the Pyramid web framework.

Indices and tables

Flask (web framework)


From Wikipedia, the free encyclopedia
Adicionar ao Google AgendaFlask
Flask logo.svg
Developer(s) Armin Ronacher
Initial release April 1, 2010; 7 years ago
Stable release
0.12 / December 21, 2016; 5 months ago[1]
Repository https://github.com/pallets/flask, git://github.com/mitsuhiko/flask.git
Written in Python
Operating system Cross-platform
Type Web framework
License BSD
Website flask.pocoo.org

Flask is a micro web framework written in Python and based on the Werkzeug toolkit and Jinja2 template engine. It is BSD licensed.

The latest stable version of Flask is 0.12 as of December 2016.[2] Applications that use the Flask framework include Pinterest,[3] LinkedIn,[4] and the community web page for Flask itself.[5]

Flask is called a micro framework because it does not require particular tools or libraries.[6] It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions. However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools. Extensions are updated far more regularly than the core Flask program.[7]

History[edit]

In 2004, Pocoo was formed as an international group of Python enthusiasts.[8]

Flask was created by Armin Ronacher of Pocoo:

“It came out of an April Fool’s joke but proved popular enough to make into a serious application in its own right.”[9][10][11]

Flask is based on the Werkzeug WSGI toolkit and Jinja2 template engine, both of them Pocoo projects that were created[when?] when Ronacher and Georg Brandl were building a bulletin board system written in Python.[12]

Despite the lack of a major release, Flask has become extremely popular among Python enthusiasts. As of mid 2016, it was the most popular Python web development framework on GitHub.[13]

Features[edit]

  • Contains development server and debugger
  • Integrated support for unit testing
  • RESTful request dispatching
  • Uses Jinja2 templating
  • Support for secure cookies (client side sessions)
  • 100% WSGI 1.0 compliant
  • Unicode-based
  • Extensive documentation
  • Google App Engine compatibility
  • Extensions available to enhance features desired

Example[edit]

The following code shows a simple web application that prints “Hello World!”:

from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
    app.run()

See also[edit]

References[edit]

External links[edit]