Intro to Flask - tsungtwu/flask-example GitHub Wiki
Intro to Flask
Quick Start
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 + Gunicorn + Nginx ι¨η½²
- https://hostpresto.com/community/tutorials/deploy-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04/
- Kickstarting Flask on Ubuntu - Setup and Deployment
- http://koko.ntex.tw/wordpress/djangopython-deploy-django-on-ubuntu-install-gunicorn-nginx/
- https://hostpresto.com/community/tutorials/deploy-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04/
Flask Full Tutorial:
- https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
- http://igordavydenko.com/talks/ua-pycon-2012.pdf
Flask Extension
Concept
- Flask Testing
http://pythonhosted.org/Flask-Testing/ ES unit test example : https://github.com/pfig/flask-elasticsearch
Reference
Offical Website
Tutorial