Raffael-Kultyshev commited on
Commit
0ec8c92
·
1 Parent(s): 58c1274

Lazy load plot data to avoid module load errors

Browse files
Files changed (1) hide show
  1. app.py +29 -20
app.py CHANGED
@@ -125,17 +125,25 @@ def prepare_data():
125
  'right_rot': right_rot
126
  }
127
 
128
- try:
129
- plot_data = prepare_data()
130
- except Exception as e:
131
- print(f"Error preparing plot data: {e}")
132
- plot_data = {
133
- 'times': [],
134
- 'left_pos': {'x': [], 'y': [], 'z': []},
135
- 'left_rot': {'yaw': [], 'pitch': [], 'roll': []},
136
- 'right_pos': {'x': [], 'y': [], 'z': []},
137
- 'right_rot': {'yaw': [], 'pitch': [], 'roll': []}
138
- }
 
 
 
 
 
 
 
 
139
 
140
  METRIC_LABELS = {
141
  "x_cm": "X (cm)",
@@ -255,40 +263,41 @@ CUSTOM_CSS = """
255
  def build_plot_fig(metric: str, hand: str = "left") -> go.Figure:
256
  """Build Plotly figure for a metric."""
257
  try:
258
- times = plot_data.get('times', [])
 
259
 
260
  if hand == "left":
261
  if "cm" in metric:
262
  key = metric.replace('_cm', '')
263
- data = plot_data.get('left_pos', {}).get(key, [])
264
  else:
265
  key = metric.replace('_deg', '')
266
- data = plot_data.get('left_rot', {}).get(key, [])
267
  name = "Left Hand"
268
  else:
269
  if "cm" in metric:
270
  key = metric.replace('_cm', '')
271
- data = plot_data.get('right_pos', {}).get(key, [])
272
  else:
273
  key = metric.replace('_deg', '')
274
- data = plot_data.get('right_rot', {}).get(key, [])
275
  name = "Right Hand"
276
 
277
- if not times or not data:
278
  # Return empty plot
279
  times = [0]
280
- data = [0]
281
  except Exception as e:
282
  print(f"Error in build_plot_fig: {e}")
283
  times = [0]
284
- data = [0]
285
  name = "Error"
286
 
287
  fig = go.Figure()
288
  fig.add_trace(
289
  go.Scatter(
290
  x=times,
291
- y=data,
292
  mode="lines",
293
  name=name,
294
  line=dict(color="#67e8f9", width=2)
 
125
  'right_rot': right_rot
126
  }
127
 
128
+ # Lazy load plot data
129
+ plot_data = None
130
+
131
+ def get_plot_data():
132
+ """Lazy load plot data."""
133
+ global plot_data
134
+ if plot_data is None:
135
+ try:
136
+ plot_data = prepare_data()
137
+ except Exception as e:
138
+ print(f"Error preparing plot data: {e}")
139
+ plot_data = {
140
+ 'times': [],
141
+ 'left_pos': {'x': [], 'y': [], 'z': []},
142
+ 'left_rot': {'yaw': [], 'pitch': [], 'roll': []},
143
+ 'right_pos': {'x': [], 'y': [], 'z': []},
144
+ 'right_rot': {'yaw': [], 'pitch': [], 'roll': []}
145
+ }
146
+ return plot_data
147
 
148
  METRIC_LABELS = {
149
  "x_cm": "X (cm)",
 
263
  def build_plot_fig(metric: str, hand: str = "left") -> go.Figure:
264
  """Build Plotly figure for a metric."""
265
  try:
266
+ data = get_plot_data()
267
+ times = data.get('times', [])
268
 
269
  if hand == "left":
270
  if "cm" in metric:
271
  key = metric.replace('_cm', '')
272
+ plot_values = data.get('left_pos', {}).get(key, [])
273
  else:
274
  key = metric.replace('_deg', '')
275
+ plot_values = data.get('left_rot', {}).get(key, [])
276
  name = "Left Hand"
277
  else:
278
  if "cm" in metric:
279
  key = metric.replace('_cm', '')
280
+ plot_values = data.get('right_pos', {}).get(key, [])
281
  else:
282
  key = metric.replace('_deg', '')
283
+ plot_values = data.get('right_rot', {}).get(key, [])
284
  name = "Right Hand"
285
 
286
+ if not times or not plot_values:
287
  # Return empty plot
288
  times = [0]
289
+ plot_values = [0]
290
  except Exception as e:
291
  print(f"Error in build_plot_fig: {e}")
292
  times = [0]
293
+ plot_values = [0]
294
  name = "Error"
295
 
296
  fig = go.Figure()
297
  fig.add_trace(
298
  go.Scatter(
299
  x=times,
300
+ y=plot_values,
301
  mode="lines",
302
  name=name,
303
  line=dict(color="#67e8f9", width=2)