# How to show a struct in html

**URL:** <https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 14, 2022, 1:11pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273 "2022-09-14T13:11:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 14, 2022, 1:11pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/1 "2022-09-14T13:11:26Z")

</div>

I have the following code:

```julia
using Parameters
import Base.show

const BOLD = "\e[1m"
const RED = "\033[31;1m"
const GREEN = "\033[32;1m"
const NORMAL = "\e[0m"

@with_kw mutable struct ChargerResult
    search_strings = String["No buffer space", "CAN1 Error"]
    frequency = Int64[0, 0]
end
Base.show(io::IO, res::ChargerResult) = begin
    println("\n\n", BOLD, "Charger log(s):\n", NORMAL)
    for i in 1:length(res.frequency)
        if res.frequency[i] == 0
            println(io, GREEN, "Message: \"$(res.search_strings[i])\" not found!", NORMAL)
        else
            println(io, RED, "Message: \"$(res.search_strings[i])\" found $(res.frequency[i]) times!", NORMAL)
        end
    end
end

res = ChargerResult()

```

This allows me to print a struct of type ChargerResult with colors on the command line.

Now I also want to have a similar html representation of this struct.

How can I achieve that?

Can I write different show methods for console and for html representation?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 14, 2022, 2:27pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/2 "2022-09-14T14:27:34Z")

</div>

You can pass a `MIME` type as the second argument to `show`. By default it’s `text/plain` i.e. plaintext, so your method above applies to plaintext console output. You can write a method for the `text/html` MIME type separately, like:

```julia
Base.show(io::IO, ::MIME"text/html", res::ChargerResult) = begin
           println("<br> <strong>Charger log(s):</strong> <br>", NORMAL)
           for i in 1:length(res.frequency)
               if res.frequency[i] == 0
                   println(io, "<span style=\"color: #270\">Message: \"$(res.search_strings[i])\" not found!</span>")
               else
                   println(io, "<span style=\"color: #D8000C\">Message: \"$(res.search_strings[i])\" found $(res.frequency[i]) times!</span>")
               end
           end
       end

```

(Untested, and sort of a direct translation of the commandline output version - you might want fancier/more idiomatic HTML in the actual code.)

---

<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:** [September 14, 2022, 2:45pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/3 "2022-09-14T14:45:58Z")

</div>

> [@ufechner7](#):
>
> ` println("\n\n", BOLD, "Charger log(s):\n", NORMAL)`

Separate from your question about HTML output, which was answered above (and is covered in the manual [section on pretty-printing](https://docs.julialang.org/en/v1/manual/types/#man-custom-pretty-printing)), is there a reason you’re not using [`printstyled(io, s, bold=true)`](https://docs.julialang.org/en/v1/base/io-network/#Base.printstyled) here?

Even if you do need to print formatting codes directly for some reason in your `show`, you should first check [`get(io, :color, false)`](https://docs.julialang.org/en/v1/base/io-network/#Base.IOContext) to see whether the `io` stream supports the ANSI formatting escapes. The `printstyled` function will do this for you.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 14, 2022, 2:53pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/4 "2022-09-14T14:53:11Z")

</div>

Thank you, I did not know the printstyled function…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 14, 2022, 2:55pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/5 "2022-09-14T14:55:04Z")

</div>

Thank you! But how can I print the html version to a file or to a string after adding the new show method?

---

<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:** [September 14, 2022, 4:25pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/6 "2022-09-14T16:25:10Z")

</div>

> [@ufechner7](#):
>
> But how can I print the html version to a file or to a string after adding the new show method?

Call it? `show(io, "text/html", mystruct)`

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 14, 2022, 4:29pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/7 "2022-09-14T16:29:37Z")

</div>

The difference between print and show is still not clear to me… Does print also calls show in the end?

---

<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:** [September 14, 2022, 4:33pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/8 "2022-09-14T16:33:07Z")

</div>

> [@ufechner7](#):
>
> Does print also calls show in the end?

By default, yes, `print(io, x)` calls `show(io, x)`, the 2-argument `show`. However, there are some types for which it is different, e.g. for strings:

```julia
julia> show("foo\nbar")
"foo\nbar"
julia> print("foo\nbar")
foo
bar

```

This is explained in the [documentation for `Base.print`](https://docs.julialang.org/en/v1/base/io-network/#Base.print).

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 14, 2022, 4:42pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/9 "2022-09-14T16:42:46Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [September 14, 2022, 6:30pm UTC](https://discourse.julialang.org/t/how-to-show-a-struct-in-html/87273/10 "2022-09-14T18:30:13Z")

</div>

There’s also this 2020 JuliaCon talk:

[![](https://global.discourse-cdn.com/julialang/original/3X/b/7/b7220be346a0979b2f2dcc93f4ea3c16c97f67e1.jpeg "JuliaCon 2020 | Display, show and print -- how Julia's display system works | Fredrik Ekre") ](https://www.youtube.com/watch?v=S1Fb5oNhhbc)

An abstract of the talk, with a link to the slides used, is at:

> **[JuliaCon 2020: display, show and print – how Julia's display system works |...](https://fredrikekre.se/posts/display-show-print/)**
>
> Abstract, slides and a recording from my JuliaCon 2020 presentation titled: display, show and print – how Julia's display system works
