code: def calc_flex(self, data, max_width, cols, preformatted=None):
          
          if preformatted is None:
              preformatted = []
          colstats = []
          for i in cols:
              lengths = [len(xx) for x in data
                         for xx in x[i].text().splitlines()]
              if self.headers:
                  lengths.append(len(self.headers[i]))
              lengths.append(self.width_normalize(self.colspec[i]['minwidth']))
              counts = collections.Counter(lengths)
              colstats.append({
                  "column": i,
                  "preformatted": i in preformatted,
                  "counts": counts,
                  "offt": max(lengths),
                  "chop_mass": 0,
                  "chop_count": 0,
                  "total_mass": sum(a * b for a, b in counts.items())
              })
          self.adjust_widths(max_width, colstats)
          required = sum(x['offt'] for x in colstats)
          justify = self.table.justify if self.table.justify is not None else \
              self.justify_default
          if required < max_width and justify:
              # Fill remaining space proportionately.
              remaining = max_width
              for x in colstats:
                  x['offt'] = int((x['offt'] / required) * max_width)
                  remaining -= x['offt']
              if remaining:
                  dist = self._uniform_dist(len(cols), remaining)
                  for adj, col in zip(dist, colstats):
                      col['offt'] += adj
          return [(x['column'], x['offt']) for x in colstats]
docs: Scan data returning the best width for each column given the
          max_width constraint.  If some columns will overflow we calculate the
          best concession widths.