code: def notebook(user, run_id, assets_path):
      """Fixture to load a notebook and work with it.
  
      This launches a live server,
      configures a notebook to use it, and enables
      devs to execute arbitrary cells.
      See test_notebooks.py for usage.
      """
      # wandb-related env vars:
      wandb_env = {
          k: v for k, v in os.environ.items() if k.startswith("WANDB")
      }
  
      # lovely-dawn-32 is run id we use for testing
      wandb_env["WANDB_RUN_ID"] = run_id
  
      @contextmanager
      def notebook_loader(
          nb_name: str,
          kernel_name: str = "wandb_python",
          notebook_type: PythonType = "jupyter",
          save_code: bool = True,
          **kwargs: Any,
      ):
          nb_path = assets_path(pathlib.Path("notebooks") / nb_name)
          shutil.copy(
              nb_path,
              os.path.join(os.getcwd(), os.path.basename(nb_path)),
          )
          with open(nb_path) as f:
              nb = nbformat.read(f, as_version=4)
  
          # set up extra env vars and do monkey-patching.
          # in particular, we import and patch wandb.
          # this goes in the first cell of the notebook.
  
          setup_cell = io.StringIO()
  
          # env vars, particularly to point the sdk at the live test server (local_testcontainer):
          if not save_code:
              wandb_env["WANDB_SAVE_CODE"] = "false"
              wandb_env["WANDB_NOTEBOOK_NAME"] = ""
          else:
              wandb_env["WANDB_SAVE_CODE"] = "true"
              wandb_env["WANDB_NOTEBOOK_NAME"] = nb_name
          setup_cell.write("import os\n")
          for k, v in wandb_env.items():
              setup_cell.write(f"os.environ['{k}'] = '{v}'\n")
          # make wandb think we're in a specific type of notebook:
          setup_cell.write(
              "import pytest\n"
              "mp = pytest.MonkeyPatch()\n"
              "import wandb\n"
              f"mp.setattr(wandb.sdk.wandb_settings, '_get_python_type', lambda: '{notebook_type}')"
          )
  
          # inject:
          nb.cells.insert(
              0, nbformat.v4.new_code_cell(setup_cell.getvalue())
          )
  
          client = WandbNotebookClient(nb, kernel_name=kernel_name)
          try:
              with client.setup_kernel(**kwargs):
                  yield client
          finally:
              pass
              # with open(os.path.join(os.getcwd(), "notebook.log"), "w") as f:
              #     f.write(client.all_output_text())
              # wandb.termlog("Find debug logs at: %s" % os.getcwd())
              # wandb.termlog(client.all_output_text())
  
      notebook_loader.base_url = wandb_env.get("WANDB_BASE_URL")
  
      return notebook_loader
  
imports: import io
  import os
  import pathlib
  import shutil
  from contextlib import contextmanager
  from typing import Any, Dict, List
  from unittest.mock import MagicMock, patch
  import nbformat
  import pytest
  import wandb
  import wandb.util
  from nbclient import NotebookClient
  from nbclient.client import CellExecutionError
  from wandb.sdk.lib.ipython import PythonType
label_desc: Use image node_id 4 for calling a global function with example usage: notebook(user, run_id, assets_path) and returns: notebook_loader