Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ classifiers =
[options]
install_requires =
tabulate
tqdm
package_dir =
= src
packages = find:
Expand Down
5 changes: 3 additions & 2 deletions src/lazy_table/_lazy_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
54 changes: 34 additions & 20 deletions src/lazy_table/artists/_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
5 changes: 0 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ deps =
pylint
numpy
tabulate
tqdm
commands =
pylint src/lazy_table
pylint examples
Expand All @@ -16,7 +15,6 @@ basepython = python3.6
deps =
numpy
tabulate
tqdm
commands =
python examples/euler_vdp.py --n_rows 8 --show_progress

Expand All @@ -25,7 +23,6 @@ basepython = python3.7
deps =
numpy
tabulate
tqdm
commands =
python examples/euler_vdp.py --n_rows 8 --show_progress

Expand All @@ -34,7 +31,6 @@ basepython = python3.8
deps =
numpy
tabulate
tqdm
commands =
python examples/euler_vdp.py --n_rows 8 --show_progress

Expand All @@ -43,6 +39,5 @@ basepython = python3.9
deps =
numpy
tabulate
tqdm
commands =
python examples/euler_vdp.py --n_rows 8 --show_progress