hvoss-techfak commited on
Commit
580e68f
Β·
1 Parent(s): 9d4ae64

fixed weird hueforge csv formatting.

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -363,11 +363,10 @@ def create_empty_error_outputs(log_message=""):
363
 
364
  def load_filaments_from_json_upload(file_obj):
365
  """
366
- Called when the user picks a .json file.
367
- Accepts both the plain Hueforge export and the wrapped
368
- {"Filaments": [...]} variant.
369
  """
370
- # Fall back to whatever is already in the state when nothing selected
371
  if file_obj is None:
372
  current_script_df = filament_df_state.value
373
  if current_script_df is not None and not current_script_df.empty:
@@ -381,12 +380,27 @@ def load_filaments_from_json_upload(file_obj):
381
  try:
382
  with open(file_obj.name, "r", encoding="utf-8") as f:
383
  data = json.load(f)
384
-
385
- # Hueforge sometimes nests the list under the β€œFilaments” key
386
  if isinstance(data, dict) and "Filaments" in data:
387
  data = data["Filaments"]
388
 
389
  df_loaded = pd.DataFrame(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
  df_loaded = ensure_required_cols(df_loaded, in_display_space=False)
391
 
392
  expected_cols = ["Brand", " Name", " TD", " Color"]
 
363
 
364
  def load_filaments_from_json_upload(file_obj):
365
  """
366
+ Called when the user picks a .json file and converts it to the
367
+ script-style DataFrame expected by the rest of the app.
 
368
  """
369
+ # ── early-out when nothing was chosen ──────────────────────────────
370
  if file_obj is None:
371
  current_script_df = filament_df_state.value
372
  if current_script_df is not None and not current_script_df.empty:
 
380
  try:
381
  with open(file_obj.name, "r", encoding="utf-8") as f:
382
  data = json.load(f)
 
 
383
  if isinstance(data, dict) and "Filaments" in data:
384
  data = data["Filaments"]
385
 
386
  df_loaded = pd.DataFrame(data)
387
+
388
+ # strip whitespace around every header first
389
+ df_loaded.columns = [c.strip() for c in df_loaded.columns]
390
+
391
+ # convert Hue-forge β€œnice” headers to the script headers that
392
+ # still carry a leading blank
393
+ rename_map = {
394
+ "Name": " Name",
395
+ "TD": " TD",
396
+ "Color": " Color",
397
+ }
398
+ df_loaded.rename(
399
+ columns={k: v for k, v in rename_map.items() if k in df_loaded.columns},
400
+ inplace=True,
401
+ )
402
+
403
+ # now make sure the usual helpers see exactly the expected headers
404
  df_loaded = ensure_required_cols(df_loaded, in_display_space=False)
405
 
406
  expected_cols = ["Brand", " Name", " TD", " Color"]