Coder Social home page Coder Social logo

flask-example's Introduction

Flask example

Using Flask to build a Restful API Server with Swagger document.

Integration with Flask-restplus, Flask-Cors, Flask-Testing, Flask-SQLalchemy,and Flask-OAuth extensions.

Extension:

Installation

Install with pip:

$ pip install -r requirements.txt

Flask Application Structure

.
|──────app/
| |────__init__.py
| |────api/
| | |────__init__.py
| | |────cve/
| | |────user/
| | |────oauth/
| |──────config.Development.cfg
| |──────config.Production.cfg
| |──────config.Testing.cfg
| |────dao/
| |────model/
| |────oauth/
| |────util/
|──────run.py
|──────tests/

Flask Configuration

Example

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

Configuring From Files

Example Usage

app = Flask(__name__ )
app.config.from_pyfile('config.Development.cfg')

cfg example


##Flask settings
DEBUG = True  # True/False
TESTING = False

##SWAGGER settings
SWAGGER_DOC_URL = '/api'

....


Builtin Configuration Values

SERVER_NAME: the name and port number of the server.

JSON_SORT_KEYS : By default Flask will serialize JSON objects in a way that the keys are ordered.

OAuth Setup

add your client_id and client_secret into config file.

ESDAO Setup

add your ES host and ES port into config file

Run Flask

Run flask for develop

$ python webapp/run.py

In flask, Default port is 5000

Swagger document page: http://127.0.0.1:5000/api

Run flask for production

** Run with gunicorn **

In webapp/

$ gunicorn -w 4 -b 127.0.0.1:5000 run:app

  • -w : number of worker
  • -b : Socket to bind

Run with Docker

$ docker build -t flask-example .

$ docker run -p 5000:5000 --name flask-example flask-example 
 

In image building, the webapp folder will also add into the image

Unittest

$ nosetests webapp/ --with-cov --cover-html --cover-package=app
  • --with-cov : test with coverage
  • --cover-html: coverage report in html format

Reference

Offical Website

Tutorial

Wiki Page

Changelog

  • Version 2.3 : add dockerfile
  • Version 2.2 : add ESDAO module
  • Version 2.1 : add OAuth extension: FLASK-OAuth, and google oauth example
  • Version 2.0 : add SQL ORM extension: FLASK-SQLAlchemy
  • Version 1.1 : update nosetest
  • Version 1.0 : basic flask-example with Flask-Restplus, Flask-Tesintg

flask-example's People

Contributors

tsungtwu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.