From 42838fd0c8b91cdbe9e52cf0c83519703a517176 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Tue, 20 Aug 2024 17:06:50 +0200 Subject: [PATCH] New macro `@bindname` to display variable name before bond --- src/PlutoUI.jl | 1 + src/bindname.jl | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/bindname.jl diff --git a/src/PlutoUI.jl b/src/PlutoUI.jl index 466d1178..41450464 100644 --- a/src/PlutoUI.jl +++ b/src/PlutoUI.jl @@ -79,5 +79,6 @@ end include("./ScrubbableMatrix.jl") +include("./bindname.jl") end diff --git a/src/bindname.jl b/src/bindname.jl new file mode 100644 index 00000000..f54483e2 --- /dev/null +++ b/src/bindname.jl @@ -0,0 +1,35 @@ +import HypertextLiteral + +""" +```julia +@bindname name Slider(1:10) +``` + +Like `@bind` in Pluto, but it also displays the name of the variable before the input widget. +""" +macro bindname(name::Symbol, ex::Expr) + + # Some macro magic to call the `@bind` macro without hygiene. This will use whatever `@bind` is defined in the scope of the caller of this macro. + # We could just do `Main.PlutoRunner.@bind`, but then if you run the notebook.jl as a standalone script, it does not find the mock `@bind`. + bindcall = Expr(:macrocall, + Symbol("@bind"), + __source__, + name, + ex + ) + + # Messy HTML to avoid unintended whitespace, which can show up in rare cases. + quote + bond = $(esc(bindcall)) + + HypertextLiteral.@htl( + """
$( + $(String(name)) + ) = $( + bond + )
""" + ) + end +end + +export @bindname \ No newline at end of file