Skip to content

Commit

Permalink
rethinking bind
Browse files Browse the repository at this point in the history
  • Loading branch information
schlichtanders committed Jul 11, 2024
1 parent acffd37 commit 9a9236f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 69 deletions.
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "JolinPluto"
uuid = "5b0b4ef8-f4e6-4363-b674-3f031f7b9530"
authors = ["Stephan Sahm <[email protected]> and contributors"]
version = "0.1.76"
version = "0.1.77"

[deps]
AbstractPlutoDingetjes = "6e696c72-6542-2067-7265-42206c756150"
Expand Down Expand Up @@ -29,7 +29,6 @@ AWSExt = "AWS"
PlotsExt = "Plots"
PythonCallExt = "PythonCall"
RCallExt = ["RCall", "CondaPkg"]
CondaPkgExt = "CondaPkg"

[compat]
julia = "1.7"
Expand Down
5 changes: 0 additions & 5 deletions ext/CondaPkgExt.jl

This file was deleted.

17 changes: 2 additions & 15 deletions ext/PythonCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,11 @@ function JolinPluto.start_python_thread(func)
end



const _python_module_where_plutoscript_is_included = Ref{Py}()

JolinPluto.init_jolin(python_globals::PyDict) = JolinPluto.init_jolin(Py(python_globals))
function JolinPluto.init_jolin(python_globals::Py)
_python_module_where_plutoscript_is_included[] = python_globals
nothing
end
pyglobals() = get!(PythonCall.pydict, PythonCall.MODULE_GLOBALS, Main)

JolinPluto.lang_enabled(::Val{:py}) = true
function JolinPluto.lang_copy_bind(::Val{:py}, def, value)
_python_module_where_plutoscript_is_included[][String(def)] = value
pyglobals()[][string(def)] = value
end

# function __init__()
# # this is not calling jolin_init, as jolin_init may extend to do further things next to initializing the module
# # e.g. in PlutoR it will also set variables
# _python_module_where_plutoscript_is_included[] = get!(PythonCall.pydict, PythonCall.Core.MODULE_GLOBALS, Main)
# end

end
33 changes: 2 additions & 31 deletions ext/RCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,18 @@ function JolinPluto.ChannelWithRepeatedFill(get_next_value::RCall.RObject, args.
end
end


# RCall's calling syntax does not support arbitrary types, but is good with functions
"""
HTML("<h1> HTML String </h1>")
"""
function _HTML(args...; kwargs...)
HTML(args...; kwargs...)
end

const _r_module_where_plutoscript_is_included = Ref{RCall.RObject{RCall.EnvSxp}}()

function JolinPluto.init_jolin(r_environment::RCall.RObject{RCall.EnvSxp})
_r_module_where_plutoscript_is_included[] = r_environment

r_environment[:format_html] = JolinPluto.format_html
# Markdown and HTML support should be there out of the box
# CommonMark is used, because the standard Markdown does not support html strings inside markdown string.
# (within Julia itself the object interpolation works, because everything is stored as julia objects and only finally transformed to html.)
r_environment[:MD] = JolinPluto.MD
r_environment[:HTML] = _HTML

r_environment[Symbol(".bond")] = JolinPluto.bond
RCall.reval("bond <- function(var, ui) .bond(sys.call()[[2]], ui)", r_environment)
nothing
end
# c(MD, HTML, format_html, viewof) %<-% julia_eval("Jolin.MD, Jolin._HTML, Jolin.format_html, Jolin.viewof")

JolinPluto.lang_enabled(::Val{:r}) = true
function JolinPluto.lang_copy_bind(::Val{:r}, def, value)
function JolinPluto.lang_copy_bind(::Val{:r}, def::Symbol, value)
RCall.Const.GlobalEnv[def] = value
end


function __init__()

# this is crucial so that the path is set correctly
# while PythonCall does this by itself, RCall needs this manual help,
# which effects both plain Julia with RCall as well as PlutoR
CondaPkg.activate!(ENV)

_r_module_where_plutoscript_is_included[] = RCall.Const.GlobalEnv
end


end # module
5 changes: 2 additions & 3 deletions src/JolinPluto.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ export @output_below, @clipboard_image_to_clipboard_html
export output_below, clipboard_image_to_clipboard_html, embedLargeHTML, plotly_responsive
export Setter, @get, @cell_ids_create_wrapper, @cell_ids_push!
export cell_ids_create_wrapper, cell_ids_push!, cell_ids_push
export MD, format_html
export bond
export init_jolin
export MD, format_html, _HTML
export viewof

using Dates, HTTP, JSON3, Git, JWTs, UUIDs, Base64
using HypertextLiteral, Continuables
Expand Down
8 changes: 8 additions & 0 deletions src/frontend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ function MD(args...; kwargs...)
end


# RCall's calling syntax does not support arbitrary types, but is good with functions
"""
HTML("<h1> HTML String </h1>")
"""
function _HTML(args...; kwargs...)
HTML(args...; kwargs...)
end

"""
format_html(anything)
Expand Down
25 changes: 12 additions & 13 deletions src/languages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ end

"""
```julia
bond(symbol, element)
bond("symbol", element)
viewof(:symbol, element)
viewof("symbol", element)
```
Return the HTML `element`, and use its latest JavaScript value as the definition of `symbol`.
# Example
```julia
bond(:x, html"<input type=range>")
viewof(:x, html"<input type=range>")
```
and in another cell:
```julia
Expand All @@ -42,7 +42,7 @@ x^2
The first cell will show a slider as the cell's output, ranging from 0 until 100.
The second cell will show the square of `x`, and is updated in real-time as the slider is moved.
"""
function bond(def, ui)
function viewof(def, ui)
if !isa(def, Symbol)
throw(ArgumentError("""\nMacro example usage: \n\n\t@bind my_number html"<input type='range'>"\n\n"""))
elseif !isdefined(Main, :PlutoRunner)
Expand All @@ -52,23 +52,22 @@ function bond(def, ui)
b -> missing
end
initial_value = Core.applicable(Base.get, ui) ? Base.get(ui) : initial_value_getter(ui)
setproperty(_module_where_plutoscript_is_included[], def, initial_value)
copy_bind_to_registered_languages(def, initial_value)
# It seems we need to hardcode the support in here
return ui
# setproperty(_module_where_plutoscript_is_included[], def, )
# copy_bind_to_registered_languages(def, initial_value)
return initial_value, ui
else
Main.PlutoRunner.load_integrations_if_needed()
initial_value_getter = Main.PlutoRunner.initial_value_getter_ref[](ui)
initial_value = Core.applicable(Base.get, ui) ? Base.get(ui) : initial_value_getter(ui)
setproperty!(Main.PlutoRunner.currently_running_module[], def, initial_value)
copy_bind_to_registered_languages(def, initial_value)
return Main.PlutoRunner.create_bond(ui, def, Main.PlutoRunner.currently_running_cell_id[])
# setproperty!(Main.PlutoRunner.currently_running_module[], def, initial_value)
# copy_bind_to_registered_languages(def, initial_value)
return initial_value, Main.PlutoRunner.create_bond(ui, def, Main.PlutoRunner.currently_running_cell_id[])
end
end

# for python and R especially
# (python strings are automatically transformed to Julia strings in JuliaCall when calling julia functions from python)
# (same for R strings)
function bond(def::AbstractString, ui)
bond(Symbol(def), ui)
function viewof(def::AbstractString, ui)
viewof(Symbol(def), ui)
end

0 comments on commit 9a9236f

Please sign in to comment.