My program was running just fine yesterday, but after making slight changes, only changing and reducing the HTML calls, the program only runs from the console. The Notification Center says "unable to load."
Forum Archive
"Unable to Load" in Notification Center
As @JonB would say, "double check line 17. thats where your problem lies."
It's hard to say without seeing your code. It could be that the widget is running out of memory (it gets far less from the system than the main app), or that you've run into a bug. Certain modules/functions are not supported in the widget, but usually this should raise an exception instead of crashing. Again, without seeing your code, it's hard to tell.
Ok thanks, I'll share the code, I realized memory could be an issue. Just to let you know I took out the URLs because it has personal data on it.
import urllib.request,ui,appex
l=0
from objc_util import *
UIDevice = ObjCClass('UIDevice')
device = UIDevice.currentDevice()
device.setBatteryMonitoringEnabled_(True)
battery_percent = device.batteryLevel() * 100
off=True
hum=0
tem=0
#Do not worry about this, this changes my lights but will disable if I'm not at home
def button_tapped(sender):
global l
if sender.name =='+':
l+=10
if sender.name =='-':
l+=-10
with urllib.request.urlopen(' ' + str(l) ) as response:
if l>100:
l=100
if l<0:
l=0
print(str(l))
# Checks if I'm at home, still doesn't work if I take this out
req = urllib.request.Request(' ')
try: urllib.request.urlopen(req)
except urllib.error.URLError :
tem = ' -- '
with urllib.request.urlopen(' ') as response:
html = response.read()
info = html
def main():
label = ui.View(frame=(0, 0, 320, 64))
# TEMPERATURE
t = ui.Label(frame=(1, 0, 100, 0), flex='wh',text_color='white', font=('HelveticaNeue-Light', 15), alignment=ui.ALIGN_LEFT, text = 'Temperature:' + str(info)[3:7] + '°')
label.add_subview(t)
# HUMIDITY
h = ui.Label(frame=(1,0,150,32),flex='wh', text_color='white',font=('HelveticaNeue-Light',15), alignment=ui.ALIGN_LEFT, text = 'Humidity:'+str(info)[11:13] + '%')
label.add_subview(h)
# LIVING ROOM LIGHTS
living_title = ui.Label(frame=(320-135,0,130,-20),flex='wh',text_color='white', font=('Monla',15), alignment=ui.ALIGN_RIGHT, text = 'Living Room Lights')
# GREETING (WIP)
greet='hi'
# LIVING ROOM BUTTONS
plus_btn = ui.Button(name='+', image=ui.Image('iow:ios7_plus_outline_32'), flex='hl', tint_color='#666', action=button_tapped)
plus_btn.frame = (320-64, 0, 64, 64)
minus_btn = ui.Button(name='-', image=ui.Image('iow:ios7_minus_outline_32'), flex='hl', tint_color='#666', action=button_tapped)
minus_btn.frame = (320-64 ,0, -64,64)
if tem!=' -- ':
label.add_subview(minus_btn)
label.add_subview(plus_btn)
label.add_subview(living_title)
else:
# GREETING
greeting = ui.Label(frame=(320-135,0,130,-20),flex='wh',text_color='white', font=('Monla',15), alignment=ui.ALIGN_RIGHT, text = greet)
#label.add_subview(greeting)
# BATTERY
if battery_percent>=85:
img='iow:battery_full_24'
if battery_percent<85 and battery_percent>=50:
img='iow:battery_half_24'
if battery_percent<50 and battery_percent>=30:
img='iow:battery_low_24'
elif battery_percent<30:
img='iow:battery_empty_24'
bat = ui.Button(name='batteryl', image=ui.Image(img),flex='hl',tint_color='#00d500',action=button_tapped)
bat.frame = (320-30,64,32,32)
label.add_subview(bat)
# BACKGROUND COLOR
label.background_color = '#1a1a1a'
# SETS WIDGET
appex.set_widget_view(label)
if __name__ == '__main__':
main()
Not sure why the one comment is huge but that is the code, I know it is not efficient and I haven't cleaned anything up yet. Thank you for the help.
Your code work well for me in the Today widget after a few minor changes:
* Create a URL variable that is still valid if a number is appended to it
* Use that URL in 3 places to replace ' '.
import urllib.request,ui,appex
l=0
from objc_util import *
UIDevice = ObjCClass('UIDevice')
device = UIDevice.currentDevice()
device.setBatteryMonitoringEnabled_(True)
battery_percent = device.batteryLevel() * 100
off=True
hum=0
tem=0
url = 'http://www.apple.com/#' # Added url definition
#Do not worry about this, this changes my lights but will disable if I'm not at home
def button_tapped(sender):
global l
if sender.name =='+':
l+=10
if sender.name =='-':
l+=-10
with urllib.request.urlopen(url + str(l) ) as response: # Added use of url
if l>100:
l=100
if l<0:
l=0
print(str(l))
# Checks if I'm at home, still doesn't work if I take this out
req = urllib.request.Request(url) # Added use of url
try: urllib.request.urlopen(req)
except urllib.error.URLError :
tem = ' -- '
with urllib.request.urlopen(url) as response: # Added use of url
html = response.read()
info = html
def main():
label = ui.View(frame=(0, 0, 320, 64))
# TEMPERATURE
t = ui.Label(frame=(1, 0, 100, 0), flex='wh',text_color='white', font=('HelveticaNeue-Light', 15), alignment=ui.ALIGN_LEFT, text = 'Temperature:' + str(info)[3:7] + '°')
label.add_subview(t)
# HUMIDITY
h = ui.Label(frame=(1,0,150,32),flex='wh', text_color='white',font=('HelveticaNeue-Light',15), alignment=ui.ALIGN_LEFT, text = 'Humidity:'+str(info)[11:13] + '%')
label.add_subview(h)
# LIVING ROOM LIGHTS
living_title = ui.Label(frame=(320-135,0,130,-20),flex='wh',text_color='white', font=('Monla',15), alignment=ui.ALIGN_RIGHT, text = 'Living Room Lights')
# GREETING (WIP)
greet='hi'
# LIVING ROOM BUTTONS
plus_btn = ui.Button(name='+', image=ui.Image('iow:ios7_plus_outline_32'), flex='hl', tint_color='#666', action=button_tapped)
plus_btn.frame = (320-64, 0, 64, 64)
minus_btn = ui.Button(name='-', image=ui.Image('iow:ios7_minus_outline_32'), flex='hl', tint_color='#666', action=button_tapped)
minus_btn.frame = (320-64 ,0, -64,64)
if tem!=' -- ':
label.add_subview(minus_btn)
label.add_subview(plus_btn)
label.add_subview(living_title)
else:
# GREETING
greeting = ui.Label(frame=(320-135,0,130,-20),flex='wh',text_color='white', font=('Monla',15), alignment=ui.ALIGN_RIGHT, text = greet)
#label.add_subview(greeting)
# BATTERY
if battery_percent>=85:
img='iow:battery_full_24'
if battery_percent<85 and battery_percent>=50:
img='iow:battery_half_24'
if battery_percent<50 and battery_percent>=30:
img='iow:battery_low_24'
elif battery_percent<30:
img='iow:battery_empty_24'
bat = ui.Button(name='batteryl', image=ui.Image(img),flex='hl',tint_color='#00d500',action=button_tapped)
bat.frame = (320-30,64,32,32)
label.add_subview(bat)
# BACKGROUND COLOR
label.background_color = '#1a1a1a'
# SETS WIDGET
appex.set_widget_view(label)
if __name__ == '__main__':
main()
Other ideas...
import appex
import requests
import ui
from objc_util import ObjCClass
l = 0
device = ObjCClass('UIDevice').currentDevice()
device.setBatteryMonitoringEnabled_(True)
battery_percent = device.batteryLevel() * 100
device.setBatteryMonitoringEnabled_(False)
off = True
hum = 0
tem = 0
url = 'http://www.apple.com/#' # Added url definition
# Do not worry about this, this changes my lights but will disable if I'm not
# at home
def button_tapped(sender):
global l
l += {'+': 10, '-': -10}.get(sender.name, 0)
l = min(max(l, 0), 100)
print('{}: {}'.format(l, requests.get(url + str(l)).text))
response = requests.get(url)
tem = response.text if response.status_code == 200 else ' -- '
info = html = response.text
def main():
label = ui.View(frame=(0, 0, 320, 64))
helv_15 = ('HelveticaNeue-Light', 15)
# TEMPERATURE
label.add_subview(ui.Label(frame=(1, 0, 100, 0), flex='wh', font=helv_15,
text_color='white', alignment=ui.ALIGN_LEFT,
text='Temperature:' + str(info)[3:7] + '°'))
# HUMIDITY
label.add_subview(ui.Label(frame=(1, 0, 150, 32), flex='wh', font=helv_15,
text_color='white', alignment=ui.ALIGN_LEFT,
text='Humidity:' + str(info)[11:13] + '%'))
# LIVING ROOM LIGHTS
monla_15 = ('Monla', 15)
living_title = ui.Label(frame=(320 - 135, 0, 130, -20), flex='wh',
font=monla_15, alignment=ui.ALIGN_RIGHT,
text_color='white', text='Living Room Lights')
# GREETING (WIP)
greet = 'hi'
# LIVING ROOM BUTTONS
plus_btn = ui.Button(name='+', image=ui.Image('iow:ios7_plus_outline_32'),
flex='hl', tint_color='#666', action=button_tapped)
plus_btn.frame = (320 - 64, 0, 64, 64)
minus_btn = ui.Button(name='-', image=ui.Image('iow:ios7_minus_outline_32'),
flex='hl', tint_color='#666', action=button_tapped)
minus_btn.frame = (320 - 64, 0, -64, 64)
if tem != ' -- ':
label.add_subview(minus_btn)
label.add_subview(plus_btn)
label.add_subview(living_title)
else:
# GREETING
label.add_subview(ui.Label(frame=(320 - 135, 0, 130, -20), flex='wh',
text_color='white', font=monla_15,
alignment=ui.ALIGN_RIGHT, text=greet))
# BATTERY
if battery_percent >= 85:
img = 'full'
elif battery_percent >= 50:
img = 'half'
elif battery_percent >= 30:
img = 'low'
else:
img = 'empty'
bat = ui.Button(name='batteryl', flex='hl', tint_color='#00d500',
image=ui.Image('iow:battery_{}_24'.format(img)),
action=button_tapped)
bat.frame = (320 - 30, 64, 32, 32)
label.add_subview(bat)
# BACKGROUND COLOR
label.background_color = '#1a1a1a'
# SETS WIDGET
appex.set_widget_view(label)
if __name__ == '__main__':
main()