hvoss-techfak commited on
Commit
9d4ae64
·
1 Parent(s): c417fc7

added load json button

Browse files
Files changed (1) hide show
  1. app.py +56 -1
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import string
2
  import uuid
3
  import os
@@ -360,6 +361,52 @@ def create_empty_error_outputs(log_message=""):
360
  gr.update(visible=False, interactive=False), # ### ZIP: download_zip
361
  )
362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
363
 
364
  # --- Gradio UI Definition ---
365
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
@@ -381,12 +428,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
381
  )
382
  gr.Markdown(
383
  'Hint: If you have an AMS 3d printer try giving it your entire filament library and then set "pruning_max_colors" under "Autoforge Parameters" in the second tab to your number of AMS slots.'
384
- 'Autoforge will automatically select the best matching colors for your image.'
385
  )
386
  with gr.Row():
387
  load_csv_button = gr.UploadButton(
388
  "Load Filaments CSV", file_types=[".csv"]
389
  )
 
 
 
390
  save_csv_button = gr.Button("Save Current Filaments to CSV")
391
  filament_table = gr.DataFrame(
392
  value=ensure_required_cols(
@@ -570,6 +620,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
570
  inputs=[load_csv_button],
571
  outputs=[filament_table],
572
  )
 
 
 
 
 
573
  save_csv_button.click(
574
  save_filaments_to_file_for_download,
575
  inputs=[filament_df_state],
 
1
+ import json
2
  import string
3
  import uuid
4
  import os
 
361
  gr.update(visible=False, interactive=False), # ### ZIP: download_zip
362
  )
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:
374
+ return current_script_df.rename(
375
+ columns={" Name": "Name", " TD": "TD", " Color": "Color (Hex)"}
376
+ )
377
+ return initial_df.copy().rename(
378
+ columns={" Name": "Name", " TD": "TD", " Color": "Color (Hex)"}
379
+ )
380
+
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"]
393
+ if not all(col in df_loaded.columns for col in expected_cols):
394
+ gr.Error(
395
+ f"JSON must contain keys/columns: {', '.join(expected_cols)}. "
396
+ f"Found: {df_loaded.columns.tolist()}"
397
+ )
398
+ return filament_table.value # keep the table unchanged
399
+
400
+ filament_df_state.value = df_loaded.copy()
401
+
402
+ return df_loaded.rename(
403
+ columns={" Name": "Name", " TD": "TD", " Color": "Color (Hex)"}
404
+ )
405
+
406
+ except Exception as e:
407
+ gr.Error(f"Error loading JSON: {e}")
408
+ return filament_table.value # keep current table on failure
409
+
410
 
411
  # --- Gradio UI Definition ---
412
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
428
  )
429
  gr.Markdown(
430
  'Hint: If you have an AMS 3d printer try giving it your entire filament library and then set "pruning_max_colors" under "Autoforge Parameters" in the second tab to your number of AMS slots.'
431
+ ' Autoforge will automatically select the best matching colors for your image.'
432
  )
433
  with gr.Row():
434
  load_csv_button = gr.UploadButton(
435
  "Load Filaments CSV", file_types=[".csv"]
436
  )
437
+ load_json_button = gr.UploadButton( # NEW
438
+ "Load Filaments JSON", file_types=[".json"]
439
+ )
440
  save_csv_button = gr.Button("Save Current Filaments to CSV")
441
  filament_table = gr.DataFrame(
442
  value=ensure_required_cols(
 
620
  inputs=[load_csv_button],
621
  outputs=[filament_table],
622
  )
623
+ load_json_button.upload(
624
+ load_filaments_from_json_upload,
625
+ inputs=[load_json_button],
626
+ outputs=[filament_table],
627
+ )
628
  save_csv_button.click(
629
  save_filaments_to_file_for_download,
630
  inputs=[filament_df_state],