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

New macro @bindname to display variable name before bond #305

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/PlutoUI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ end


include("./ScrubbableMatrix.jl")
include("./bindname.jl")

end
35 changes: 35 additions & 0 deletions src/bindname.jl
Original file line number Diff line number Diff line change
@@ -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(
"""<div style='display: flex; flex-wrap: wrap; align-items: baseline;'><code style='color: var(--cm-color-var) !important; font-weight: 700;'>$(
$(String(name))
)&nbsp<span style="opacity: .6">=</span>&nbsp</code>$(
bond
)</div>"""
)
end
end

export @bindname
Loading