Basic usage of flask-classy

What to do in this article

Not covered in this article

The following articles are not written by using flask-classy on a regular basis, but are written while studying as needed. If you find any mistakes, please submit an edit request or point them out in the comments. Thank you.

What is flask-classy

Flask is a lightweight web framework available in python.

When using flask, usually you create an application using decorators and functions, but when you try to create a large web application across multiple files, use a guy called Blueprint. You need to play with Gonyo Gonyo.

Flask-Classy can introduce a unified method of routing without the hassle of messing around with it.

Official page

https://pythonhosted.org/Flask-Classy/

I will roughly describe it with reference to the above page.

Installation

pip install flask-classy

Minimum usage

  1. Create an instance of the Flask class as you normally would use flask
  2. Create a class that inherits FlaskView.
  3. ** Use the class method `` `register``` of the created class with an instance of Flask class as an argument **
  4. Launch the application

from flask import Flask
from flask.ext.classy import FlaskView

app = Flask(__name__)

class HelloView(FlaskView):
    def index(self):
        return "hello world"

HelloView.register(app)

if __name__ == '__main__':
    app.run()

If you launch the above file from the command line, you should be able to access `localhost: 5000 / hello``` and see the string `hello world```.

Please note that the process of registering 3 is skipped in the following samples.

Routing and naming conventions

Class naming convention

flaskviewFor class names that inherit from, as suffixviewCan be used.

helloviewThe corresponding url is/hello/It will be.

You can use it, so you don't have to use suffix.

flaskviewInheritedhelloEven if you create a class, the corresponding url is/hello/It will be.

Method naming convention

Although it has appeared in the minimum usage, the following methods in the class that inherits FlaskView have predetermined routing.

Method name routing http method
index /Class definition/ get
get /Class definition/ get
post /Class definition/ post
put /Class definition/ put
patch /Class definition/ patch
delete /Class definition/ delete

In addition to Python's usual naming convention, the following method names are used in FlaskView and should be avoided.

Method names other than the above can be accessed as follows.

URL http method
/Class definition/Method name get

There are also some other special method names defined.

Method name function
before_request Executed before executing the method defined in the class
before_Target method name Executed before executing the target method defined in the class
after_request Executed after executing the method defined in the class
after_Target method name Executed after executing the target method defined in the class

life cycle

  1. @app.before_request
  2. class.before_request
  3. class.before_ target method
  4. class. Target method
  5. class.after_ target method
  6. class.after_request
  7. @app.after_request

Will be in the order of

Routing override

The URL is determined by the class name and function name, but here are some measures to avoid it.

Override routing from class name

There are two ways

Declare route_base as a class variable

class HelloView(FlaskView):
    route_base='/'

Pass route_base to register as an argument

HelloView.register(app, route_base='/')

Both of the above overwrite / hello, which should be defined from the class name, and make it accessible with `/`.

Override method routing

This is one way

from flask.ext.classy import FlaskView, route

class HelloView(FlaskView):
	@route('/hoge/')
    def index(self):
        return 'hello'

As mentioned above, for the method of the class that inherits FlaskView, use `route``` defined in `flask.ext.classy``` You can overwrite the routing by decorating it.

In the case of the above sample, you can access it if the url is `/ hello / hoge /`.

HTTP method

flask.ext.classsy route can take exactly the same arguments as flask route.



 ** As with normal flask, you can only get GET access just by defining methods and routing. ** **

 If you want to add an http method other than GET, pass a list with the name `` `methods``` in the argument of `` `@ route```.

 For the time being, it seems that any character string can be entered in the list element (verification required), but I think that it is okay but not profitable.
 Let's put in a normal HTTP method.


 Below is a sample.


```python3
from flask.ext.classy import FlaskView, route

class HelloView(FlaskView):
	@route('/index/', methods=['GET','POST'])
    def index(self):
        return 'hello'

As mentioned above, by adding methods, you can access with the corresponding HTTP method.

Recommended Posts

Basic usage of flask-classy
Basic usage of Jinja2
Basic usage of SQLAlchemy
Super basic usage of pytest
Basic usage of Pandas Summary
Basic usage of Python f-string
Basic usage of Btrfs on Arch Linux
Basic operation of pandas
Summary of pyenv usage
Usage of Python locals ()
Basic operation of Pandas
Basic knowledge of Python
Basic processing of librosa
[Introduction to Python] Basic usage of lambda expressions
Make a note of the list of basic Pandas usage
(Beginner) Basic usage of Datastore on Google App Engine
[Introduction to Python] Basic usage of the library matplotlib
Convenient usage summary of Flask
Basic flow of anomaly detection
virtualenv Basic command usage memo
(Minimal) usage of django logger
XPath Basics (1) -Basic Concept of XPath
Sample usage of Python pickle
One-liner basic graph of HoloViews
[Python] Correct usage of join
Index of certain pandas usage
[2020/06 latest version] Basic usage of python dependency management tool poetry
Summary of basic knowledge of PyPy Part 1
Summary of basic implementation by PyTorch
Easy usage memo of Anaconda (conda)
About the basic type of Go
Installation and easy usage of pytest
Basic grammar of Python3 system (dictionary)
[python] Correct usage of if statement
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
Basic study of OpenCV with Python
[Linux] Review of frequently used basic commands 2
Acquisition of past electricity usage China Electricity Edition
Scraping the usage history of the community cycle
Basic writing of various programming languages (self-memo)
The usage of TensorBoard has changed slightly
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
[Linux] Review of frequently used basic commands
Non-logical operator usage of or in python
Python Basic Course (at the end of 15)
Basic level performance evaluation of programming languages
Basic grammar of Python3 system (included notation)
How to install fabric and basic usage
BESS Development Memo # 01: BESS Installation and Basic Usage