66This example will access the Space Devs astronaut API, display the number of
77humans currently in space and their names and agency on a PyPortal screen.
88"""
9- import time
10- import board
119import gc
1210import json
11+ import time
12+ import board
1313import supervisor
1414from adafruit_pyportal import PyPortal
1515from adafruit_bitmap_font import bitmap_font
6464
6565gc .collect ()
6666
67- def make_count_label (count ):
68- lbl = Label (names_font , text = "{} People in Space" .format (count ))
67+
68+ def make_count_label (num_people ):
69+ lbl = Label (names_font , text = "{} People in Space" .format (num_people ))
6970 lbl .color = count_color
7071 lbl .x = count_position [0 ]
7172 lbl .y = count_position [1 ]
7273 return lbl
7374
75+
7476def fetch_astronauts ():
7577 """Fetch and parse astronaut data, filtering out non-humans."""
7678 gc .collect ()
@@ -79,25 +81,27 @@ def fetch_astronauts():
7981 data = json .loads (raw )
8082 else :
8183 data = raw
82- astronauts = [
84+ result = [
8385 a for a in data ["results" ]
8486 if a .get ("type" , {}).get ("name" ) != "Non-Human"
8587 ]
86- count = len (astronauts )
88+ num_people = len (result )
8789 del data
8890 del raw
8991 gc .collect ()
90- return count , astronauts
92+ return num_people , result
9193
92- def build_name_list (astronauts ):
94+
95+ def build_name_list (astronaut_list ):
9396 """Build display strings from astronaut records."""
9497 result = []
95- for a in astronauts :
98+ for a in astronaut_list :
9699 agency_full = a .get ("agency" , "" )
97100 abbrev = AGENCY_ABBREV .get (agency_full , agency_full [:4 ])
98101 result .append ("%s (%s)" % (a ["name" ], abbrev ))
99102 return result
100103
104+
101105def calculate_columns (names ):
102106 """Calculate column widths and positions based on name lengths.
103107 Col1 names are truncated to fit; col2 start is fixed from col1 width.
@@ -125,55 +129,54 @@ def calculate_columns(names):
125129 max_chars_col1 = col1_width // char_width
126130 max_chars_col2 = col2_width // char_width if col2_names else 0
127131
128- return col2_x , max_chars_col1 , max_chars_col2 , bool (col2_names )
132+ return col2_x , max_chars_col1 , max_chars_col2
133+
129134
130- def display_astronauts (count , names ):
135+ def display_astronauts (num_people , names ):
131136 """Create and append all labels, return list for later cleanup."""
132- labels = []
137+ label_list = []
133138
134139 # Count label
135- lbl = make_count_label (count )
136- pyportal .root_group .append (lbl )
137- labels .append (lbl )
140+ label_list .append (make_count_label (num_people ))
141+ pyportal .root_group .append (label_list [- 1 ])
138142
139- col2_x , max_chars_col1 , max_chars_col2 , has_col2 = (
140- calculate_columns (names )
141- )
143+ col2_x , max_chars_col1 , max_chars_col2 = calculate_columns (names )
142144 y_start = names_position [1 ]
143145
144146 for i , name in enumerate (names ):
145147 in_col2 = i >= max_rows
146148 max_chars = max_chars_col2 if in_col2 else max_chars_col1
147149 if len (name ) > max_chars :
148150 name = name [:max_chars - 1 ] + "~"
149- lbl = Label (names_font , text = name )
150- lbl .color = names_color
151- lbl .x = col2_x if in_col2 else names_position [0 ]
152- lbl .y = (
151+ new_lbl = Label (names_font , text = name )
152+ new_lbl .color = names_color
153+ new_lbl .x = col2_x if in_col2 else names_position [0 ]
154+ new_lbl .y = (
153155 y_start + (i - max_rows ) * font_height
154156 if in_col2
155157 else y_start + i * font_height
156158 )
157- pyportal .root_group .append (lbl )
158- labels .append (lbl )
159+ pyportal .root_group .append (new_lbl )
160+ label_list .append (new_lbl )
161+
162+ return label_list
159163
160- return labels
161164
162- def clear_labels (labels ):
165+ def clear_labels (label_list ):
163166 """Remove all labels from the display group."""
164- for lbl in labels :
167+ for _lbl in label_list :
165168 pyportal .root_group .pop ()
166169
167170
168171# Main loop
169172while True :
170173 try :
171- count , astronauts = fetch_astronauts ()
172- names = build_name_list (astronauts )
173- del astronauts
174+ people_count , astronaut_list = fetch_astronauts ()
175+ name_list = build_name_list (astronaut_list )
176+ del astronaut_list
174177 gc .collect ()
175- print ("People in space:" , count )
176- for n in names :
178+ print ("People in space:" , people_count )
179+ for n in name_list :
177180 print (" " , n )
178181 except HttpError as e :
179182 print ("Rate limited, backing off! -" , e )
@@ -184,8 +187,8 @@ def clear_labels(labels):
184187 time .sleep (30 )
185188 supervisor .reload ()
186189
187- labels = display_astronauts (count , names )
190+ active_labels = display_astronauts (people_count , name_list )
188191 time .sleep (3600 ) # display for 1 hour - data changes infrequently
189- clear_labels (labels )
192+ clear_labels (active_labels )
190193 gc .collect ()
191194 supervisor .reload ()
0 commit comments