4

I want to use Flask to create a web API for my application, but have some problems to make my flask app aware of my other objects.

  • I want to use Flask in order to be able to interact with my application through http requests. So the whole flask application in my case is just an external API, and relies on a core application.
  • Let's imagine that my flask application will have to perform database calls.
  • To manage database calls in my application, I use a single object that connects to the db ans implements some kind of Queue.
  • That means my core application running in the background has a reference to my db object in order to make db calls.
  • This is done by giving a reference to my queue object to this core application.
  • Now I want to be able to perform actions on the db using a flask application too.

What is the correct way to pass a reference to this Queue object to my Flask application?

If I define all my objects at module level, I have no way to interact with them afterwards, do I?

All the example of Flask applications use Flask as the core of their system and define everything in their app on module level. How do I make Flask just a part of my app?

Laurel
  • 5,965
  • 14
  • 31
  • 57
jlengrand
  • 12,152
  • 14
  • 57
  • 87

1 Answers1

1

I'm not sure what you mean by

If I define all my objects at module level, I have no way to interact with them afterwards, do I?

But no, you don't have to define your objects at the module level - that's true of your Flask instance, blueprints and any object which you provide. For example you can create an AppBuilder class that makes and configures Flask instances.

For some interactions context locals are a very handy tool as well.

If you can clarify the issue I'll try to expand my answer.

jd.
  • 10,678
  • 3
  • 46
  • 55