Python Forum
Flask Create global response function to be called from every where in the web app - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Flask Create global response function to be called from every where in the web app (/thread-25885.html)



Flask Create global response function to be called from every where in the web app - umen - Apr-14-2020

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)



RE: Flask Create global response function to be called from every where in the web app - Larz60+ - Apr-14-2020

I'd like to suggest that you run through Miguel Grinberg's tutorial here: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
Free and well done.


RE: Flask Create global response function to be called from every where in the web app - umen - Apr-14-2020

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 ...