class_docstr: WSGI server for notebook environments.
class_docstr_tok: ["WSGI","server","for","notebook","environments","."]
docstr: Starts a server in a thread using the WSGI application provided.
  
  Will wait until the thread has started calling with an already serving
  application will simple return.
docstr_tok: ["Starts","a","server","in","a","thread","using","the","WSGI","application","provided",".","Will","wait","until","the","thread","has","started","calling","with","an","already","serving","application","will","simple","return","."]
code: def serve(self):
      """Starts a server in a thread using the WSGI application provided.
  
      Will wait until the thread has started calling with an already serving
      application will simple return.
      """
      if self._server_thread is not None:
          return
      if self._port is None:
          self._port = portpicker.pick_unused_port()
      started = threading.Event()
      self._stopped = threading.Event()
      self._stopping = threading.Event()
  
      def build_server(started, stopped, stopping):
          """Closure to build the server function to be passed to the thread.
  
          Args:
            started: Threading event to notify when started.
            stopped: Threading event to notify when stopped.
            stopping: Threading event to notify when stopping.
          Returns:
            A function that function that takes a port and WSGI app and notifies
              about its status via the threading events provided.
          """
  
          def server(port, wsgi_app):
              """Serve a WSGI application until stopped.
  
              Args:
                port: Port number to serve on.
                wsgi_app: WSGI application to serve.
              """
              try:
                  httpd = wsgiref.simple_server.make_server(
                      self._host, port, wsgi_app
                  )
              except socket.error:
                  # Try IPv6
                  httpd = wsgiref.simple_server.make_server(
                      self._host,
                      port,
                      wsgi_app,
                      server_class=WsgiServerIpv6,
                  )
              started.set()
              httpd.timeout = 30
              while not stopping.is_set():
                  httpd.handle_request()
              stopped.set()
  
          return server
  
      server = build_server(started, self._stopped, self._stopping)
      server_thread = threading.Thread(
          target=server, args=(self._port, self._app)
      )
      self._server_thread = server_thread
  
      server_thread.start()
      started.wait()
  
imports: import socket
  import threading
  from typing import Optional
  from wsgiref import validate
  import wsgiref.simple_server
  from absl import logging
  import portpicker
  import termcolor
  from werkzeug import serving
label_desc: Use image node_id 5 for calling the NotebookWsgiServer obj's underlying member method code with example usage: obj.serve() and returns: server