Indicate "In Debug" mode

What I’m trying to do:

Is there a way to indicate whether or not the app is running in “Debug” mode? I would like to write a function to print info only if in “Debug” mode.

What I’ve tried and what’s not working:

Code Sample:

# code snippet

Clone link:
share a copy of your app

If by “debug mode”, you mean “in the ide” vs. “in a regular browser tab”, see Call Context.

Here is the solution I came up with.

On the client side, at app start-up I run this functions:

_debug_state = False
_ip_address = None

""" RUN ON LOAD """
def set_debug_state():
  global _debug_state
  client_ip = client_ip_address()
  _debug_state = False if client_ip else True
  anvil.server.call('set_debug_state', _debug_state)
  
  return _debug_state

def client_ip_address():
  global _ip_address
  
  if not _ip_address:
    _ip_address = anvil.server.context.client.ip
  
  return _ip_address

Then on the server side, I have this function:

@anvil.server.callable
def set_debug_state(state: bool):
  anvil.server.session['debug_state'] = state

Whenever I want to print something when in “debug” mode, but not during normal use, I use these functions.

client:

def d_print(*args):
  if not anvil.server.context.client.ip:
    if len(args) < 2:
      args = args[0]
    print("DEBUG >>>", args)

server:

@anvil.server.callable
def d_print(*args):
  if anvil.server.session['debug_state']:
    if len(args) < 2:
      args = args[0]
    print("DEBUG >>>", args)

Many pythonistas use @decorators for this purpose, here a link to some more info:

https://realpython.com/primer-on-python-decorators/

1 Like