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
- fixed global context setting on function call; fixes #58 — committed to raman325/pyscript by craigbarratt 4 years ago
@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.pytomakePersonTracker()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.pyI do not get this error.I believe the reason for this is that
registered_triggersis defined inside ofperson_tracker.py. However, the factory method (makePersonTrackerin this case) is being called fromload_app.pywhereregistered_triggersis not defined. Addingregistered_triggers = []to the top ofload_app.pyfixed it for me.