#๐Ÿ”’ Use env value to determine whether to call external API or local json

9 messages ยท Page 1 of 1 (latest)

frank gale
#

Hi pythonics,

I am having a Flask project setup with Docker (Compose). In my docker-compose.yml file I want to set an environment variable to determine if a dummy file should be used or an external API should be called. Initially, at my given Flask endpoint I directly check whether the environment variable is set to True like so;

services:
    flask:
        environment:
           - USE_DUMMY=True
def operation_advice():
    if os.getenv('USE_DUMMY', False):
       # return dummy
    
    # call API and return it contents here

Since docker-compose environment variables are strings apparently, the given if statement is always true. I see I can do constructions like this but I have my doubts about it honestly: https://www.baeldung.com/ops/booleans-docker-compose-yml#:~:text=Docker Compose always expects environment,receive environment variables as text.&text=Above%2C Docker Compose treats 'true,processes it without any errors.

I hope someone can give me a suggestion in how I can tackle this problem.

bitter sequoiaBOT
#

@frank gale

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

buoyant solar
#

I don't recommend doing this and instead dummy should be used for local instead.
However, you can use os.getenv("USE_DUMMY", "False").lower() == "true"

#

"False" doesn't have to be "False", but any content that is not "true" when convert to lower case.

frank gale
#

I use dummy for local purposes, based on the boolean i want to determin if local or external should be called.

#

Your code suggestion is what I will try out now.

#

Your suggestion works, thanks for that. If you have any more comments I am eager to hear them. Otherwise I will close this issue.

bitter sequoiaBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.