Detect if in development or deployment

Question:

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 replit.app for deployments, or replit.dev 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:

try:
  print(url_for(request.endpoint).removesuffix('replit.app') # Try to see if `replit.app` 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('replit.dev/')

  if in_development():
    print(f':{text}')
  else:
    if 'log' not in db:
      db['log'] = []
    db['log'].append(text)
  
  return
1 Like

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

in_development = 'replit.dev/' in request.url_root
1 Like

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