https://github.com/parsiad/lazy-table
A python-tabulate wrapper for producing tables from generators
https://github.com/parsiad/lazy-table
lazy-evaluation python tables
Last synced: 8 months ago
JSON representation
A python-tabulate wrapper for producing tables from generators
- Host: GitHub
- URL: https://github.com/parsiad/lazy-table
- Owner: parsiad
- License: mit
- Created: 2021-09-25T07:01:30.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-05T19:08:45.000Z (over 3 years ago)
- Last Synced: 2025-09-21T12:42:50.739Z (9 months ago)
- Topics: lazy-evaluation, python, tables
- Language: Python
- Homepage: https://pypi.org/project/lazy-table
- Size: 186 KB
- Stars: 57
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://pepy.tech/project/lazy-table)
[](https://badge.fury.io/py/lazy-table)

A [python-tabulate](https://github.com/astanin/python-tabulate) wrapper for producing tables from generators.
## Motivation
lazy_table is useful when (*i*) each row of your table is generated by a possibly expensive computation and (*ii*) you want to print rows to the screen as soon as they are available.
For example, the rows in the table below correspond to [numerically solving an ordinary differential equation](https://en.wikipedia.org/wiki/Numerical_methods_for_ordinary_differential_equations) with progressively more steps. See [examples/euler_vdp.py](https://raw.githubusercontent.com/parsiad/lazy-table/master/examples/euler_vdp.py) for the code to generate this table.

Here is the same example with a progress bar.

## Installation and usage
Install lazy_table via pip:
```console
$ pip install lazy_table
```
In your Python code, add
```python
import lazy_table as lt
```
Now, generating your own lazy table is as simple as calling `lt.stream` on a generator that yields one or more lists, where each list is a row of your table (see the example below).
## Example
As a toy example, consider printing the first 10 [Fibonacci numbers](https://en.wikipedia.org/wiki/Fibonacci_number) in a table.
Since this is a relatively cheap operation, we will pretend it is expensive by adding an artificial `sleep(1)` call beteween Fibonacci numbers.
```python
import time
import lazy_table as lt
def fib_table(n):
x0, x1 = 0, 1
yield [0, x0]
yield [1, x1]
for i in range(2, n + 1):
time.sleep(1) # Simulate work
x0, x1 = x1, x0 + x1
yield [i, x1]
lt.stream(fib_table(10), headers=['N', 'F_N'])
```
Your final table should look like this:
```
N F_N
--- -----
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
```
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 = 10
artist = lt.artists.ConsoleWithProgress()
lt.stream(fib_table(n), headers=['N', 'F_N'], n_rows=n + 1, artist=artist)
```