fonsp / Pluto.jl

🎈 Simple reactive notebooks for Julia
https://plutojl.org/
MIT License
4.91k stars 284 forks source link

Reactive re-run does not trigger for function defining HypertextLiteral result #2838

Open lukavdplas opened 4 months ago

lukavdplas commented 4 months ago

I was working on a notebook that creates a function with HypertextLiteral.Result output. This is intended to create an interactive HTML element with Javascript.

A minimal version is something like this:

https://gist.github.com/lukavdplas/ebf28806752fba4cb999e0e3c573dc1a

where there is a cell

function mybutton()
    return @htl """
    <div>
        <button>I'm a button!</button>
        <script>
            var parent = currentScript.parentElement;
            var button = parent.querySelector("button");

            button.addEventListener("click", (e) => {
                button.innerText = "I have been clicked!"
            });
        </script>
    </div>
    """
end

and a cell

@bind a mybutton()

I'm adding this simplified version for the sake of clarity, but I could not replicate the issue there. The actual notebook is this one: https://github.com/lukavdplas/PlutoMapPicker.jl/blob/main/src/map-picker.jl

The linked notebook defines two such functions MapPicker() and MapPickerMultiple(), which follow the same principle as the example above, but with much bigger functions.

The issue I encountered is that when I update the HTML (or Javascript) in the function body of MapPicker() or MapPickerMultiple(), the cell with the function call (@bind place MapPicker()) does not seem to re-run. It still shows the "old" version of the output. I need to manually hit Shift + Enter in the cell with the function call to get it to update.

Strangely enough, the dependency also seems broken in the other direction, The notebook contains a cell that defines a struct TileLayer, that is used in the body of MapPicker() and MapPickerMultiple(), and one "normal" Julia cell. When I rename TileLayer so any references to it should break, the "normal" cell" immediately shows an error, but the cells defining MapPicker() and MapPickerMultiple() don't react until I use Shift + Enter to rerun them.

Another weird issue that may or may not be related is that re-running the cell with the function definition for MapPickerMultiple() seems to somehow cause duplicates in the cell output that shows the docstring:

screenshot of docstring cell output

I encountered this in a different notebook a few weeks ago, which had a similar structure: a function that defines a sizable HypertextLiteral.Result. So it doesn't seem to be a one-off, but both notebooks were written by me, so they might have the same "quirk".

(I am using: Pluto, v0.19.38 / Julia 1.6)

Pangoraw commented 4 months ago

I'm adding this simplified version for the sake of clarity, but I could not replicate the issue there. The actual notebook is this one: https://github.com/lukavdplas/PlutoMapPicker.jl/blob/main/src/map-picker.jl

This can be caused by docstrings + @htl macro (as in https://github.com/fonsp/Pluto.jl/issues/2811) ?

lukavdplas commented 4 months ago

Ah, yes, it looks like the issue reported there is caused by the same underlying issue. I'll check if I can get a minimum working example by combining docstrings + @htl.

lukavdplas commented 4 months ago

Indeed, the combination of docstrings and @htl seems to cause the issue!

Updated MWE

Here is that notebook after I open it:

image

After I change the return value of the function:

image

So the issue is:

Thanks, this at least helped me find a work-around: you can comment out docstrings while working on the body of the function. That's not really a fix, just a tip for anyone else running into the same issue.

fonsp commented 4 months ago

Workaround in https://github.com/JuliaPluto/PlutoDependencyExplorer.jl/pull/9