Testing whether a function is executing in a Jupyter notebook?

I want to be able to test whether a function is executing in a Jupyter notebook, so as to call the right display function. Perhaps something similar to this:

julia> isdefined(:IJulia)
true

although that works in the REPL too… :slight_smile:

The function isinteractive() might be what you need.

isdefined(Main, :IJulia) && Main.IJulia.inited should work.

However, if you are trying to determine whether you are running in Jupyter in order to “call the right display function”, then you are probably using the whole display machinery incorrectly.

The whole point of the display machinery is that the front-end will decide on the display format for you. Your job, for a given object type T, is to define show(io::IO, ::MIME"image/png", x::T) etc. methods (or writemime for Julia < 0.5) for each of the MIME types that you support. Then you can just call display(x) and the richest displayable format will be chosen for you.

(Or you can just return x::T … when a REPL input or an IJulia cell executes, it automatically calls display on the result of the last expression.)

Thanks, Steven, this works fine. I’m creating a file and then want to display it in a notebook if it’s available.