Detect if in development or deployment


Is there a way my Python app can detect whether it is being run in the context of development vs. deployment?

Hi @latinlens !
You could check if the website ends with for deployments, or for development.
You can use url_for(request.endpoint) if you are using Flask, then try to remove the domain and see if it can be removed, maybe try:

  print(url_for(request.endpoint).removesuffix('') # Try to see if `` can be removed, which means it is deployed.
  print('App is deployed')
except Exception: 
  print('App is in development')

url_for(request.endpoint) is taken from:

1 Like

It’s so a conditional statement can change its contents based on the URL:

1 Like

Excellent, thanks for your suggestions! url_for(request.endpoint) just returned ‘/’ for some reason, so I looked around a bit and found this attribute ‘url_root’ that seems to do the job:

def add_to_log(text):
  in_development = lambda : request.url_root.endswith('')

  if in_development():
    if 'log' not in db:
      db['log'] = []
1 Like

Well, that wouldn’t work for a route other than /, so you might want to use:

in_development = '' in request.url_root
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.