Skip to content

Intro to Flask

tsungtwu edited this page Apr 19, 2017 · 1 revision

Intro to Flask

Quick Start

http://flask.pocoo.org/docs/0.12/quickstart/#routing

Install

pip install Flask

Flask folder Structure

.
|──────app/
| |────__init__.py
| |────api/
| | |────__init__.py
| | |────cve/
| | |────tweet/
| |──────config.Development.cfg
| |──────config.Production.cfg
| |──────config.Testing.cfg
| |────model/
| |────util/
|──────run.py
|──────tests/
| |──────test_cve.py
| |──────test_twitter.py
| |──────testData/

Flask Cinfiguration

Example

app = Flask(__name__)
app.config['DEBUG'] = True

Configuring From Files

Example Usage

app = Flask(__name__, )
app.config.from_pyfile('application.cfg')

application.cfg example

#Flask settings
DEBUG = True  # True/False
SERVER_NAME = 'localhost:5000'
JSON_AS_ASCII = 'false'

#elasticsearch settings
ELASTICSEARCH_HOST = 'http://localhost'
ELASTICSEARCH_PORT = '9200'

Builtin Configuration Values

The following configuration values are used internally by Flask:

Depoly


Using the simple app.run() from within Flask creates a single synchronous server on a single thread capable of serving only one client at a time. It is intended for use in controlled environments with low demand (i.e. development, debugging) for exactly this reason.

Spawning threads and managing them yourself is probably not going to get you very far either, because of the Python GIL.

That said, you do still have some good options. Gunicorn is a solid, easy-to-use WSGI server that will let you spawn multiple workers (separate processes, so no GIL worries), and even comes with asynchronous workers that will speed up your app (and make it more secure) with little to no work on your part (especially with Flask).

Still, even Gunicorn should probably not be directly publicly exposed. In production, it should be used behind a more robust HTTP server; nginx tends to go well with Gunicorn and Flask.

Reference

Flask Full Tutorial:

Flask Extension

Concept

All about Flask Extension

http://flask.pocoo.org/extensions/

  • Flask Testing

http://pythonhosted.org/Flask-Testing/ ES unit test example : https://github.com/pfig/flask-elasticsearch

Reference

Offical Website

Tutorial

Clone this wiki locally