I have a Flask app with multiple routes. I have defined an Utility class with few methods to handle the common functionalities, for eg. getting some properties etc. Now I want to create and instance of the Utility class in a common place to be used by functions in each route. How do I achieve this ?
from flask import Flask, request, jsonify
import mysql.connector as mariadb
app = Flask(__name__)
@app.route('/aaa/bbb/ccc',methods=['POST'])
def func1():
pass
@app.route('/xxx/yyy/zzz',methods=['POST'])
def func2():
pass
@app.route('/abc/dfg/ijk',methods=['POST'])
def func3():
pass
class Utility:
def get_properties():
pass
def get_database_conn():
pass
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5000, debug=True)
Now, I want to do something like -
util_obj = Utility()
in the beginning of the app, so that from any of my routes I can call the utility methods without having to create the object every time.
What is the best way to achieve this ?