# Conditional use of VSCode plot pane in \`Base.show\`?

**URL:** https://discourse.julialang.org/t/conditional-use-of-vscode-plot-pane-in-base-show/57520
**Category:** VS Code
**Created:** [March 19, 2021, 8:17am UTC](https://discourse.julialang.org/t/conditional-use-of-vscode-plot-pane-in-base-show/57520 "2021-03-19T08:17:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)
#### Post date: [March 19, 2021, 8:17am UTC](https://discourse.julialang.org/t/conditional-use-of-vscode-plot-pane-in-base-show/57520/1 "2021-03-19T08:17:36Z")

</div>

Hi, I have a type that can usually, but not always, be displayed nicely (through Latexify) in the VSCode plot pane. I can use conditionals/error handling in my `Base.show(::IO, ::MIME"juliavscode/html", ::MyType)` overload to fall back on `MIME"text/plain"` if it turns out that the neat display won’t work. So far, so good, and here is an MWE:

```julia
struct MWEType 
    toggle::Bool
end

function Base.show(io::IO, mime::MIME"juliavscode/html", x::MWEType)
  if x.toggle
      print(io, "<h1> Hello there! </h1>")
  else
      show(stdout, MIME("text/plain"), x)
  end
end

MWEType(true) ## Displays nicely in the plot pane
MWEType(false) ## Falls back on a safer display

```

However, even when I fall back on `stdout`, I still generate a new, empty “plot”. Is there a way to avoid this?

---

<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 19, 2021, 8:57am UTC](https://discourse.julialang.org/t/conditional-use-of-vscode-plot-pane-in-base-show/57520/2 "2021-03-19T08:57:25Z")

</div>

Yes, via `showable`:

```julia
struct Foo 
    html::Bool
end

Base.showable(::MIME"juliavscode/html", f::Foo) = f.html
Base.show(io::IO, ::MIME"juliavscode/html", f::Foo) = print(io, "<h1>pretty!</h1>")
Base.show(io::IO, ::MIME"text/plain", f::Foo) = print(io, "a bit plain, don't you think?")

Foo(false)
Foo(true)

```

or by throwing a `MethodError` to make the display system think your `show` method doesn’t exist:

```julia
struct Bar 
    html::Bool
end

function Base.show(io::IO, m::MIME"juliavscode/html", f::Bar)
    if f.html
        print(io, "<h1>pretty!</h1>")
    else
        throw(MethodError(show, (io, m, f)))
    end
end

Bar(false)
Bar(true)

```

---

<div class="post-metadata">

### Author: ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)
#### Post date: [March 19, 2021, 9:07am UTC](https://discourse.julialang.org/t/conditional-use-of-vscode-plot-pane-in-base-show/57520/3 "2021-03-19T09:07:30Z")

</div>

Perfect, thank you very much! 🎉

---

<div class="post-metadata">

### Author: ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)
#### Post date: [March 19, 2021, 9:09am UTC](https://discourse.julialang.org/t/conditional-use-of-vscode-plot-pane-in-base-show/57520/4 "2021-03-19T09:09:30Z")

</div>

To anyone coming after to see this: the call signature to `MethodError` should be `MethodError(show, (io, m, f))`.

---

<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 19, 2021, 9:11am UTC](https://discourse.julialang.org/t/conditional-use-of-vscode-plot-pane-in-base-show/57520/5 "2021-03-19T09:11:28Z")

</div>

Fixed above 🙂  
FWIW, the `showable` implementation is preferable unless it causes you to duplicate a bunch of work you’d ideally only do in `show`.

---

<div class="post-metadata">

### Author: ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)
#### Post date: [March 19, 2021, 9:23am UTC](https://discourse.julialang.org/t/conditional-use-of-vscode-plot-pane-in-base-show/57520/6 "2021-03-19T09:23:07Z")

</div>

Indeed, I would certainly have preferred that method if it was easily done. In my case, however, the second version could be used plug-and-play whereas the first one would require refactoring and I would still need to rely on `try`/`catch` since I don’t have a robust way of telling whether the nicer approach will actually work.
