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.
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.
https://pythonhosted.org/Flask-Classy/
I will roughly describe it with reference to the above page.
pip install flask-classy
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.
flaskview
For class names that inherit from, as suffixview
Can be used.
helloview
The corresponding url is/hello/
It will be.
You can use it, so you don't have to use suffix.
flaskview
Inheritedhello
Even if you create a class, the corresponding url is/hello/
It will be.
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 |
Will be in the order of
The URL is determined by the class name and function name, but here are some measures to avoid it.
There are two ways
route_base
as a class variableclass HelloView(FlaskView):
route_base='/'
route_base
to register as an argumentHelloView.register(app, route_base='/')
Both of the above overwrite / hello
, which should be defined from the class name, and make it accessible with `/`
.
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 /`
.
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