# Testing whether a function is executing in a Jupyter notebook?

**URL:** <https://discourse.julialang.org/t/testing-whether-a-function-is-executing-in-a-jupyter-notebook/222>\
**Category:** General Usage\
**Created:** [November 9, 2016, 4:20pm UTC](https://discourse.julialang.org/t/testing-whether-a-function-is-executing-in-a-jupyter-notebook/222 "2016-11-09T16:20:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [November 9, 2016, 4:20pm UTC](https://discourse.julialang.org/t/testing-whether-a-function-is-executing-in-a-jupyter-notebook/222/1 "2016-11-09T16:20:53Z")

</div>

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… 🙂

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [November 9, 2016, 6:29pm UTC](https://discourse.julialang.org/t/testing-whether-a-function-is-executing-in-a-jupyter-notebook/222/2 "2016-11-09T18:29:34Z")

</div>

The function `isinteractive()` might be what you need.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 9, 2016, 6:42pm UTC](https://discourse.julialang.org/t/testing-whether-a-function-is-executing-in-a-jupyter-notebook/222/3 "2016-11-09T18:42:16Z")

</div>

`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.)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [November 10, 2016, 8:28am UTC](https://discourse.julialang.org/t/testing-whether-a-function-is-executing-in-a-jupyter-notebook/222/4 "2016-11-10T08:28:55Z")

</div>

> [@stevengj](#):
>
> isdefined(Main, :IJulia) && Main.IJulia.inited

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