Skip to content

Commit c6facca

Browse files
authored
Merge pull request #51 from greimel/columns
Generalize `columns`
2 parents 562dd11 + 6d715bc commit c6facca

File tree

4 files changed

+59
-60
lines changed

4 files changed

+59
-60
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PlutoTeachingTools"
22
uuid = "661c6b06-c737-4d37-b85c-46df65de6f69"
33
authors = ["Eric Ford <[email protected]> and contributors"]
4-
version = "0.3.0"
4+
version = "0.3.1"
55

66
[deps]
77
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

example.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,18 @@ md"""
295295
"""
296296

297297
# ╔═╡ fc5789dd-4b86-4007-9771-a6246235fd73
298-
TwoColumn(md"Left col", md"Right col")
298+
Columns(md"Left col", md"Right col")
299299

300300
# ╔═╡ 2881e5de-f6b7-47c0-a794-3e0fa57b712b
301-
ThreeColumn(md"Left col", md"Middle col", md"Right col")
301+
Columns(md"Left col", md"Middle col", md"Right col")
302+
303+
# ╔═╡ c22f7f6a-171d-4379-86c9-1781875cf0a4
304+
md"""
305+
You can customize the widths of the columns.
306+
"""
302307

303308
# ╔═╡ 0e1e62a6-3b78-4415-89fe-fa17279fddbf
304-
TwoColumnWideLeft(md"Left col", md"Right col")
309+
Columns(md"Left col", md"Right col"; widths=[33, 66])
305310

306311
# ╔═╡ f43beea9-7a7e-4ee6-8ae6-350640c426aa
307312
TwoColumnWideRight(md"Left col", md"Right col")
@@ -957,6 +962,7 @@ version = "17.4.0+2"
957962
# ╟─d7593309-3462-4dba-8275-c2eb76b4c3fe
958963
# ╠═fc5789dd-4b86-4007-9771-a6246235fd73
959964
# ╠═2881e5de-f6b7-47c0-a794-3e0fa57b712b
965+
# ╟─c22f7f6a-171d-4379-86c9-1781875cf0a4
960966
# ╠═0e1e62a6-3b78-4415-89fe-fa17279fddbf
961967
# ╠═f43beea9-7a7e-4ee6-8ae6-350640c426aa
962968
# ╠═44d651d3-ce42-4061-b193-da7c31efed8e

src/PlutoTeachingTools.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module PlutoTeachingTools
22

3+
using PlutoUI.ExperimentalLayout: Div
34
using Markdown: @md_str, MD, Admonition, LaTeX
45
using HypertextLiteral: @htl, @htl_str
56
using Downloads: download # used in robustlocalresouce.jl
@@ -31,7 +32,7 @@ export display_msg_if_fail
3132
include("present.jl")
3233
export present_button
3334
export Foldable
34-
export TwoColumn, ThreeColumn
35+
export Columns, TwoColumn, ThreeColumn
3536
export TwoColumnWideLeft, TwoColumnWideRight
3637
export ChooseDisplayMode # combines present_button and WidthOverDocs
3738

src/present.jl

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,63 @@ end
88

99
Foldable(title, content) = details(title, content)
1010

