Python Forum

Full Version: Flask Create global response function to be called from every where in the web app
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
new to flask , i have web app that containes few parts
for example:
    utils/ 
    -- utils.py
    db/ 
    -- oracle.py 
    main.py 
My Question is how can i make global_response function/handler that can be called from : utils.py,oracle.py,main.py without threading problem
for example:
in utils.py

 clas Utils():
      def A(self):
         return global_response("error","a")
oracle.py

 clas Utils():
          def ORA(self):
             return global_response("error ora","b")
main.py
def some_fun(self):
        return global_response("error main","c")
and here is the response function which was in main.py but now needs to be called from any where :

 def global_response(error_msg, title,*options):
        res = {"status": error_msg, "title": title}
    
        for option in options:
            res.update(option)
       
     return json.dumps(res)
I'd like to suggest that you run through Miguel Grinberg's tutorial here: https://blog.miguelgrinberg.com/post/the...ello-world
Free and well done.
Thanks for the response , mybe i was misundestod , i do know how to create responses ,
my question is what is the right way to create response handler that i can use from all the web app .
like singletone that contains the global_response fucntion ,
or pass the object to the method that needs to response ...