Rectangle 27 0

Object Oriented Python with Flask Server?


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")

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

Note
Rectangle 27 0

Object Oriented Python with Flask Server?


If you want to use classes you can also consider pluggable views

Note