text: #!/usr/bin/env python3
  
  import os, sys
  from PyQt5.QtWidgets import QWidget,QGridLayout
  from PyQt5.QtCore import QRectF
  from PyQt5 import QtGui,QtCore
  from prompt_toolkit.key_binding.bindings.named_commands import self_insert
  
  
  class BarWidget(QWidget):
      TEXT_WIDTH = 40
      RECT_Y = 10
      RECT_HEIGHT = 10
  
      def __init__(self, value, baseValue, mode, width=None, scale=1):
          super().__init__()
          self._val = value
          self._baseVal = baseValue
          self._width = width
          self._mode = mode
          self._scale = scale
  
          if self._val is None or self._baseVal is None:
              self.setFixedWidth(0)
              return
  
          if self._width is None:
              self._width = self.getWidgetWidth()
  
          self.setFixedWidth(self._width)
          self.initUI()
  
      def getWidgetWidth(self):
          if self._val is None:
              return 1
  
          if self._mode == 'absolute':
              return self._val*self._scale + BarWidget.TEXT_WIDTH
          else:
              return (abs(self._val-self._baseVal) * self._scale + BarWidget.TEXT_WIDTH)* 2
  
      def initUI(self):
          lay = QGridLayout()
  
      def paintEvent(self, e):
          if self._val is None or self._baseVal is None:
              return
          qp = QtGui.QPainter()
          qp.begin(self)
          self.drawRectangle(qp)
          self.drawText(qp)
          qp.end()
  
      def drawRectangle(self, qp):
          color = QtGui.QColor()
          if self._mode == "absolute":
              if self._val > self._baseVal:
                  color.setNamedColor('red')
              else:
                  color.setNamedColor('green')
              qp.setPen(color)
              qp.setBrush(color)
              qp.drawRect(0, BarWidget.RECT_Y, self._val * self._scale, BarWidget.RECT_HEIGHT)
          else:
              center = self._width/2
              diff = self._val - self._baseVal
              rectWidth = abs(self._val - self._baseVal) * self._scale
  
              # draw vertical line in the center
              color.setNamedColor('black')
              qp.setPen(color)
              qp.setBrush(color)
              qp.drawRect(center-1, 0, 1, 30)
              if diff > 0:
                  color.setNamedColor('red')
                  qp.setPen(color)
                  qp.setBrush(color)
                  qp.drawRect(center, BarWidget.RECT_Y, rectWidth, BarWidget.RECT_HEIGHT)
              else:
                  color.setNamedColor('green')
                  qp.setPen(color)
                  qp.setBrush(color)
                  qp.drawRect(center-rectWidth-1, BarWidget.RECT_Y, rectWidth, BarWidget.RECT_HEIGHT)
  
      def drawText(self, qp):
          qp.setPen(QtGui.QColor('black'))
          qp.setFont(QtGui.QFont('Decorative', 8))
          if self._mode == "absolute":
              rec = QRectF(self._val * self._scale, BarWidget.RECT_Y, BarWidget.TEXT_WIDTH, BarWidget.RECT_HEIGHT)
              qp.drawText(rec, QtCore.Qt.AlignCenter, "{0:.2f}".format(self._val))
          else:
              center = self._width / 2
              rectWidth = abs(self._val - self._baseVal) * self._scale
              diff = self._val - self._baseVal
              if diff > 0:
                  rec = QRectF(center+rectWidth, BarWidget.RECT_Y, BarWidget.TEXT_WIDTH, BarWidget.RECT_HEIGHT)
              else:
                  rec = QRectF(center-rectWidth-1-BarWidget.TEXT_WIDTH, BarWidget.RECT_Y, BarWidget.TEXT_WIDTH, BarWidget.RECT_HEIGHT)
              qp.drawText(rec, QtCore.Qt.AlignCenter, "{0:.2f}".format(self._val - self._baseVal))
  