Object Oriented Python with Flask Server?

后端 未结 5 1888
悲哀的现实
悲哀的现实 2021-02-04 02:18

I\'m using Flask to expose some data-crunching code as a web service. I\'d like to have some class variables that my Flask functions can access.

Let me walk you through

相关标签:
5条回答
  • 2021-02-04 02:40

    a bit late but heres a quick implementation that i use to register routes at init time

    from flask import Flask,request,render_template
    from functools import partial
    
    
    registered_routes = {}
    def register_route(route=None):
        #simple decorator for class based views
        def inner(fn):
            registered_routes[route] = fn
            return fn
        return inner
    
    class MyServer(Flask):
        def __init__(self,*args,**kwargs):
            if not args:
                kwargs.setdefault('import_name',__name__)
            Flask.__init__(self,*args ,**kwargs)
            # register the routes from the decorator
            for route,fn in registered_routes.items():
                partial_fn = partial(fn,self)
                partial_fn.__name__ = fn.__name__
                self.route(route)(partial_fn)
    
    
        @register_route("/")
        def index(self):
            return render_template("my_template.html")
    
    if __name__ == "__main__":
        MyServer(template_folder=os.path.dirname(__file__)).run(debug=True)
    
    0 讨论(0)
  • 2021-02-04 02:42

    You can create an instance of MyServer just outside the scope of your endpoints and access its attributes. This worked for me:

    class MyServer:
        def __init__(self):
            self.globalData = "hello"
    
    from flask import Flask
    app = Flask(__name__)
    
    my_server = MyServer()
    
    @app.route("/getSomeData")
    def getSomeData():
        return my_server.globalData
    
    if __name__ == "__main__":
        app.run(host="0.0.0.0")
    
    0 讨论(0)
  • 2021-02-04 02:46

    I know this is a late reply, but I came across this question while facing a similar issue. I found flask-classful really good. You inherit your class from FlaskView and register the Flask app with your MyServer class

    http://flask-classful.teracy.org/#

    In this case, with flask-classful, your code would look like this:

    from flask import Flask
    from flask_classful import FlaskView, route
    
    app = Flask(__name__)
    
    class MyServer(FlaskView):
      def __init__(self):
        globalData = json.load(filename)
    
      @route('/getSomeData')
      def getSomeData():
        return random.choice(globalData) #select some random data to return
    
    
    MyServer.register(app, base_route="/")
    
    
    if __name__ == "__main__":
      app.run(host='0.0.0.0')
    
    0 讨论(0)
  • 2021-02-04 02:55

    if you wish to approach MyServer class as a resource
    I believe that flask_restful can help you:

    from flask import Flask
    from flask_restful import Resource, Api
    import json
    import numpy as np
    
    app = Flask(__name__)
    api = Api(app)
    
    class MyServer(Resource):
        def __init__(self):
            self.globalData = json.load(filename)
    
        def get(self):
            return np.random.choice(self.globalData)
    
    api.add_resource(MyServer, '/')
    
    if __name__ == '__main__':
        app.run()
    
    0 讨论(0)
  • 2021-02-04 03:05

    The least-coupled solution is to apply the routes at runtime (instead of at load time):

    def init_app(flask_app, database_interface, filesystem_interface):
        server = MyServer(database_interface, filesystem_interface)
        flask_app.route('get_data', methods=['GET'])(server.get_data)
    

    This is very testable--just invoke init_app() in your test code with the mocked/faked dependencies (database_interface and filesystem_interface) and a flask app that has been configured for testing (app.config["TESTING"]=True or something like that) and you're all-set to write tests that cover your entire application (including the flask routing).

    The only downside is this isn't very "Flasky" (or so I've been told); the Flask idiom is to use @app.route(), which is applied at load time and is necessarily tightly coupled because dependencies are hard-coded into the implementation instead of injected into some constructor or factory method (and thus complicated to test).

    0 讨论(0)
提交回复
热议问题