<commit_before>from __future__ import print_function

try:
	from IPython.html.widgets import HBox, IntProgress, Latex, FloatProgress
	from IPython.display import display, HTML
	ipython = True
except ImportError:
	ipython = False

class Progress():
	_value = -1
	_iter = None
	__iter__ = lambda self: self
	
	def __init__(self, iterable, length=None, keep=False):
		self._iter = iterable.__iter__()
		self.keep = keep

		if ipython:
			if length is None:
				length = len(iterable)

			self.box = HBox(width="100%", align="center")
			#HTML("""<style> .widget-progress {flex: 2;}</style>""")
			self.bar = IntProgress(width="auto", min=0, max=length, margin='auto')
			self.text = Latex(width="auto", margin='auto')
			l = len(str(length))
			self.textfmt = "[{}/{}]".format("{:%dd}"%l, length)
			self.text.value = self.textfmt.format(self.bar.value)

			self.box.children = (self.bar,self.text)

			display(self.box)
			
	@property
	def value(self):
		return self._value
	
	@value.setter
	def value(self, value):
		self._value = value
		if ipython:
			self.bar.value = self._value
			self.text.value = self.textfmt.format(self.bar.value)
			
	def __next__(self):
		if ipython:
			self.value += 1
			try:
				return self._iter.__next__()
			except StopIteration:
				if not self.keep:
					self.box.close()
				raise StopIteration

class FlProgress():
	_value = -1

	def __init__(self, mn, mx):
		self.box = HBox(width="100%", align="center")
		#HTML("""<style> .widget-progress {flex: 2;}</style>""")
		self.bar = FloatProgress(width="auto", min=mn, max=mx, margin='auto')
		self.text = Latex(width="auto", margin='auto')
		l = max(len(str(mn)),len(str(mx)))
		self.textfmt = "[{} < {} > {}]".format(mn, "{:%dg}"%l, mx)
		self.text.value = self.textfmt.format(self.bar.value)

		self.box.children = (self.bar, self.text)

		display(self.box)

	@property
	def value(self):
		return self._value
	
	@value.setter
	def value(self, value):
		self._value = value
		if ipython:
			self.bar.value = self._value
			self.text.value = self.textfmt.format(self.bar.value)

	def finished(self):
		self.box.close()<commit_msg>adapted to jupyter 4<commit_after>from IPython.display import display
import ipywidgets

try:
	get_ipython()
	ipy = True
except NameError:
	ipy = False

class Progress():
	__iter__ = lambda self: self
	__len__ = lambda self: self.length

	def __init__(self, iterable, length=None, keep=False, style=''):
		self._iter = iterable.__iter__()
		self.keep = keep
		self._value = -1

		if length is None:
			self.length = len(iterable)
		else:
			self.length = length

		if ipy:
			self.box = ipywidgets.HBox(width="100%", align="center")
			self.bar = ipywidgets.IntProgress(min=0, max=self.length, margin="auto", width="auto")
			self.label = ipywidgets.Latex(margin="auto", width="auto")
			self.labelproto = "[{}/{}]".format("{:%dd}"%len(str(self.length)), self.length)
			self.label.value = self.labelproto.format(self.bar.value)

			self.box.children = (self.bar, self.label)

			display(self.box)

	@property
	def value(self):
		return self._value

	@value.setter
	def value(self, value):
		self._value = value
		if ipy:
			self.bar.value = self._value
			self.label.value = self.labelproto.format(self.bar.value)

	def __next__(self):
		self.value += 1
		try:
			return self._iter.__next__()
		except StopIteration:
			if ipy and not self.keep:
				self.box.close()
			raise StopIteration
{'fraction_non_alphanumeric': 0.1578019394651778, 'fraction_numerical': 0.005583308845136644, 'mean_word_length': 3.8767908309455588, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6161053', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1227, 'n_words': 267}