Cosmographer commited on
Commit
8d3ce78
·
verified ·
1 Parent(s): f45f61b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -40
app.py CHANGED
@@ -525,49 +525,55 @@ def transform_cols(df: pd.DataFrame, cols: List[str], method="log"):
525
  # -----------------------------
526
 
527
  def update_column_lists(df):
528
- """Update all column dropdowns and checkboxes based on current dataframe"""
529
- if df is None or df.empty:
530
- return (
531
- gr.CheckboxGroup.update(choices=[]), # available_cols
532
- gr.Dropdown.update(choices=[]), # rename_col_old
533
- gr.Dropdown.update(choices=[]), # dtype_col
534
- gr.CheckboxGroup.update(choices=[]), # num_impute_cols
535
- gr.CheckboxGroup.update(choices=[]), # cat_impute_cols
536
- gr.Dropdown.update(choices=[]), # replace_col
537
- gr.Dropdown.update(choices=[]), # split_col
538
- gr.Dropdown.update(choices=[]), # date_col_selected
539
- gr.Dropdown.update(choices=[]), # pivot_index
540
- gr.CheckboxGroup.update(choices=[]), # text_ops_cols
541
- gr.Dropdown.update(choices=[]), # groupby_cols
542
- gr.Dropdown.update(choices=[]), # agg_columns
543
- gr.Dropdown.update(choices=[]), # pivot_columns
544
- gr.Dropdown.update(choices=[]), # pivot_values
545
- gr.Dropdown.update(choices=[]), # join_key
546
- )
547
-
548
- cols = df.columns.tolist()
549
- numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
550
- categorical_cols = df.select_dtypes(include=["object", "category"]).columns.tolist()
551
- datetime_cols = df.select_dtypes(include=["datetime64"]).columns.tolist()
552
-
 
 
 
 
 
553
  return (
554
- gr.CheckboxGroup.update(choices=cols), # available_cols
555
- gr.Dropdown.update(choices=cols), # rename_col_old
556
- gr.Dropdown.update(choices=cols), # dtype_col
557
- gr.CheckboxGroup.update(choices=numeric_cols), # num_impute_cols
558
- gr.CheckboxGroup.update(choices=categorical_cols), # cat_impute_cols
559
- gr.Dropdown.update(choices=cols), # replace_col
560
- gr.Dropdown.update(choices=categorical_cols), # split_col
561
- gr.Dropdown.update(choices=cols), # date_col_selected
562
- gr.Dropdown.update(choices=cols), # pivot_index
563
- gr.CheckboxGroup.update(choices=categorical_cols), # text_ops_cols
564
- gr.Dropdown.update(choices=cols), # groupby_cols
565
- gr.Dropdown.update(choices=numeric_cols), # agg_columns
566
- gr.Dropdown.update(choices=categorical_cols), # pivot_columns
567
- gr.Dropdown.update(choices=numeric_cols), # pivot_values
568
- gr.Dropdown.update(choices=cols), # join_key
569
  )
570
 
 
571
  def apply_column_operations(df, selected_cols, rename_mapping, dtype_conversions):
572
  """Apply column selection, renaming, and type conversions"""
573
  df = df.copy()
 
525
  # -----------------------------
526
 
527
  def update_column_lists(df):
528
+ """
529
+ Return a tuple of gr.update(...) values that correspond exactly to the
530
+ prepare_widget_outputs order:
531
+ [available_cols, rename_col_old, dtype_col, num_impute_cols,
532
+ cat_impute_cols, replace_col, split_col, date_col_selected, pivot_index,
533
+ text_ops_cols, groupby_cols, agg_columns, pivot_columns, pivot_values, join_key]
534
+ """
535
+ # safe column list getter - ensure strings, flatten MultiIndex if present
536
+ def get_columns_safe(df):
537
+ if df is None:
538
+ return []
539
+ cols = list(df.columns)
540
+ # handle MultiIndex columns
541
+ if hasattr(df.columns, "levels") and hasattr(df.columns, "names"):
542
+ try:
543
+ cols = ['_'.join(map(str, c)) if isinstance(c, (list, tuple)) else str(c) for c in cols]
544
+ except Exception:
545
+ cols = [str(c) for c in cols]
546
+ else:
547
+ cols = [str(c) for c in cols]
548
+ return cols
549
+
550
+ cols = get_columns_safe(df)
551
+ single_value = cols[0] if cols else None
552
+ multi_value = cols if cols else []
553
+
554
+ # Choose reasonable defaults for UI elements:
555
+ # - For components that expect a list/multiselect: value=multi_value
556
+ # - For single-select dropdowns: value=single_value
557
+ # NOTE: adjust ordering if your outputs wiring order differs.
558
  return (
559
+ gr.update(choices=cols, value=multi_value), # available_cols (CheckboxGroup / Multiselect)
560
+ gr.update(choices=cols, value=single_value), # rename_col_old (Dropdown)
561
+ gr.update(choices=cols, value=single_value), # dtype_col (Dropdown for selecting column to change dtype)
562
+ gr.update(choices=cols, value=multi_value), # num_impute_cols (CheckboxGroup / Multiselect)
563
+ gr.update(choices=cols, value=multi_value), # cat_impute_cols (CheckboxGroup / Multiselect)
564
+ gr.update(choices=cols, value=multi_value), # replace_col (CheckboxGroup / Multiselect)
565
+ gr.update(choices=cols, value=multi_value), # split_col (CheckboxGroup / Multiselect)
566
+ gr.update(choices=cols, value=single_value), # date_col_selected (Dropdown)
567
+ gr.update(choices=cols, value=single_value), # pivot_index (Dropdown)
568
+ gr.update(choices=cols, value=multi_value), # text_ops_cols (CheckboxGroup / Multiselect)
569
+ gr.update(choices=cols, value=multi_value), # groupby_cols (CheckboxGroup / Multiselect)
570
+ gr.update(choices=cols, value=multi_value), # agg_columns (CheckboxGroup / Multiselect)
571
+ gr.update(choices=cols, value=multi_value), # pivot_columns (CheckboxGroup / Multiselect)
572
+ gr.update(choices=cols, value=multi_value), # pivot_values (CheckboxGroup / Multiselect)
573
+ gr.update(choices=cols, value=single_value) # join_key (Dropdown)
574
  )
575
 
576
+
577
  def apply_column_operations(df, selected_cols, rename_mapping, dtype_conversions):
578
  """Apply column selection, renaming, and type conversions"""
579
  df = df.copy()