class_docstr: FlaskHelper is a class for common Flask utilities used in dashboards.
class_docstr_tok: ["FlaskHelper","is","a","class","for","common","Flask","utilities","used","in","dashboards","."]
code: def __init__(self, ip=None, port=None, with_credentials=False):
      # The name passed to Flask needs to be unique per instance.
      self.app = Flask(uuid.uuid4().hex)
  
      self.port = port
      self.ip = ip
      self.with_credentials = with_credentials
      # dictionary to store arbitrary state for use by consuming classes
      self.shared_state = {}
      if self.ip is None:
          self.ip = "localhost"
      if self.port is None:
          # Try 100 different ports
          available = False
          for port in range(8704, 8994):
              available = FlaskHelper._is_local_port_available(
                  self.ip, port, raise_error=False
              )
              if available:
                  self.port = port
                  break
  
          if not available:
              error_message = """Ports 8704 to 8993 not available.
                      Please specify an open port for use via the 'port'
                      parameter"""
              raise RuntimeError(error_message.format(port))
      else:
          FlaskHelper._is_local_port_available(
              self.ip, self.port, raise_error=True
          )
      self.env = build_environment(self)
      if hasattr(self.env, "nbvm_origins"):
          self.socketio = SocketIO(
              self.app, cors_allowed_origins=self.env.nbvm_origins
          )
      else:
          self.socketio = SocketIO(self.app)
      if self.env.base_url is None:
          return
      # Sleep for 1 second in order to prevent random errors while
      # socket is still closing
      time.sleep(1)
      self._thread = threading.Thread(target=self.run, daemon=True)
      self._thread.start()
  
      # Closes server on program exit, including freeing all sockets
      def closeserver():
          self.stop()
  
      atexit.register(closeserver)
  
file_path: responsible-ai-toolbox/rai_core_flask/rai_core_flask/flask_helper.py
imports: import atexit
  import logging
  import socket
  import threading
  import time
  import uuid
  import warnings
  from flask import Flask
  from flask_socketio import SocketIO
  from gevent.pywsgi import WSGIServer
  from .environment_detector import build_environment
  from .environments.credentialed_vm_environment import CREDENTIALED_VM
  from .environments.public_vm_environment import PUBLIC_VM
label_desc: Use image node_id 1 to create a new FlaskHelper object from inherited base classes: object with example: obj = FlaskHelper(ip, port, with_credentials)