On Tuesday, March 19, 2019 at 11:57:35 AM UTC-7, Maurice Waka wrote:
>
> I have a code that works OK except when there is no data in the database.
>
> [...]
> gauge.add('Awake', [{'value':[i.awake for i in data][-1], 'max_value':
> 24}])
> gauge.add('Light Sleep', [{'value':[i.light_sleep for i in data][-1],
> 'max_value': 24}])
> gauge.add('Deep Sleep', [{'value':[i.deep_sleep for i in data][-1],
> 'max_value': 24}])
> return gauge.render()
>
> [...]
File "/home/mau/web2py/applications/Hesty/controllers/e_default.py"
<http://127.0.0.1:8000/admin/default/edit/Hestque_Wellness/controllers/e_default.py>
, line 649, in sleep_chart
> gauge.add('Awake', [{'value':[i.awake for i in data][-1], 'max_value': 24
> }])
> IndexError: list index out of range
>
>
> Expectation:I want to have a default figure in the database. I have tried
> having "default=0" but the int only appears in the form text area. So the
> error persists.
> Is there a way to have a default figure in the database or a way to catch
> this errors and let the new user to input int figure?
>
The simplistic way is to wrap the lines I left in the quoted material:
if len(data):
gauge.add('Awake', [{'value':[i.awake for i in data][-1], 'max_value':
24}])
gauge.add('Light Sleep', [{'value':[i.light_sleep for i in data][-1],
'max_value': 24}])
gauge.add('Deep Sleep', [{'value':[i.deep_sleep for i in data][-1],
'max_value': 24}])
else:
gauge.add('Unknown', [{'value':[0], 'max_value': 24}])
A better person than I might put the if into the comprehension, but I tend
not to get that right.
/dps
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.