pyscript: registered_triggers is not defined in new app

Using the below code for a new app and no matter what I try I can’t get it to not throw an error on registered_triggers

registered_triggers = []

# start the app
@time_trigger('startup')
def personTrackerStartup():
    loadApp('location', makePersonTracker)


def makePersonTracker(config):
    global registered_triggers
    personID = config['person']
    personName = personID.split('.')[1]

    @task_unique(f'{personName}_tracker')
    @state_trigger(f'{personID} != {personID}.old')
    def tracker(value=None):
        return

        
    # register to global scope
    registered_triggers.append(tracker) # registered_triggers.append(tracker): NameError: global name 'registered_triggers' is not defined


def loadApp(app_name, factory):
    if 'apps' not in pyscript.config:
        return
    
    if app_name not in pyscript.config['apps']:
        return

    for app in pyscript.config['apps'][app_name]:
        factory(app)

I followed the Wiki for the structure but there’s a good chance I’m doing something wrong.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@dlashua - thanks for figuring out a workaround.

This is indeed a bug, and I can recreate it after seeing a complete example (thanks for sending). The callback from load_app.py to makePersonTracker() doesn’t run that callback inside that function’s global context, so the global variable is missing when it runs.

Reopening until it’s fixed.

I get the same error as you with these exact files. However, if I move loadApp into person_tracker.py I do not get this error.

I believe the reason for this is that registered_triggers is defined inside of person_tracker.py. However, the factory method (makePersonTracker in this case) is being called from load_app.py where registered_triggers is not defined. Adding registered_triggers = [] to the top of load_app.py fixed it for me.