Using flask inside class

前端 未结 3 1601
攒了一身酷
攒了一身酷 2020-12-08 09:36

I have application with many threads. One of them is flask, which is used to implement (axillary) API. It used with low load and never exposed to the Internet, so build-in f

3条回答
  •  旧时难觅i
    2020-12-08 10:18

    so I just came across the library Flask-Classful

    which was really simple comparatively
    To create a simple web app inside a class is this

    from flask import Flask
    from flask_classful import FlaskView
    
    app = Flask(__name__)
    
    class TestView(FlaskView):
    
        def index(self):
        # http://localhost:5000/
            return "

    This is my indexpage

    " TestView.register(app,route_base = '/') if __name__ == '__main__': app.run(debug=True)

    Handling multiple route and dynamic route is also simple

    class TestView(FlaskView):
    
        def index(self):
        # http://localhost:5000/
            return "

    This is my indexpage

    " def secondpage(self): # http://localhost:5000/secondpage return "

    This is my second

    " def thirdpage(self,name): # dynamic route # http://localhost:5000/thirdpage/sometext return "

    This is my third page
    welcome"+name+"

    " TestView.register(app,route_base = '/')

    Adding own route name with a different method that is also possible

    from flask_classful import FlaskView,route
    
    class TestView(FlaskView):
    
        def index(self):
        # http://localhost:5000/
            return "

    This is my indexpage

    " @route('/diffrentname') def bsicname(self): # customized route # http://localhost:5000/diffrentname return "

    This is my coustom route

    " TestView.register(app,route_base = '/')

    This gives the potential to create separate class and handlers for a separate dependent and independent process and just import them as a package to run on the main file or wrapper file

    from package import Classname
    Classname.register(app,route_base = '/')
    

    which is really simple and object-oriented

提交回复
热议问题