Skip to content
Advertisement

Object Oriented Python with Flask Server?

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 where I’m stuck:

JavaScript

When I run getSomeData() outside of Flask, it works fine. But, when I run this with Flask, I get 500 internal server error. There’s no magic here, and Flask has no idea that it’s supposed to initialize a MyServer object. How can I feed an instance of MyServer to the app.run() command?

I could admit defeat and put globalData into a database instead. But, is there an other way?

Advertisement

Answer

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

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement