Skip to content

Commit

Permalink
fix IPyWidget support
Browse files Browse the repository at this point in the history
  • Loading branch information
schlichtanders committed Sep 11, 2024
1 parent 871e71e commit 1017456
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion 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.82"
version = "0.1.83"

[deps]
AbstractPlutoDingetjes = "6e696c72-6542-2067-7265-42206c756150"
Expand Down
15 changes: 13 additions & 2 deletions ext/PythonCallExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ JolinPluto.IPyWidget_init() = @htl """
window.require(["@jupyter-widgets/base"], (function(b) {
b.WidgetView.prototype.touch = function(){
let div = this.el.closest('div[type="application/vnd.jupyter.widget-view+div"]')
if (this.model._buffered_state_diff?.value != null){
div.value = this.model._buffered_state_diff.value
if ((div != null) && (this.model._buffered_state_diff?.value != null)){
div.value = this.model._buffered_state_diff.value
div.dispatchEvent(new CustomEvent('input'))
}
}
Expand Down Expand Up @@ -164,6 +164,7 @@ function AbstractPlutoDingetjes.Bonds.initial_value(w::JolinPluto.IPyWidget)
end

function pyshow_rule_ipywidgets(io::IO, mime::String, x::Py)
mime == "text/html" || return false
pyissubclass(pytype(x), @pyconst(pyimport("ipywidgets").widgets.ValueWidget)) || return false
try
show(io, mime, JolinPluto.IPyWidget(x))
Expand All @@ -177,6 +178,16 @@ function pyshow_rule_ipywidgets(io::IO, mime::String, x::Py)
end
end

# auto convert ipywidgets inside viewof, as the ui element is not shown
JolinPluto.viewof(def::AbstractString, ui::Py) = JolinPluto.viewof(Symbol(def), ui)
function JolinPluto.viewof(def::Symbol, ui::Py)
if pyissubclass(pytype(ui), @pyconst(pyimport("ipywidgets").widgets.ValueWidget))
@invoke JolinPluto.viewof(def::Symbol, IPyWidget(ui)::Any)
else
@invoke JolinPluto.viewof(def::Symbol, ui::Any)
end
end

function __init__()
# improve support for ipywidgets
if isdefined(PythonCall, :Compat) # older versions of PythonCall don't have the Compat submodule
Expand Down
2 changes: 1 addition & 1 deletion src/viewof.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function viewof(def, ui)
return initial_value, ui
else
Main.PlutoRunner.load_integrations_if_needed()
initial_value_getter = Main.PlutoRunner.initial_value_getter_ref[](ui)
initial_value_getter = Main.PlutoRunner.initial_value_getter_ref[]
initial_value = Core.applicable(Base.get, ui) ? Base.get(ui) : initial_value_getter(ui)
return initial_value, Main.PlutoRunner.create_bond(ui, def, Main.PlutoRunner.currently_running_cell_id[])
end
Expand Down

0 comments on commit 1017456

Please sign in to comment.