Launch and display a TensorBoard instance as if at the command line.

  Args:
    args_string: Command-line arguments to TensorBoard, to be
      interpreted by `shlex.split`: e.g., "--logdir ./logs --port 0".
      Shell metacharacters are not supported: e.g., "--logdir 2>&1" will
      point the logdir at the literal directory named "2>&1".

def start(args_string):
  """Launch and display a TensorBoard instance as if at the command line.

  Args:
    args_string: Command-line arguments to TensorBoard, to be
      interpreted by `shlex.split`: e.g., "--logdir ./logs --port 0".
      Shell metacharacters are not supported: e.g., "--logdir 2>&1" will
      point the logdir at the literal directory named "2>&1".
  """
  context = _get_context()
  try:
    import IPython
    import IPython.display
  except ImportError:
    IPython = None

  if context == _CONTEXT_NONE:
    handle = None
    print("Launching TensorBoard...")
  else:
    handle = IPython.display.display(
        IPython.display.Pretty("Launching TensorBoard..."),
        display_id=True,
    )

  def print_or_update(message):
    if handle is None:
      print(message)
    else:
      handle.update(IPython.display.Pretty(message))

  parsed_args = shlex.split(args_string, comments=True, posix=True)
  start_result = manager.start(parsed_args)

  if isinstance(start_result, manager.StartLaunched):
    _display(
        port=start_result.info.port,
        print_message=False,
        display_handle=handle,
    )

  elif isinstance(start_result, manager.StartReused):
    template = (
        "Reusing TensorBoard on port {port} (pid {pid}), started {delta} ago. "
        "(Use '!kill {pid}' to kill it.)"
    )
    message = template.format(
        port=start_result.info.port,
        pid=start_result.info.pid,
        delta=_time_delta_from_info(start_result.info),
    )
    print_or_update(message)
    _display(
        port=start_result.info.port,
        print_message=False,
        display_handle=None,
    )

  elif isinstance(start_result, manager.StartFailed):
    def format_stream(name, value):
      if value == "":
        return ""
      elif value is None:
        return "\n<could not read %s>" % name
      else:
        return "\nContents of %s:\n%s" % (name, value.strip())
    message = (
        "ERROR: Failed to launch TensorBoard (exited with %d).%s%s" %
        (
            start_result.exit_code,
            format_stream("stderr", start_result.stderr),
            format_stream("stdout", start_result.stdout),
        )
    )
    print_or_update(message)

  elif isinstance(start_result, manager.StartTimedOut):
    message = (
        "ERROR: Timed out waiting for TensorBoard to start. "
        "It may still be running as pid %d."
        % start_result.pid
    )
    print_or_update(message)

  else:
    raise TypeError(
        "Unexpected result from `manager.start`: %r.\n"
        "This is a TensorBoard bug; please report it."
        % start_result
    )