11+
"""
1112
12-
struct TwoColumn{L,R}
13-
left::L
14-
right::R
15-
end
13+
Columns(cols...; widths, gap)
1614
17-
function Base.show(io, mime::MIME"text/html", tc::TwoColumn)
18-
write(io, """<div style="display: flex;"><div style="flex: 49%;">""")
19-
show(io, mime, tc.left)
20-
write(io, """</div><div style="flex: 2%;">""")
21-
write(io, """</div><div style="flex: 49%;">""")
22-
show(io, mime, tc.right)
23-
write(io, """</div></div>""")
24-
return nothing
25-
end
15+
Displays (any number of) columns nicely in Pluto.
2616
27-
struct TwoColumnWideLeft{L,R}
28-
left::L
29-
right::R
30-
end
17+
* `widths` should sum to 100
18+
* `gap` in percent
3119
32-
function Base.show(io, mime::MIME"text/html", tc::TwoColumnWideLeft)
33-
write(io, """<div style="display: flex;"><div style="flex: 65%;">""")
34-
show(io, mime, tc.left)
35-
write(io, """</div><div style="flex: 2%;">""")
36-
write(io, """</div><div style="flex: 33%;">""")
37-
show(io, mime, tc.right)
38-
write(io, """</div></div>""")
39-
return nothing
40-
end
20+
### Examples
21+
```julia
22+
# three columns
23+
Columns(
24+
almost(md"here"),
25+
almost(md"there"),
26+
md"bla bla bla"
27+
)
4128
42-
struct TwoColumnWideRight{L,R}
43-
left::L
44-
right::R
45-
end
29+
# two columns with customization
30+
Columns(
31+
almost(md"here"), almost(md"there"),
32+
widths = [40, 60], gap = 2
33+
)
34+
```
35+
"""
36+
function Columns(cols...; widths=nothing, gap=2)
37+
ncols = length(cols)
38+
ngaps = ncols - 1
39+
if isnothing(widths)
40+
widths = fill(100 / ncols, ncols)
41+
end
42+
if gap > 0 # adjust widths if gaps are desired
43+
widths = widths / sum(widths) * (sum(widths) - gap * ngaps)
44+
end
4645

47-
function Base.show(io, mime::MIME"text/html", tc::TwoColumnWideRight)
48-
write(io, """<div style="display: flex;"><div style="flex: 33%;">""")
49-
show(io, mime, tc.left)
50-
write(io, """</div><div style="flex: 2%;">""")
51-
write(io, """</div><div style="flex: 65%;">""")
52-
show(io, mime, tc.right)
53-
write(io, """</div></div>""")
54-
return nothing
55-
end
46+
columns = [
47+
Div([cols[i]], style=Dict("flex" => "0 1 $(widths[i])%")) for
48+
i in 1:ncols
49+
]
50+
the_gap = Div([], style=Dict("flex" => "0 0 $gap%"))
5651

57-
struct ThreeColumn{L,C,R}
58-
left::L
59-
center::C
60-
right::R
61-
end
52+
# insert gaps between columns
53+
# i.e. [a, b, c] ==> [a, gap, b, gap, c]
54+
children = vec([reshape(columns, 1, :); fill(the_gap, 1, ncols)])[1:(end - 1)]
6255

63-
function Base.show(io, mime::MIME"text/html", tc::ThreeColumn)
64-
write(io, """<div style="display: flex;"><div style="flex: 32%;">""")
65-
show(io, mime, tc.left)
66-
write(io, """</div><div style="flex: 2%;">""")
67-
write(io, """</div><div style="flex: 32%;">""")
68-
show(io, mime, tc.center)
69-
write(io, """</div><div style="flex: 2%;">""")
70-
write(io, """</div><div style="flex: 32%;">""")
71-
show(io, mime, tc.right)
72-
write(io, """</div></div>""")
73-
return nothing
56+
return Div(children, style=Dict("display" => "flex", "flex-direction" => "row"))
7457
end
7558

59+
# for backwards compatibility
60+
TwoColumn(a, b; kwargs...) = Columns(a, b; kwargs...)
61+
62+
ThreeColumn(a, b, c; kwargs...) = Columns(a, b, c; kwargs...)
63+
64+
TwoColumnWideLeft(a, b; kwargs...) = Columns(a, b; widths=[66, 34], kwargs...)
65+
66+
TwoColumnWideRight(a, b; kwargs...) = Columns(a, b; widths=[34, 66], kwargs...)
67+
7668
""" Provides checkbox to toggle full width and present mode. """
7769
function ChooseDisplayMode(;
7870
wide::Bool=false, present::Bool=false, lang::AbstractLanguage=default_language[]

0 commit comments

Comments
 (0)