Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add live updating for any render #382

Merged
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
fcf3ed6
Set playground folder untracked
Oct 10, 2020
dd26a3d
Use Progress to generate a Live class to have an updatable render
Oct 13, 2020
52cc966
Merge remote-tracking branch 'upstream/master' into feat/add-live-tab…
Oct 13, 2020
133ae93
Merge branch 'master' into feat/add-live-table-proposal
Oct 24, 2020
d8df37b
add threading to the live function
Oct 24, 2020
cfa625c
remove spurious typing auto-import
Oct 24, 2020
119211e
use same name for refresh per second instead of delay time. matches w…
Oct 24, 2020
ae15cff
provide comment as to why Live needs lock in process_renderables
Oct 24, 2020
a2385c5
Add the default feature of hiding the Live when the display is too
Oct 24, 2020
d4bb878
add some of the playground examples into the module
Oct 24, 2020
7e01d31
add documentation describing the API of Live
Oct 24, 2020
6da3b2a
fix un-needed assert check expression
Oct 24, 2020
bf35879
add saner values for the example
Oct 25, 2020
34f1366
add reference documentation for the live module
Oct 25, 2020
5c09f32
add in an initial line of tests
Oct 25, 2020
f6d1496
correctly format all files
Oct 25, 2020
3da8727
add a diff to see what is wrong in ubuntu test
Oct 25, 2020
9e11042
live test only print diff
Oct 25, 2020
bdbd05e
remove unhelpful difflib print
Oct 25, 2020
6a50f1a
fix incorrect test
Oct 25, 2020
fa63fb8
force utf-8 encoding for python3.6
Oct 25, 2020
8c045b7
put throught the same force encoding-decoding conversion
Oct 25, 2020
125dc1d
temporarily disable live tests on 3.7
Oct 25, 2020
4b7ada8
remove carriage return for windows testing
Oct 25, 2020
972eace
add live tests with fluctuating data lengths
Oct 25, 2020
3853be1
add check for getting item
Oct 25, 2020
6b7373f
remove auto-refresh test as it is non-deterministic
Oct 25, 2020
5a27df8
add check with a file to make sure it prints nothing
Oct 25, 2020
6ccbaee
file/dumb_terminal console will now print out the final result
Oct 26, 2020
98c1d58
add usage documentation for the live module
Oct 26, 2020
a154bcd
add nathan to contributors
Oct 26, 2020
6ee5f06
fix live test by disabling auto_refresh
Oct 26, 2020
4ac0cb8
remove old code from live docs
Oct 26, 2020
e0337f5
simplify live demo example
Oct 31, 2020
bf510d1
Merge remote-tracking branch 'upstream/master' into feat/add-live-tab…
Oct 31, 2020
d44e588
reformat files
Oct 31, 2020
171cad4
duplicate table_movie with live
Oct 31, 2020
5553e6b
adjust rendering technique to custom live_render
Nov 1, 2020
cbb88a4
add changes from last night, will start to adjust with review changes…
Nov 1, 2020
d890c4e
fix up the trivial changes first from review
Nov 1, 2020
1f8d565
change implementation from hiding overflow with message to provide ov…
Nov 1, 2020
c4e9241
clean up documentation and remove un-needed re-shaping
Nov 1, 2020
58a824e
clean up the tests to use a simple testing procedure
Nov 1, 2020
20b2d29
fix docstring for live
Nov 1, 2020
7514708
fix typo in live vertial overflow
Nov 1, 2020
a509961
add reference to complex examples of live display in repo
Nov 1, 2020
30fe4d5
remove duplicate code by extending live_render for live
Nov 1, 2020
c9ac903
remove print displays
Nov 1, 2020
14e1d76
Get constantly fluctuating tables to work with refresh thread.
Nov 9, 2020
a1cc74e
use live vertical_overflow for live_render instead of keeping track
Nov 10, 2020
068f432
Center live overflow ellipsis
Dec 3, 2020
7a82414
consolidate table_movie_live and table_movie into one file and update…
Dec 3, 2020
1020405
Merge branch 'master' of github.com:willmcgugan/rich into feat/add-li…
Dec 3, 2020
47d94e8
fix format check for file not apart of merge request
Dec 3, 2020
4236348
modify to pass code coverage
Dec 3, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mypy_report
docs/build
docs/source/_build
tools/*.txt
playground/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ The following people have contributed to the development of Rich:
- [Oleksis Fraga](https://github.com/oleksis)
- [Hedy Li](https://github.com/hedythedev)
- [Will McGugan](https://github.com/willmcgugan)
- [Nathan Page](https://github.com/nathanrpage97)
11 changes: 6 additions & 5 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ Welcome to Rich's documentation!
logging.rst
traceback.rst
prompt.rst

tables.rst
padding.rst
panel.rst
group.rst
panel.rst
group.rst
columns.rst
live.rst
progress.rst
markdown.rst
syntax.rst
syntax.rst

protocol.rst

reference.rst
Expand Down
172 changes: 172 additions & 0 deletions docs/source/live.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
.. _live:

Live Display
============

Rich can display continiuously updated information for any renderable.

To see some live display examples, try this from the command line::

python -m rich.live

.. note::

If you see ellipsis "...", this indicates that the terminal is not tall enough to show the full table.

Basic Usage
-----------

The basic usage can be split into two use cases.

1. Same Renderable
~~~~~~~~~~~~~~~~~~

When keeping the same renderable, you simply pass the :class:`~rich.console.RenderableType` you would like to see updating and provide
a ``refresh_per_second`` parameter. The Live :class:`~rich.live.Live` will automatically update the console at the provided refresh rate.


**Example**::

import time

from rich.live import Live
from rich.table import Table

table = Table()
table.add_column("Row ID")
table.add_column("Description")
table.add_column("Level")

with Live(table, refresh_per_second=4): # update 4 times a second to feel fluid
for row in range(12):
time.sleep(0.4) # arbitrary delay
# update the renderable internally
table.add_row(f"{row}", f"description {row}", "[red]ERROR")


2. New Renderable
~~~~~~~~~~~~~~~~~

You can also provide constant new renderable to :class:`~rich.live.Live` using the :meth:`~rich.live.Live.update` function. This allows you to
completely change what is rendered live.

**Example**::

import random
import time

from rich.live import Live
from rich.table import Table


def generate_table() -> Table:

table = Table()
table.add_column("ID")
table.add_column("Value")
table.add_column("Status")

for row in range(random.randint(2, 6)):
value = random.random() * 100
table.add_row(
f"{row}", f"{value:3.2f}", "[red]ERROR" if value < 50 else "[green]SUCCESS"
)
return table


with Live(refresh_per_second=4) as live:
for _ in range(40):
time.sleep(0.4)
live.update(generate_table())

Advanced Usage
--------------

Transient Display
~~~~~~~~~~~~~~~~~

Normally when you exit live context manager (or call :meth:`~rich.live.Live.stop`) the last refreshed item remains in the terminal with the cursor on the following line.
You can also make the live display disappear on exit by setting ``transient=True`` on the Live constructor. Here's an example::

with Live(transient=True) as live:
...

Auto refresh
~~~~~~~~~~~~

By default, the live display will refresh 4 times a second. You can set the refresh rate with the ``refresh_per_second`` argument on the :class:`~rich.live.Live` constructor.
You should set this to something lower than 4 if you know your updates will not be that frequent or higher for a smoother feeling.

You might want to disable auto-refresh entirely if your updates are not very frequent, which you can do by setting ``auto_refresh=False`` on the constructor.
If you disable auto-refresh you will need to call :meth:`~rich.live.Live.refresh` manually or :meth:`~rich.live.Live.update` with ``refresh=True``.

Vertical Overflow
~~~~~~~~~~~~~~~~~

By default, the live display will display ellipsis if the renderable is too large for the terminal. You can adjust this by setting the
``vertical_overflow`` argument on the :class:`~rich.live.Live` constructor.

- crop: Show renderable up to the terminal height. The rest is hidden.
- ellipsis: Similar to crop except last line of the terminal is replaced with "...". This is the default behavior.
- visible: Will allow the whole renderable to be shown. Note that the display cannot be properly cleared in this mode.

.. note::

Once the live display stops on a non-transient renderable, the last frame will render as **visible** since it doesn't have to be cleared.

Complex Renders
~~~~~~~~~~~~~~~

Refer to the :ref:`Render Groups` about combining multiple :class:`RenderableType` together so that it may be passed into the :class:`~rich.live.Live` constructor
or :meth:`~rich.live.Live.update` method.

For more powerful structuring it is also possible to use nested tables.


Print / log
~~~~~~~~~~~

The Live class will create an internal Console object which you can access via ``live.console``. If you print or log to this console, the output will be displayed *above* the live display. Here's an example::

import time

from rich.live import Live
from rich.table import Table

table = Table()
table.add_column("Row ID")
table.add_column("Description")
table.add_column("Level")

with Live(table, refresh_per_second=4): # update 4 times a second to feel fluid
for row in range(12):
live.console.print("Working on row #{row}")
time.sleep(0.4)
table.add_row(f"{row}", f"description {row}", "[red]ERROR")


If you have another Console object you want to use, pass it in to the :class:`~rich.live.Live` constructor. Here's an example::

from my_project import my_console

with Live(console=my_console) as live:
my_console.print("[bold blue]Starting work!")
...

.. note::

If you are passing in a file console, the live display only show the last item once the live context is left.

Redirecting stdout / stderr
~~~~~~~~~~~~~~~~~~~~~~~~~~~

To avoid breaking the live display visuals, Rich will redirect ``stdout`` and ``stderr`` so that you can use the builtin ``print`` statement.
This feature is enabled by default, but you can disable by setting ``redirect_stdout`` or ``redirect_stderr`` to ``False``.


Examples
--------

See `table_movie_live.py <https://github.com/willmcgugan/rich/blob/master/examples/table_movie_live.py>`_ and
nathanrpage97 marked this conversation as resolved.
Show resolved Hide resolved
`top_lite_simulator.py <https://github.com/willmcgugan/rich/blob/master/examples/top_lite_simulator.py>`_
for deeper examples of live displaying.
3 changes: 2 additions & 1 deletion docs/source/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Reference
reference/columns.rst
reference/console.rst
reference/emoji.rst
reference/highlighter.rst
reference/highlighter.rst
reference/live.rst
reference/logging.rst
reference/markdown.rst
reference/markup.rst
Expand Down
5 changes: 5 additions & 0 deletions docs/source/reference/live.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
rich.live
=========

.. automodule:: rich.live
:members:
Loading