How to have progress bar nicely at the bottom of screen and additional messages scrolling up while using echo, echo_error and verbose and not progress.print? #1709
Replies: 2 comments 1 reply
-
I don't know why your example isn't working but here is a minimal example that does work. I suggest starting from this then adding the additional Progress options (perhaps it's one or more of those that are affecting this). It more likely may be how you're configuring The rich / echo / verbose / progress code in OSXPhotos is a bit of a mess and requires some hacking such as setting up """Sample query command for osxphotos """
from __future__ import annotations
import time
from rich.progress import Progress
import osxphotos
from osxphotos.cli import query_command, verbose
from osxphotos.cli.verbose import get_verbose_console
from osxphotos.utils import pluralize
@query_command
def example(photos: list[osxphotos.PhotoInfo], **kwargs):
"""Sample query command for osxphotos. Prints out the filename and date of each photo.
Whatever text you put in the function's docstring here, will be used as the command's
help text when run via `osxphotos run cli_example_1.py --help` or `python cli_example_1.py --help`
"""
# verbose() will print to stdout if --verbose option is set
# you can optionally provide a level (default is 1) to print only if --verbose is set to that level
# for example: -VV or --verbose --verbose == level 2
verbose(f"Found {len(photos)} photo(s)")
verbose("This message will only be printed if verbose level 2 is set", level=2)
with Progress(console=get_verbose_console()) as progress:
num_photos = len(photos)
task = progress.add_task(
f"Processing [num]{num_photos}[/] {pluralize(num_photos, 'photo', 'photos')}",
total=num_photos,
)
for photo in photos:
photo_str = (
f"[filename]{photo.original_filename}[/] ([uuid]{photo.uuid}[/])"
)
progress.print(f"Processing {photo_str}")
verbose(f"Photo UUID: {photo.uuid}")
verbose(f"Photo filename: {photo.original_filename}", level=2)
time.sleep(1) # simulate processing time
progress.advance(task)
if __name__ == "__main__":
# call your function here
# you do not need to pass any arguments to the function
# as the decorator will handle parsing the command line arguments
example() |
Beta Was this translation helpful? Give feedback.
-
Thanks for the support. Was not able to have echo play nice with verbose. I guess they're working with different Consoles! So I ended up doing a bit of hacking... echo_error still messes up the output... but that's understandable as it writes to stderr. So that's ok! global echo
# Save echo for compatibility with progress
_old_echo = echo
(...)
with Progress(
(...)
console = get_verbose_console(),
) as progress:
# Redefine echo for compatibility with progress
echo = progress.print
(...)
# calling functions that use echo and verbose
# echo_error writes to stderr and messes progress output. But that's ok!
(...)
# Restore echo for compatibility with progress
echo = _old_echo |
Beta Was this translation helpful? Give feedback.
-
How to have progress bar nicely at the bottom of screen and additional messages scrolling up while using echo, echo_error and verbose and not progress.print?
This is beyond the scope of osxphotos... thought I'd try my luck.
Based on cli_example_2.py and cli_example_3.py I have a for loop to process some photos.
from osxphotos.cli.verbose import get_verbose_console
and passing it intoProgress(console=get_verbose_console,...)
and playing with optionsredirect_stdout
andredirect_stderr
but getting nowhere!Imports
Setting up verbose from verbose_print
Setting up the Progress Bar
Beta Was this translation helpful? Give feedback.
All reactions