Skip to content

Output embeddable HTML directly #48

@lorenzoh

Description

@lorenzoh

I've run into two use cases where I'd like to show an image to text/html directly to get embeddable output:

  • to render code block results in Pollen.jl
    image

  • for rich HTML display of images inside other HTML structures, like a table
    For example, inside a PrettyTables.jl table using HTML output:
    image

Is there an implementation of show(::IO, ::MIME"text/html", ::ColorantMatrix) somewhere?


Workaround

For now, I've been using the below functions to render images (and other data that can be rendered to image mimetypes like plots) to HTML. However, it would be nice to have a method, at least for images (ColorantMatrixes) for showing to text/html that falls back on png/jpg/svg output.

function Base.show(io::IO, mime::MIME"text/html", x::ShowAsHTML)
    if showable(mime, x.val)
        show(io, mime, x.val)
    elseif showable(MIME("image/jpg"), x.val)
        buf = IOBuffer()
        show(buf, MIME("image/jpg"), x.val)
        print(io, """<img src="data:image/png;base64,$(Base64.base64encode(take!(buf)))"/>""")
    elseif showable(MIME("image/png"), x.val)
        buf = IOBuffer()
        show(buf, MIME("image/png"), x.val)
        print(io, """<img src="data:image/png;base64,$(Base64.base64encode(take!(buf)))"/>""")
    elseif showable(MIME("image/svg+xml"), x.val)
        buf = IOBuffer()
        show(buf, MIME("image/svg+xml"), x.val)
        print(io, replace(String(take!(buf)), "\n" => ""))
    else
        print(io, x)
    end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions