background preloader


Facebook Twitter

How to Handle Requests in Flask. In this blog post I will describe how to handle web requests and serve responses for them in a Flask application.

How to Handle Requests in Flask

Routing Routing is used to bind a URL to a specific action (function) and allows web applications to have better URLs, easy to remember. Flask provides developers with a route decorator, which is used to register a view function to a given URL rule. More about the route decorator can be found here. It is possible to add variable parts to the URL by using the <var_name> section. Now you could reuse the provided var_name argument in your Flask application by adding a parameter to the view function matching the variable name. @app.route('/hi/<name>') def say_hi(name): return 'Hello, %s!

' Optionally a converter can be used together with the variable: @app.route('/age/<int:age>') def show_age(age): return 'Age is %d! ' – string – default; – int – accepts integers; – float – accepts floating point values; – path – accepts slashes; By default Flask handles only GET requests. Damyanbogoev/flask-bookshelf. API — Flask Documentation (0.10) This part of the documentation covers all the interfaces of Flask.

API — Flask Documentation (0.10)

For parts where Flask depends on external libraries, we document the most important right here and provide links to the canonical documentation. Application Object class flask.Flask(import_name, static_path=None, static_url_path=None, static_folder='static', template_folder='templates', instance_path=None, instance_relative_config=False) The flask object implements a WSGI application and acts as the central object. It is passed the name of the module or package of the application. The name of the package is used to resolve resources from inside the package or the folder the module is contained in depending on if the package parameter resolves to an actual python package (a folder with an file inside) or a standard module (just a .py file).

For more information about resource loading, see open_resource(). Usually you create a Flask instance in your main module or in the file of your package like this: g. Angularjs - How to use token based authentication to access protected assets inline? Uploading Files — Flask Documentation (0.10) Ah yes, the good old problem of file uploads.

Uploading Files — Flask Documentation (0.10)

The basic idea of file uploads is actually quite simple. It basically works like this: A Gentle Introduction Let’s start with a very basic application that uploads a file to a specific upload folder and displays a file to the user. Let’s look at the bootstrapping code for our application: import osfrom flask import Flask, request, redirect, url_forfrom werkzeug import secure_filename UPLOAD_FOLDER = '/path/to/the/uploads'ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) app = Flask(__name__)app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER So first we need a couple of imports.

Why do we limit the extensions that are allowed? Next the functions that check if an extension is valid and that uploads the file and redirects the user to the URL for the uploaded file: def allowed_file(filename): return '.' in filename and \ filename.rsplit(' So what does that secure_filename() function actually do?

Information for the Pros Improving Uploads. The Flask Mega-Tutorial, Part I: Hello, World! This is the first article in a series where I will be documenting my experience writing web applications in Python using the Flask microframework.

The Flask Mega-Tutorial, Part I: Hello, World!

NOTE: This article was revised in September 2014 to be in sync with current versions of Python and Flask. Here is an index of all the articles in the series that have been published to date: My background I'm a software engineer with double digit years of experience developing complex applications in several languages. I first learned Python as part of an effort to create bindings for a C++ library at work. In addition to Python, I've written web apps in PHP, Ruby, Smalltalk and believe it or not, also in C++. UPDATE: I have written a book titled "Flask Web Development", published in 2014 by O'Reilly Media. The application The application I'm going to develop as part of this tutorial is a decently featured microblogging server that I decided to call microblog. These are some of the topics I will cover as we make progress with this project: #!

. Quickstart — Flask Documentation (0.10) Eager to get started?

Quickstart — Flask Documentation (0.10)

This page gives a good introduction to Flask. It assumes you already have Flask installed. If you do not, head over to the Installation section. Tutorial — Flask Documentation (0.10)