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

Could tqdm default to redirecting std{out,err} to tqdm.write? #296

Open
anntzer opened this issue Oct 27, 2016 · 7 comments
Open

Could tqdm default to redirecting std{out,err} to tqdm.write? #296

anntzer opened this issue Oct 27, 2016 · 7 comments
Labels
question/docs ‽ Documentation clarification candidate

Comments

@anntzer
Copy link
Contributor

anntzer commented Oct 27, 2016

I don't think it ever makes sense to print to the default stdout when in a tqdm loop (I may be wrong?), so perhaps it would make sense to default to enabling the writing redirection (with a kwarg to opt-out)?

@lrq3000
Copy link
Member

lrq3000 commented Oct 27, 2016

I don't think that's a good idea, libraries should never mess with the
standard IO. Plus there is no general case to cober because some people
will write messages to stdout, others to a log managed manually, others by
using the logging lib, others by writing to stdout but fork output to also
write in a log file (as I do), etc.

So we can provide code snippets but i don't think it's wise to implement
these as part of the lib. Unless something very elegant and general purpose
can be found.
Le 27 Oct. 2016 07:55, "Antony Lee" notifications@github.com a écrit :

I don't think it ever makes sense to print to the default stdout when in
a tqdm loop (I may be wrong?), so perhaps it would make sense to default to
enabling the writing redirection (with a kwarg to opt-out)?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#296, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABES3r5Q_ySnEp_AXj_WKxp2QgUGnM-gks5q4DzXgaJpZM4Kh-EE
.

@anntzer
Copy link
Contributor Author

anntzer commented Oct 27, 2016

Doesn't the example provided in the "redirecting writing" section of tqdm.github.io cover all the cases that you suggest? Please let me know if there are any specific counter-examples.

@lrq3000
Copy link
Member

lrq3000 commented Oct 30, 2016

No because for example you have to use a different wrapper if you want to redirect logging module output. I don't think there is a way to allow for generic redirection, this is a per application need...

@anntzer
Copy link
Contributor Author

anntzer commented Oct 30, 2016

No, logging (or more specifically, StreamHandler) just writes to whatever stream you asked it to write to (by default, it looks up sys.stderr every time it needs to emit a message, see https://hg.python.org/cpython/file/3.5/Lib/logging/__init__.py#l969 and https://hg.python.org/cpython/file/3.5/Lib/logging/__init__.py#l1051).

I find it a bit funny that I need to be claiming that your code is has more capabilities than what you believe, but let's play a game: assume that the stdout_redirect_to_tqdm context manager (from the "redirecting writing" example) was applied to each tqdm loop. Could you come up with a minimal example where this would lead to unexpected behavior?

@lrq3000
Copy link
Member

lrq3000 commented Oct 31, 2016

It's not that I doubt tqdm's capabilities, but rather that I fear automatic output redirection. We don't want tqdm to mess with parent apps, and output redirection is generally a big mess, so my opinion is that we should avoid providing an automated way to do that. But that's just my opinion...

@anntzer
Copy link
Contributor Author

anntzer commented Oct 31, 2016

What about providing a kwarg to tqdm.tqdm() (say, redirect=True) that would enable the redirection? Then lazy enough devs can try using it (I may just write tqdm = partial(tqdm, redirect=True)), and you'll see whether anyone reports a bug :-)

@casperdcl casperdcl added the question/docs ‽ Documentation clarification candidate label Dec 6, 2016
@cledoux
Copy link

cledoux commented Jun 5, 2018

I'd really like to see this implement, either as an option or via a tqdm provided context manager. Personally, I'd prefer the option suggested by @anntzer because I am a lazy dev. 😄

I think tqdm provided context manager would also be an acceptable compromise. Something that basically implements the example given in the README.

import tqdm
with tqdm.redirect_stdout():
    for i in tqdm.tqdm(range(10)):
        print(i)

I can put together a PR if there is interest in this. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question/docs ‽ Documentation clarification candidate
Projects
None yet
Development

No branches or pull requests

4 participants