Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize show for TwoColumn etc to work with svgs #37

Open
greimel opened this issue Oct 28, 2023 · 9 comments
Open

Generalize show for TwoColumn etc to work with svgs #37

greimel opened this issue Oct 28, 2023 · 9 comments

Comments

@greimel
Copy link
Contributor

greimel commented Oct 28, 2023

Hi @eford!

@jkrumbiegel provided the following code snippet on Slack

using Base64
using CairoMakie

struct TwoColumn{L, R}
    left::L
    right::R
end

function show_html_or_svg(io, x)
    if showable("text/html", x)
        show(io, MIME"text/html"(), x)
    elseif showable("image/svg+xml", x)
        print(io, """<img src="data:image/svg+xml;base64,""")
        print(io, base64encode(repr(MIME"image/svg+xml"(), x)))
        print(io, "\">")
    else
        show(io, x)
    end
    return
end

function Base.show(io::IO, M::MIME"text/html", tc::TwoColumn)
    write(io, """<div style="display: flex;"><div style="flex: 50%;">""")
    show_html_or_svg(io, tc.left)
    write(io, """</div><div style="flex: 50%;">""")
    show_html_or_svg(io, tc.right)
    write(io, """</div></div>""")
    return
end

TwoColumn("hi", lines(1:10))

I think it would be really nice to use this approach in this package, do you agree?

I'd be willing to prepare a PR if @jkrumbiegel and @eford approve (unless @jkrumbiegel wants to do it himself)

@eford
Copy link
Collaborator

eford commented Oct 28, 2023

I'm happy to see PRs for improvements.
I tried to give your snippet a try, but don't know what to import to get lines.

@greimel
Copy link
Contributor Author

greimel commented Oct 28, 2023

sorry, you need using CairoMakie. I adapted the snippet. Plots from this package currently don't have an html representation, but an svg representation.

@eford
Copy link
Collaborator

eford commented Oct 28, 2023 via email

@jkrumbiegel
Copy link

Might be that you also need a CairoMakie.activate!(type = "svg") so it activates that showable. But this is not really about CairoMakie I think, just about objects that may return svg but not html. And this probably needs a more general solution for more MIME types, I just provided some quick help on slack :)

@eford
Copy link
Collaborator

eford commented Oct 28, 2023 via email

@fonsp
Copy link
Member

fonsp commented Oct 30, 2023

Hey! Do you already know about PlutoUI.ExperimentalLayout? It allows you to create a TwoColumn function that works for anything, not just html and svg. It also makes it super fast!

@fonsp
Copy link
Member

fonsp commented Oct 30, 2023

image

# ╔═╡ df7dd1e4-7739-11ee-2c0a-5779ff4a782b
function TwoColumns(a, b)
	PlutoUI.ExperimentalLayout.Div([
		PlutoUI.ExperimentalLayout.Div([a]; style=Dict("flex" => "0 0 50%"))
		PlutoUI.ExperimentalLayout.Div([b]; style=Dict("flex" => "0 0 50%"))
	]; style=Dict("display" => "flex", "flex-direction" => "row"))
end

# ╔═╡ 2193c3aa-96c3-4621-a451-58289e879cdb
TwoColumns(
	md"# Hello!
	
	I am a computer",
	rand(10)
)

# ╔═╡ efd1aca8-5c8f-499d-9748-ccc297c7d167
import PlutoUI

@eford
Copy link
Collaborator

eford commented Oct 31, 2023 via email

@eford
Copy link
Collaborator

eford commented Apr 24, 2024

Hi @fonsp,
While merging another PR, I checked open issues and noticed this one.
Is PlutoUI.ExperimentalLayout still experimental? Or has it become something we expect to persist (perhaps with a different name)?
Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants