controller classes in flask

  • Last Update :
  • Techknowledgy :

For grouping actions I try split controllers (views with flask notation, see https://stackoverflow.com/a/20999302/880326) by logic, but you can have more important criterias for this. Easy example:

views /
   __init__.py
about.py
home.py
products.py
user /
   __init__.py
dashboard.py
product_1.py
product_2.py
product_3.py
settings.py

Suggestion : 2

provide of prepare authentication and request validation other class based view library is not provided of validation ,other class based view library is not provided of validation, View statistics for this project via Libraries.io, or by using our public dataset on Google BigQuery ,Follow the RESTful design

Installation

$ pip install flask - rest - controller

Usage

from flask
import Flask
from flask_rest_controller
import Controller, set_routing

app = Flask(__name__)
app.secret_key = '͏?Š|?    èg<Î|ÇæãhŽÖúÈi|î°'

class JsonController(Controller):
   schema = {
      'GET': {
         'type': 'array',
         'properties': {
            'id': {
               'type': 'string'
            }
         }
      },
      'POST': {
         'type': 'object',
         'properties': {
            'result': {
               'type': 'string'
            },
            'code': {
               'type': 'integer'
            }
         }
      }
   }

def get(self):
   return self.render_json(["Hello World"])

def post(self):
   return self.render_json({
      'result': "ok",
      'code': 200
   })

ROUTING = [
   ("/", "app.JsonController", "json_controller"),
]

set_routing(app, ROUTING)

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

Just save it as app.py and try

$ python app.py

You should see a post request result, try this command

$ curl--request POST http: //127.0.0.1:5000

Suggestion : 3

A Minimal Application,Read more on Application Errors.,A minimal Flask application looks something like this:,it enables the debug mode on the Flask application.

from flask
import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
   return 'Hello, World!'
$
export FLASK_APP = hello.py
$ flask run
   *
   Running on http: //127.0.0.1:5000/
C: \path\ to\ app > set FLASK_APP = hello.py
PS C: \path\ to\ app > $env: FLASK_APP = "hello.py"
$
export FLASK_APP = hello.py
$ python - m flask run *
   Running on http: //127.0.0.1:5000/
$ flask run--host = 0.0 .0 .0

Suggestion : 4

Last Updated On: August 30, 2021

Then use it in our endpoints:

# Controllers API

# This doesn 't need authentication
@app.route("/ping")
@cross_origin(headers = ['Content-Type', 'Authorization'])
def ping():
   return "All good. You don't need to be authenticated to call this"

# This does need authentication
@app.route("/secured/ping")
@cross_origin(headers = ['Content-Type', 'Authorization'])
@requires_auth
def secured_ping():
   return "All good. You only get this message if you're authenticated"