diff --git a/README.md b/README.md index ff9a0fa..d7a790e 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ You can also specify an "artist" that determines how the table is rendered. For example, the `ConsoleWithProgress` artist displays a progress bar alongside the table indicating the percentage of rows completed: ```python -n_rows = 10 +n = 10 artist = lt.artists.ConsoleWithProgress() -lt.stream(fib_table(n_rows), headers=['N', 'F_N'], n_rows=n_rows, artist=artist) +lt.stream(fib_table(n), headers=['N', 'F_N'], n_rows=n + 1, artist=artist) ``` diff --git a/setup.cfg b/setup.cfg index 3cceb9e..2e4a9c3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -17,7 +17,6 @@ classifiers = [options] install_requires = tabulate - tqdm package_dir = = src packages = find: diff --git a/src/lazy_table/_lazy_table.py b/src/lazy_table/_lazy_table.py index 544a31e..97f4b62 100644 --- a/src/lazy_table/_lazy_table.py +++ b/src/lazy_table/_lazy_table.py @@ -39,8 +39,9 @@ def stream(table, artist=None, n_rows=None, **kwargs): artist = Console() rows = [] result = tabulate(rows, **kwargs) - artist(result, n_rows) + artist.init(n_rows) + artist.render(result) for row in table: rows.append(row) result = tabulate(rows, **kwargs) - artist(result, n_rows) + artist.render(result) diff --git a/src/lazy_table/artists/_console.py b/src/lazy_table/artists/_console.py index 480d81d..41b9b49 100644 --- a/src/lazy_table/artists/_console.py +++ b/src/lazy_table/artists/_console.py @@ -2,16 +2,15 @@ # pylint: disable=too-few-public-methods +from time import perf_counter import sys -from tqdm import tqdm - def _clear(clear, n_lines, out): if clear: - out.write('\x1b[2J') + out.write("\x1b[2J") else: - out.write('\033[F\033[K' * n_lines) + out.write("\x1b[1K\033[F\033[K" * n_lines) class Console: @@ -26,36 +25,51 @@ class Console: """ def __init__(self, clear=False, out=sys.stdout): self._clear = clear - self._n_lines = 0 self._out = out - def __call__(self, result, _): + def render(self, result): _clear(self._clear, self._n_lines, self._out) - self._n_lines = result.count('\n') + 1 + self._n_lines = result.count("\n") + 1 self._out.write(result) - self._out.write('\n') + self._out.write("\n") self._out.flush() + def init(self, _): + self._n_lines = 0 + class ConsoleWithProgress: """Renders a table to the console along with a progress bar. See docstring of ``Console`` for a list of parameters. """ - def __init__(self, clear=False, out=sys.stdout): + def __init__(self, clear=False, out=sys.stdout, width=32): self._clear = clear - self._n_lines = 0 - self._n_calls = -1 self._out = out - self._tqdm = None + self._width = width + + def _progress(self): + frac = self._n_calls / self._n_rows + n_complete_symbols = int(self._width * frac) + n_incomplete_symbols = self._width - n_complete_symbols + complete_symbols = "█" * n_complete_symbols + incomplete_symbols = " " * n_incomplete_symbols + elapsed = perf_counter() - self._start + n_iters_per_sec = self._n_calls / elapsed + self._out.write(f"|{complete_symbols}{incomplete_symbols}| {self._n_calls}/{self._n_rows}") + self._out.write(f" [{elapsed:.2f}s, {n_iters_per_sec:.2f}it/s]\n") - def __call__(self, result, n_rows): - if self._tqdm is None: - self._tqdm = tqdm(total=n_rows) - else: - self._tqdm.update(1) + def render(self, result): _clear(self._clear, self._n_lines, self._out) + self._n_lines = result.count("\n") + 3 + self._out.write(result) + self._out.write("\n") + self._out.write("\n") + self._progress() self._n_calls += 1 - self._n_lines = result.count('\n') + 3 - self._tqdm.write(result) - self._tqdm.write('\n') + + def init(self, n_rows): + self._n_calls = 0 + self._n_lines = 0 + self._n_rows = n_rows + self._start = perf_counter() diff --git a/tox.ini b/tox.ini index 5b3056a..11f00b7 100644 --- a/tox.ini +++ b/tox.ini @@ -6,7 +6,6 @@ deps = pylint numpy tabulate - tqdm commands = pylint src/lazy_table pylint examples @@ -16,7 +15,6 @@ basepython = python3.6 deps = numpy tabulate - tqdm commands = python examples/euler_vdp.py --n_rows 8 --show_progress @@ -25,7 +23,6 @@ basepython = python3.7 deps = numpy tabulate - tqdm commands = python examples/euler_vdp.py --n_rows 8 --show_progress @@ -34,7 +31,6 @@ basepython = python3.8 deps = numpy tabulate - tqdm commands = python examples/euler_vdp.py --n_rows 8 --show_progress @@ -43,6 +39,5 @@ basepython = python3.9 deps = numpy tabulate - tqdm commands = python examples/euler_vdp.py --n_rows 8 --show_progress