From 9a9236feb837fc5b7d4c21f9a785a0e9b7e95c8c Mon Sep 17 00:00:00 2001 From: Stephan Sahm Date: Thu, 11 Jul 2024 23:00:06 +0200 Subject: [PATCH] rethinking bind --- Project.toml | 3 +-- ext/CondaPkgExt.jl | 5 ----- ext/PythonCallExt.jl | 17 ++--------------- ext/RCallExt.jl | 33 ++------------------------------- src/JolinPluto.jl | 5 ++--- src/frontend.jl | 8 ++++++++ src/languages.jl | 25 ++++++++++++------------- 7 files changed, 27 insertions(+), 69 deletions(-) delete mode 100644 ext/CondaPkgExt.jl diff --git a/Project.toml b/Project.toml index 0a93ed4..9a70099 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "JolinPluto" uuid = "5b0b4ef8-f4e6-4363-b674-3f031f7b9530" authors = ["Stephan Sahm and contributors"] -version = "0.1.76" +version = "0.1.77" [deps] AbstractPlutoDingetjes = "6e696c72-6542-2067-7265-42206c756150" @@ -29,7 +29,6 @@ AWSExt = "AWS" PlotsExt = "Plots" PythonCallExt = "PythonCall" RCallExt = ["RCall", "CondaPkg"] -CondaPkgExt = "CondaPkg" [compat] julia = "1.7" diff --git a/ext/CondaPkgExt.jl b/ext/CondaPkgExt.jl deleted file mode 100644 index db8316a..0000000 --- a/ext/CondaPkgExt.jl +++ /dev/null @@ -1,5 +0,0 @@ -module CondaPkgExt -import CondaPkg -function __init__() -end -end \ No newline at end of file diff --git a/ext/PythonCallExt.jl b/ext/PythonCallExt.jl index f706172..2a21b24 100644 --- a/ext/PythonCallExt.jl +++ b/ext/PythonCallExt.jl @@ -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 \ No newline at end of file diff --git a/ext/RCallExt.jl b/ext/RCallExt.jl index 7e19bc9..054aec2 100644 --- a/ext/RCallExt.jl +++ b/ext/RCallExt.jl @@ -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("

HTML String

") -""" -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 \ No newline at end of file diff --git a/src/JolinPluto.jl b/src/JolinPluto.jl index c211e84..ddb6a03 100644 --- a/src/JolinPluto.jl +++ b/src/JolinPluto.jl @@ -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 diff --git a/src/frontend.jl b/src/frontend.jl index 0df93e1..860ace5 100644 --- a/src/frontend.jl +++ b/src/frontend.jl @@ -172,6 +172,14 @@ function MD(args...; kwargs...) end +# RCall's calling syntax does not support arbitrary types, but is good with functions +""" + HTML("

HTML String

") +""" +function _HTML(args...; kwargs...) + HTML(args...; kwargs...) +end + """ format_html(anything) diff --git a/src/languages.jl b/src/languages.jl index 119e2ab..df42a4c 100644 --- a/src/languages.jl +++ b/src/languages.jl @@ -23,8 +23,8 @@ 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`. @@ -32,7 +32,7 @@ Return the HTML `element`, and use its latest JavaScript value as the definition # Example ```julia -bond(:x, html"") +viewof(:x, html"") ``` and in another cell: ```julia @@ -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""\n\n""")) elseif !isdefined(Main, :PlutoRunner) @@ -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