code: def lgbm_plotting_callback():
      """LightGBM callback with metrics plotting widget from CatBoost"""
  
      plotter = None
  
      def _init(env):
          train_metrics = []
          test_metrics = []
  
          for item in env.evaluation_result_list:
              assert (
                  len(item) == 4
              ), "Plotting was run in not suppored mode"
              data_name, eval_name = item[:2]
  
              if "train" in data_name.lower():
                  train_metrics.append(eval_name)
              elif (
                  "valid" in data_name.lower()
                  or "test" in data_name.lower()
              ):
                  test_metrics.append(eval_name)
              else:
                  raise Exception(
                      "Unexpected sample name during evaluation"
                  )
  
          nonlocal plotter
          plotter = MetricsPlotter(
              train_metrics,
              test_metrics,
              env.end_iteration - env.begin_iteration,
          )
  
          display(plotter._widget)
  
      def _callback(env):
          if plotter is None:
              _init(env)
  
          metrics = {"train": {}, "test": {}}
  
          for item in env.evaluation_result_list:
              data_name, eval_name, result = item[:3]
  
              if "train" in data_name.lower():
                  metrics["train"][eval_name] = result
              elif (
                  "valid" in data_name.lower()
                  or "test" in data_name.lower()
              ):
                  metrics["test"][eval_name] = result
              else:
                  raise Exception(
                      "Unexpected sample name during evaluation"
                  )
  
          plotter.log(
              env.iteration - env.begin_iteration,
              train=True,
              metrics=metrics["train"],
          )
          plotter.log(
              env.iteration - env.begin_iteration,
              train=False,
              metrics=metrics["test"],
          )
  
      _callback.order = 20
      return _callback
  
imports: from IPython.display import display
  from .metrics_plotter import MetricsPlotter
label_desc: Use image node_id 1 for calling a global function with example usage: lgbm_plotting_callback() and returns: _callback