Skip to content

Commit b4148eb

Browse files
authored
Merge pull request #24 from makermelissa/main
Handle empty json and non-data labels better
2 parents 056ad75 + c68ae4e commit b4148eb

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

adafruit_magtag/magtag.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def __init__(
112112
self._text_font = []
113113
self._text_line_spacing = []
114114
self._text_anchor_point = []
115+
self._text_is_data = []
115116

116117
gc.collect()
117118

@@ -127,6 +128,7 @@ def add_text(
127128
text_scale=1,
128129
line_spacing=1.25,
129130
text_anchor_point=(0, 0.5),
131+
is_data=True,
130132
):
131133
"""
132134
Add text labels with settings
@@ -143,6 +145,10 @@ def add_text(
143145
length. Defaults to 0.
144146
:param text_transform: A function that will be called on the text before display
145147
:param int text_scale: The factor to scale the default size of the text by
148+
:param float line_spacing: The factor to space the lines apart
149+
:param (float, float) text_anchor_point: Values between 0 and 1 to indicate where the text
150+
position is relative to the label
151+
:param bool is_data: If True, fetch will attempt to update the label
146152
"""
147153
if text_font is terminalio.FONT:
148154
self._text_font.append(text_font)
@@ -174,6 +180,7 @@ def add_text(
174180
self._text_scale.append(text_scale)
175181
self._text_line_spacing.append(line_spacing)
176182
self._text_anchor_point.append(text_anchor_point)
183+
self._text_is_data.append(bool(is_data))
177184

178185
# pylint: enable=too-many-arguments
179186

@@ -358,17 +365,21 @@ def fetch(self, refresh_url=None, timeout=10):
358365

359366
# fill out all the text blocks
360367
if self._text:
368+
value_index = 0 # In case values and text is not the same
361369
for i in range(len(self._text)):
370+
if not self._text_is_data[i]:
371+
continue
362372
string = None
363373
if self._text_transform[i]:
364374
func = self._text_transform[i]
365-
string = func(values[i])
375+
string = func(values[value_index])
366376
else:
367377
try:
368-
string = "{:,d}".format(int(values[i]))
378+
string = "{:,d}".format(int(values[value_index]))
369379
except (TypeError, ValueError):
370-
string = values[i] # ok its a string
380+
string = values[value_index] # ok its a string
371381
self.set_text(string, index=i, auto_refresh=False)
382+
value_index += 1
372383
self.refresh()
373384
if len(values) == 1:
374385
return values[0]

adafruit_magtag/network.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ def json_traverse(json, path):
137137
"The json_path parameter should be enclosed in a list or tuple."
138138
)
139139
for x in path:
140-
value = value[x]
140+
try:
141+
value = value[x]
142+
except TypeError as error:
143+
raise ValueError(
144+
"The specified json_path was not found in the results."
145+
) from error
141146
gc.collect()
142147
return value
143148

@@ -512,7 +517,7 @@ def fetch_data(
512517
raise
513518

514519
# extract desired text/values from json
515-
if json_out and json_path:
520+
if json_out is not None and json_path:
516521
for path in json_path:
517522
try:
518523
values.append(self.json_traverse(json_out, path))

0 commit comments

Comments
 (0)