# Display svg

**URL:** <https://discourse.julialang.org/t/display-svg/57469>\
**Category:** Visualization\
**Created:** [March 18, 2021, 12:40pm UTC](https://discourse.julialang.org/t/display-svg/57469 "2021-03-18T12:40:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonyhur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tonyhur/32/22732_2.png) [@tonyhur](https://discourse.julialang.org/u/tonyhur)\
**Post date:** [March 18, 2021, 12:40pm UTC](https://discourse.julialang.org/t/display-svg/57469/1 "2021-03-18T12:40:20Z")

</div>

In a Jupyter notebook I have the following code that worked with Julia 1.0

f = open(“Output.svg”, “r”)  
s = read(f,String)  
display(“image/svg+xml”,s)

Now in Julia 1.5.3 it did not work anymore. I looked up the documentation but could not find why.

Thanks Tony

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [May 22, 2021, 5:04pm UTC](https://discourse.julialang.org/t/display-svg/57469/2 "2021-05-22T17:04:23Z")

</div>

Ah neat! I didn’t know you could _ **directly** _ request Julia to draw an image like this! I thought you had to request it to display your object (ex: `display(myObject)`), and wait for a “display object” on the “display stack” to call the appropriate `show()` function.

### New way to write your code

```julia-auto
f = open("images/animage.svg", "r")
s = read(f) #Don't force Julia to read a "String"
close(f) #Don't forget to close your file!
@show typeof(s); flush(stdout) #FYI: So you can understand what type is read back
display(MIME("image/svg+xml"), s)

```

### Explanation

So it appears the main problem is that `display()` no longer has access to `show()` functions that accept image data stored in `String` types.

The good news is that if instead, you call `read(f)` without specifying a type, Julia returns a `Vector{UInt8}` - which is probably a more appropriate data structure anyways.

If you now call your `display()` function with this `Vector{UInt8}`, everything should work!

### Supplemental information

Notice how my version calls `display()` using `MIME("image/svg+xml")`. I suggest writing it this way, as it explicitly wraps the MIME information in an appropriate type. As a consequence, there should be less ambiguity with the call to `display()` (i.e.: Is this `String` parameter the MIME information, or is it the data I want to display?).

---

<div class="post-metadata">

**Author:** ![lazycoder](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@lazycoder](https://discourse.julialang.org/u/lazycoder)\
**Post date:** [July 1, 2022, 7:03pm UTC](https://discourse.julialang.org/t/display-svg/57469/3 "2022-07-01T19:03:18Z")

</div>

Does this still work? I’m getting errors:

```julia

julia> display("image/svg+xml",s)
ERROR: MethodError: no method matching show(::Base.TTY, ::MIME{Symbol("image/svg+xml")}, ::Vector{UInt8})
Closest candidates are:
  show(::IO, ::AbstractString, ::Any) at /usr/share/julia/base/multimedia.jl:111
  show(::IO, ::MIME{Symbol("text/tab-separated-values")}, ::Any) at /usr/share/julia/stdlib/v1.7/DelimitedFiles/src/DelimitedFiles.jl:830
  show(::IO, ::MIME{Symbol("text/csv")}, ::Any) at /usr/share/julia/stdlib/v1.7/DelimitedFiles/src/DelimitedFiles.jl:829
  ...
Stacktrace:
 [1] display(d::TextDisplay, M::MIME{Symbol("image/svg+xml")}, x::Any)
   @ Base.Multimedia ./multimedia.jl:249
 [2] display(m::MIME{Symbol("image/svg+xml")}, x::Any)
   @ Base.Multimedia ./multimedia.jl:342
 [3] display(mime::String, x::Any)
   @ Base.Multimedia ./multimedia.jl:217
 [4] top-level scope
   @ REPL[6]:1

```

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [July 1, 2022, 11:32pm UTC](https://discourse.julialang.org/t/display-svg/57469/4 "2022-07-01T23:32:06Z")

</div>

@lazycoder:  
This will only work if your “display” object supports rendering SVGs.

- Note how the original question was for displaying within Jupyter Notebooks.
- It appears from your error messages that you are using the base Julia command line (aka “REPL”).

So it is quite expected that your particular call to `display()` failed.

### Easiest way to “display” SVG with the REPL?

If that’s what you want, I suggest constructing/displaying an external window that can display .svg images (like a GTK window).

Here is a straw man of how you could go about implementing this:

```julia-auto
using Gtk #Need gtk loaded

#Viewer object to add to Julia's display stack:
struct GTKImgView <: AbstractDisplay
end

function display(d::GTKImgView, M::MIME{Symbol("image/svg+xml")}, x::Vector{UInt8})
   # Insert code here:
   # Construct a new GTK window
   # Render SVG on window
   # Show GTK window
end

#Create a GTKImgView object & add it to Julia's display stack:
#(It will get "asked" to display first when an SVG image is to be displayed)
pushdisplay(GTKImgView())

```

---

<div class="post-metadata">

**Author:** ![KeithWM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keithwm/32/48936_2.png) [@KeithWM](https://discourse.julialang.org/u/KeithWM)\
**Post date:** [February 16, 2024, 10:53pm UTC](https://discourse.julialang.org/t/display-svg/57469/5 "2024-02-16T22:53:40Z")

</div>

FWIW: I came across this issue in trying to plot an SVG in Pluto. There the solution was simply to type `HTML(s)`. I was inspired by using typeof to look at the type of the `html_str` example of [SVG cells in Pluto](https://discourse.julialang.org/t/svg-cells-in-pluto/87524).
