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

Jupyter Stderr #77

Closed
Mathemmagician opened this issue Apr 17, 2021 · 11 comments
Closed

Jupyter Stderr #77

Mathemmagician opened this issue Apr 17, 2021 · 11 comments

Comments

@Mathemmagician
Copy link

Mathemmagician commented Apr 17, 2021

Since ic outputs to stderr, it displays weirdly in Jupyter, especially when the cell is both printing and ic-ing a lot of stuff at the same time, I've observed the order of output to be inconsistent at times. Would be nice to be able to redirect to stdout instead.

Maybe something like:

from icecream import ic
ic.configureOutput(stdout=True)

After digging deeper, I found out that what I'm looking for can be accomplished with:

ic.configureOutput(outputFunction=print)

It took me a while to figure it out, so I'm sure there are others. Should probably be added to the README as an example showing to how to redirect to stdout instead of stderr. Although, the line above is quite long, something shorter would be nicer.

P.S. Jupyter inconsistency can be achieved with:

from icecream import ic
for i in range(1, 100):
    print(i)
    ic('test')

outputs with messed up order:

Screen Shot 2021-04-16 at 10 49 03 PM

Adding flush=True solves it, but obviously rather just have everything in stdout instead.

@gruns
Copy link
Owner

gruns commented Apr 17, 2021

is flush=True not required for stdout, only for stderr?

@Mathemmagician
Copy link
Author

Mathemmagician commented Apr 19, 2021

That's right, only stderr.

This makes sense since ic.configureOutput(outputFunction=print) sends both outputs (normal print and ic) to the same stream, so the order is preserved.

@gruns
Copy link
Owner

gruns commented Apr 20, 2021

This makes sense since ic.configureOutput(outputFunction=print) sends both outputs (normal print and ic) to the same stream, so the order is preserved.

aha

Should probably be added to the README as an example showing to how to redirect to stdout instead of stderr.

youre actually the first one to express a desire here. when others express the same, ill add it to the readme

or ill include this as an example in the forthcoming overhaul to icecream's output. see #31

does ic.configureOutput(outputFunction=print) resolve this for you for now? if so, ill close this issue

@Mathemmagician
Copy link
Author

Surprised I'm the first one! I would imagine most data scientists who work with Jupyter would run into this, but maybe I'm wrong.

ic.configureOutput(outputFunction=print) worked, yes.

@gruns gruns closed this as completed Apr 20, 2021
@gruns
Copy link
Owner

gruns commented Apr 20, 2021

Surprised I'm the first one! I would imagine most data scientists who work with Jupyter would run into this, but maybe I'm wrong.

future reader of this thread: if you want a way to print to stdout highlighted, like in the readme, please let us know!

thank you, @Mathemmagician! 👐

@jerabaul29
Copy link

Had the same issue with the Xonsh shell, this fixed it, thanks :) .

@gruns
Copy link
Owner

gruns commented Jul 2, 2021

ic.configureOutput(outputFunction=print) worked, yes.

this means the issue lies in writing to stderr, not (afaict) in the underlying icecream library itself

@jerabaul29 what behavior do you observe, exactly, with icecream outputting to stderr in xonsh? was it slow? did output stutter? if possible, can you take a video and share it here so i can see what you see?

perhaps the best path for icecream is to try to detect (how?) when icecream runs in an environment where stderr has issues. then, in such an environment, write to stdout instead of stderr

@jerabaul29
Copy link

Sorry for the late reply. As said, using ic.configureOutput(outputFunction=print) as suggested in this thread solved the problem, so nothing special more from my side. The issue was being output order being messed up.

@gjvnq
Copy link

gjvnq commented Dec 15, 2021

Hey, while this technically solves the issue, it would be nice to have colorful printing on Jupyter by default.

@gjvnq
Copy link

gjvnq commented Dec 15, 2021

I found a work around for the coloring problem that plays nice with Jupyer Lab. I simply added the following to the top of my notebook:

try:
    from icecream import ic, colorize as ic_colorize
    ic.configureOutput(outputFunction=lambda s: print(ic_colorize(s)))
    ic('Hey, icecream is installed and working.')
except ImportError:  # Graceful fallback if IceCream isn't installed.
    ic = print
    ic('Using work around as icecream is not installed.')

I suggest you either add this to the documentation or even better autodetcte if the script is running on Jupyter and auto change to stdout.

Aparently this function works: (got from https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook )

def isnotebook():
    try:
        shell = get_ipython().__class__.__name__
        if shell == 'ZMQInteractiveShell':
            return True   # Jupyter notebook or qtconsole
        elif shell == 'TerminalInteractiveShell':
            return False  # Terminal running IPython
        else:
            return False  # Other type (?)
    except NameError:
        return False      # Probably standard Python interpreter

@danieldjewell
Copy link

I'm not sure that this is 100% the proper way to interface with the Jupyter/IPython API?

I was trying out icecream in the IPython REPL (from the command line) and was running into double output issues ... One solution that seemed to help was:

import IPython
ic.configureOutput(outputFunction = IPython.lib.pretty.RawText) 

This did break colorizing the output ... I suspect that interfacing directly with the IPython API is the "right" way to ensure that output works both in the IPython console REPL and also in Jupyter notebooks ... https://ipython.readthedocs.io/en/stable/api/generated/IPython.lib.pretty.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants