code: def test_show(unittest):
      from dtale.app import show, get_instance, instances
      import dtale.views as views
      import dtale.global_state as global_state
  
      global_state.clear_store()
      instances()
      test_data = pd.DataFrame([dict(a=1, b=2)])
      with ExitStack() as stack:
          mock_run = stack.enter_context(
              mock.patch("dtale.app.DtaleFlask.run", mock.Mock())
          )
          mock_find_free_port = stack.enter_context(
              mock.patch(
                  "dtale.app.find_free_port",
                  mock.Mock(return_value=9999),
              )
          )
          stack.enter_context(
              mock.patch(
                  "socket.gethostname",
                  mock.Mock(return_value="localhost"),
              )
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.is_up", mock.Mock(return_value=False)
              )
          )
          mock_requests = stack.enter_context(
              mock.patch("requests.get", mock.Mock())
          )
          instance = show(
              data=test_data,
              subprocess=False,
              name="foo",
              ignore_duplicate=True,
          )
          assert "http://localhost:9999" == instance._url
          assert (
              "http://localhost:9999/dtale/main/1"
              == instance.build_main_url()
          )
          assert (
              "http://localhost:9999/dtale/main/foo"
              == instance.main_url()
          )
          mock_run.assert_called_once()
          mock_find_free_port.assert_called_once()
  
          pdt.assert_frame_equal(instance.data, test_data)
          tmp = test_data.copy()
          tmp["biz"] = 2.5
          instance.data = tmp
          unittest.assertEqual(
              global_state.get_dtypes(instance._data_id),
              views.build_dtypes_state(tmp),
              "should update app data/dtypes",
          )
          # duplicate name
          assert (
              show(
                  data=test_data,
                  subprocess=False,
                  name="foo",
                  ignore_duplicate=True,
              )
              is None
          )
  
          instance2 = get_instance(instance._data_id)
          assert instance2._url == instance._url
          instance2 = get_instance("foo")
          assert instance2._url == instance._url
          pdt.assert_frame_equal(instance2.data, tmp)
  
          instances()
  
          assert (
              get_instance(20) is None
          )  # should return None for invalid data ids
  
          instance.kill()
          mock_requests.assert_called_once()
          assert (
              mock_requests.call_args[0][0]
              == "http://localhost:9999/shutdown"
          )
  
          # invalid name characters
          assert (
              show(
                  data=test_data,
                  subprocess=False,
                  name="It's Here",
                  ignore_duplicate=True,
              )
              is None
          )
          instance3 = show(
              data=test_data,
              subprocess=False,
              name="Its Here",
              ignore_duplicate=True,
          )
          assert (
              instance3.build_main_url()
              == "http://localhost:9999/dtale/main/2"
          )
          assert (
              instance3.main_url()
              == "http://localhost:9999/dtale/main/its_here"
          )
          pdt.assert_frame_equal(instance3.data, test_data)
  
          instance3 = get_instance("its_here")
          assert instance3._url == instance._url
          pdt.assert_frame_equal(instance3.data, test_data)
          instance3.cleanup()
  
          assert (
              "Its Here" not in global_state._default_store._data_names
          )
  
      with ExitStack() as stack:
          mock_run = stack.enter_context(
              mock.patch("dtale.app.DtaleFlask.run", mock.Mock())
          )
          mock_find_free_port = stack.enter_context(
              mock.patch(
                  "dtale.app.find_free_port",
                  mock.Mock(return_value=9999),
              )
          )
          stack.enter_context(
              mock.patch(
                  "socket.gethostname",
                  mock.Mock(return_value="localhost"),
              )
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.is_up", mock.Mock(return_value=False)
              )
          )
          mock_data_loader = mock.Mock(return_value=test_data)
          instance = show(
              data_loader=mock_data_loader,
              subprocess=False,
              port=9999,
              force=True,
              debug=True,
              ignore_duplicate=True,
              ssl_context="adhoc",
          )
          assert "https://localhost:9999" == instance._url
          mock_run.assert_called_once()
          mock_find_free_port.assert_not_called()
          mock_data_loader.assert_called_once()
          _, kwargs = mock_run.call_args
  
          assert "9999" in instance._url
  
      # RangeIndex test
      test_data = pd.DataFrame([1, 2, 3])
      with ExitStack() as stack:
          stack.enter_context(
              mock.patch("dtale.app.DtaleFlask", MockDtaleFlask)
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.find_free_port",
                  mock.Mock(return_value=9999),
              )
          )
          stack.enter_context(
              mock.patch(
                  "socket.gethostname",
                  mock.Mock(return_value="localhost"),
              )
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.is_up", mock.Mock(return_value=False)
              )
          )
          stack.enter_context(
              mock.patch("dtale.app.logger", mock.Mock())
          )
          instance = show(
              data=test_data,
              subprocess=False,
              name="foo2",
              ignore_duplicate=True,
          )
          assert np.array_equal(
              instance.data["0"].values, test_data[0].values
          )
  
      with ExitStack() as stack:
          stack.enter_context(
              mock.patch("dtale.app.DtaleFlask", MockDtaleFlask)
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.find_free_port",
                  mock.Mock(return_value=9999),
              )
          )
          stack.enter_context(
              mock.patch(
                  "socket.gethostname",
                  mock.Mock(return_value="localhost"),
              )
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.is_up", mock.Mock(return_value=False)
              )
          )
          stack.enter_context(
              mock.patch("dtale.app.logger", mock.Mock())
          )
          stack.enter_context(
              mock.patch(
                  "dtale.views.in_ipython_frontend",
                  mock.Mock(return_value=False),
              )
          )
  
          get_calls = {"ct": 0}
          getter = namedtuple("get", "ok")
  
          def mock_requests_get(url, verify=True):
              if url.endswith("/health"):
                  is_ok = get_calls["ct"] > 0
                  get_calls["ct"] += 1
                  return getter(is_ok)
              return getter(True)
  
          stack.enter_context(
              mock.patch("requests.get", mock_requests_get)
          )
          mock_display = stack.enter_context(
              mock.patch("IPython.display.display", mock.Mock())
          )
          mock_iframe = stack.enter_context(
              mock.patch("IPython.display.IFrame", mock.Mock())
          )
          instance = show(
              data=test_data,
              subprocess=True,
              name="foo1",
              notebook=True,
              ignore_duplicate=True,
          )
          mock_display.assert_called_once()
          mock_iframe.assert_called_once()
          assert mock_iframe.call_args[0][
              0
          ] == "http://localhost:9999/dtale/iframe/{}".format(
              instance._data_id
          )
  
          assert type(instance.__str__()).__name__ == "str"
          assert type(instance.__repr__()).__name__ == "str"
  
      class MockDtaleFlaskRunTest(Flask):
          def __init__(
              self,
              import_name,
              reaper_on=True,
              url=None,
              app_root=None,
              *args,
              **kwargs,
          ):
              kwargs.pop("instance_relative_config", None)
              kwargs.pop("static_url_path", None)
              super(MockDtaleFlaskRunTest, self).__init__(
                  import_name, *args, **kwargs
              )
  
          def run(self, *args, **kwargs):
              assert self.jinja_env.auto_reload
              assert self.config["TEMPLATES_AUTO_RELOAD"]
  
      with mock.patch("dtale.app.DtaleFlask", MockDtaleFlaskRunTest):
          show(
              data=test_data,
              subprocess=False,
              port=9999,
              debug=True,
              ignore_duplicate=True,
          )
  
      with mock.patch(
          "dtale.app._thread.start_new_thread", mock.Mock()
      ) as mock_thread:
          show(data=test_data, subprocess=True, ignore_duplicate=True)
          mock_thread.assert_called()
  
      test_data = pd.DataFrame([dict(a=1, b=2)])
  
      with ExitStack() as stack:
          mock_build_app = stack.enter_context(
              mock.patch("dtale.app.build_app", mock.Mock())
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.find_free_port",
                  mock.Mock(return_value=9999),
              )
          )
          stack.enter_context(
              mock.patch(
                  "socket.gethostname",
                  mock.Mock(return_value="localhost"),
              )
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.is_up", mock.Mock(return_value=False)
              )
          )
          stack.enter_context(mock.patch("requests.get", mock.Mock()))
          show(
              data=test_data,
              subprocess=False,
              name="foo3",
              ignore_duplicate=True,
          )
  
          _, kwargs = mock_build_app.call_args
          unittest.assertEqual(
              {
                  "app_root": None,
                  "host": "localhost",
                  "reaper_on": True,
              },
              kwargs,
              "build_app should be called with defaults",
          )
  
      # test adding duplicate column
      with ExitStack() as stack:
          stack.enter_context(
              mock.patch("dtale.app.DtaleFlask", MockDtaleFlask)
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.find_free_port",
                  mock.Mock(return_value=9999),
              )
          )
          stack.enter_context(
              mock.patch(
                  "socket.gethostname",
                  mock.Mock(return_value="localhost"),
              )
          )
          stack.enter_context(
              mock.patch(
                  "dtale.app.is_up", mock.Mock(return_value=False)
              )
          )
          stack.enter_context(mock.patch("requests.get", mock.Mock()))
          instance = show(
              data=pd.DataFrame([dict(a=1, b=2)]),
              subprocess=False,
              name="foo3",
              ignore_duplicate=True,
          )
          with pytest.raises(Exception):
              instance.data = instance.data.rename(columns={"b": "a"})
  
          curr_instance_ct = global_state.size()
          show(
              data=pd.DataFrame([dict(a=1, b=2)]),
              subprocess=False,
              name="foo",
              ignore_duplicate=False,
          )
          assert curr_instance_ct == global_state.size()
  
      # cleanup
      global_state.cleanup()
  
imports: import getpass
  from collections import namedtuple
  from flask import Flask
  import mock
  import numpy
  import os
  import pandas
  import pytest
  from tests import ExitStack, pdt
label_desc: Use image node_id 3 for calling a global function with example usage: test_show(unittest) and returns: getter, getter