# Modify the behaviour of Base.show based on an environment variable

**URL:** https://discourse.julialang.org/t/modify-the-behaviour-of-base-show-based-on-an-environment-variable/21852
**Category:** Juno
**Created:** [March 14, 2019, 4:02am UTC](https://discourse.julialang.org/t/modify-the-behaviour-of-base-show-based-on-an-environment-variable/21852 "2019-03-14T04:02:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [March 14, 2019, 4:02am UTC](https://discourse.julialang.org/t/modify-the-behaviour-of-base-show-based-on-an-environment-variable/21852/1 "2019-03-14T04:02:38Z")

</div>

Hello,

I would like to use a different `Base.show` based on some environment variable so for instance either using `::MIME"image/png"` or `::MIME"text/plain"`. Something like:

```julia
import Base.show

struct Foo
    fpath::String
end

const MYENV = Dict("imshow"=>false)
toggle(b::Bool) = (MYENV["imshow"] = b)

# use this if MYENV["imshow"]
function Base.show(io, ::MIME"image/png", f::Foo)
    write(io, read(f.fpath))
    return nothing
end

# use this if !MYENV["imshow"]
function Base.show(io, ::MIME"text/plain", f::Foo)
    write(io, f.fpath)
    return nothing
end

```

is this possible? thanks! (a somewhat dirty hack is to toggle `Atom.PlotPaneEnabled.x`…)

* * *

**Edit** : the following seems to work but I welcome alternative ideas

```julia
import Base.show

const MYENV = Dict("imshow" => false)

toggle(b::Bool) = (MYENV["imshow"] = b)

struct Foo
    fpath::String
end

struct ImFoo
    f::Foo
end

function Base.show(io::IO, f::Foo)
    MYENV["imshow"] && display(ImFoo(f))
    write(io, f.fpath)
    return nothing
end

function Base.show(io::IO, ::MIME"image/png", o::ImFoo)
    write(io, read(o.f.fpath))
    return nothing
end

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 14, 2019, 7:41am UTC](https://discourse.julialang.org/t/modify-the-behaviour-of-base-show-based-on-an-environment-variable/21852/2 "2019-03-14T07:41:10Z")

</div>

> [@tlienart](#):
>
> use a different `Base.show` based on some environment variable so for instance either using `::MIME"image/png"` or `::MIME"text/plain"`

I would just code two functions, eg `_show1` and `_show2`, and branch inside the implementation of `show` based on the flag. Eg

```julia
const FLAG = Ref(false)
toggle() = FLAG[] = !FLAG[]
_show1(io, x) = print(io, "what a nice $x")
_show2(io, x) = print(io, "great $x we have here")
show(io::IO, x::Foo) = FLAG[] ? _show1(io, x) : _show2(io, x)

```

Also note that using a `Dict` is not optimal, so I used a `RefValue`, but in practice it may not matter much.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 14, 2019, 8:23am UTC](https://discourse.julialang.org/t/modify-the-behaviour-of-base-show-based-on-an-environment-variable/21852/3 "2019-03-14T08:23:42Z")

</div>

You should also be able to (conditionally) throw a `MethodError` in `Base.show(io, ::MIME"image/png", f::Foo)` to disable it.

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [March 14, 2019, 10:56am UTC](https://discourse.julialang.org/t/modify-the-behaviour-of-base-show-based-on-an-environment-variable/21852/4 "2019-03-14T10:56:25Z")

</div>

Thanks both, maybe I should have given more context as to what prompted the question.

It seems to me (unless I’m mistaken) that Juno captures the fact that there exists a show method with a `::MIME"image/png"` and consequently opens the plotpane (assuming the show plots in plot pane is enabled), this even if it within the body of the corresponding show, nothing happens.  
This is why as far as I could tell from experimenting there needed to be a separate pathway so that Juno does not open the plotpane when the flag is false. It seems the workaround that I mentioned in my original post works fine so I’ll probably stick with it.

@Tamas_Papp would you have a pointer on what’s best practice in terms of keeping a bunch (I reckon I have a dozen or so) of environment variables which may be modified by the user? I assumed a Dict would be ok but maybe that a named tuple of refs is preferable?

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 14, 2019, 11:03am UTC](https://discourse.julialang.org/t/modify-the-behaviour-of-base-show-based-on-an-environment-variable/21852/5 "2019-03-14T11:03:33Z")

</div>

> [@tlienart](#):
>
> It seems to me (unless I’m mistaken) that Juno captures the fact that there exists a show method with a `::MIME"image/png"` and consequently opens the plotpane (assuming the show plots in plot pane is enabled), this even if it within the body of the corresponding show, nothing happens.

Ah, right. Just overload `showable(::MIME"image/png", ::Foo)` depending on your condition then, that should work fine with Juno.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 14, 2019, 11:36am UTC](https://discourse.julialang.org/t/modify-the-behaviour-of-base-show-based-on-an-environment-variable/21852/6 "2019-03-14T11:36:09Z")

</div>

> [@tlienart](#):
>
> best practice in terms of keeping a bunch (I reckon I have a dozen or so) of environment variables which may be modified by the user? I assumed a Dict would be ok but maybe that a named tuple of refs is preferable?

A `Dict` may be fine, especially if you are just using these for printing and it is not a bottleneck. If you want types inferred, you could go with a `NamedTuple` of `RefValue`s.
