Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Alternative approach to redirect outputs to tqdm.write() #197

@lrq3000

Description

@lrq3000

A StackOverflow user reported another way to redirect all outputs to tqdm.write(), which is a lot more concise and as effective as ours, even more so in cases where we want to redirect all printings in all channels, not just stdout:

import inspect
import tqdm
# store builtin print
old_print = print
def new_print(*args, **kwargs):
    # if tqdm.tqdm.write raises error, use builtin print
    try:
        tqdm.tqdm.write(*args, **kwargs)
    except:
        old_print(*args, ** kwargs)
# globaly replace print with new_print
inspect.builtins.print = new_print

Maybe we should add it to the documentation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted 🙏We need you (discussion or implementation)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions