Pluto.jl icon indicating copy to clipboard operation
Pluto.jl copied to clipboard

Update skipped/disabled cell dependency before saving notebook

Open disberd opened this issue 3 years ago • 7 comments

The load_notebook function loaded and re-saved the notebook if not called with a flag to disable writing. Before this PR, since the indirect dependency of disabled and skipped cells was not computed (probably because in most cases when a notebook is loaded is also then somehow executed so the dependency is computed after), the save to file would remove the block comments to all cells that were not directly skipped or disabled, breaking the intended functionality and creating errors when including Pluto notebooks as scripts.

Fixes https://github.com/fonsp/Pluto.jl/issues/2182

I did not put the update functions inside load_notebook_nobackup as they might create some overhead that is not needed in most cases (I computed only 40-50 μs on a fairly large notebook in a simple benchmark)

disberd avatar Aug 05 '22 14:08 disberd

Try this Pull Request!

Open Julia and type:

julia> import Pkg
julia> Pkg.activate(temp=true)
julia> Pkg.add(url="https://github.com/disberd/Pluto.jl", rev="load_notebook_fix")
julia> using Pluto

github-actions[bot] avatar Aug 05 '22 14:08 github-actions[bot]

Without running the notebook the topology might loose dependency information regarding macros and therefore may not be able to re-disable the same cells. Another solution to make sure that the cells loaded as dependent on skipped/disabled are still saved as such is using the information given by the notebook file:

https://github.com/fonsp/Pluto.jl/blob/5bc4a813a5fbc21a915632ef8bca29bbe1785fc9/src/notebook/saving%20and%20loading.jl#L203-L223

Pangoraw avatar Aug 10 '22 12:08 Pangoraw

@Pangoraw, I have seen your referenced commit and I have just one clarification. Do you mean just having a single field to record the depends_on_something on the julia back-end and then distinguishing between what is the cause of the indirect dependence on the front-end? Because the behavior on the front-end is different depending on whether the cell depends on a disabled or skipped one

disberd avatar Aug 10 '22 14:08 disberd

Yes the UI is one problem with my approach, so perhaps there should be a marker for those cells that is disabled on the first run (maybe not ideal) ?

My point is preventing the notebooks from stopping to work as a script when using Symbolics.jl for example with activate_notebook_environment() for example.

Pangoraw avatar Aug 16 '22 14:08 Pangoraw

@Pangoraw, what do you think about a shift in the way we track all these status flags of a cell? Now we have basically 4 different flags for disabled, depends_on_disabled, skipped, depends_on_skipped. I suppose at some point we might want/need to add an additional status. What if we migrate from separate flags to maybe a single field that is a Set of strings or symbols representing a status. That way it's much easier to add potentially additional status and you have a single field in the Cell that contains all currently existing statuses, with checking for a specific field becoming checking for presence of the status name in the Set.

For the specific problem of this PR, we could have an additional generic state which is like :commented_on_file, which would be added whenever something is depending on skipped or disabled, but we could also just create it when loading the notebook as per your suggestion above.

I could try a separate PR for that unless you or others see something that would already be a deal-breaker with this approach. Checking for inclusion in a Set is slower than checking for the value of a specific field, but it's still in the range of 10-20ns in my laptop, which I believe is still significantly faster than needed for the purpose at hand.

disberd avatar Aug 18 '22 15:08 disberd

You're right that it would require a new state for a cell (that can simple be removed when the cell is first run).

What if we migrate from separate flags to maybe a single field that is a Set of strings or symbols representing a status.

What would be the advantage over adding cell fields ? Ultimately, I'd like these properties to be more tied to the topology than the cell itself.

Pangoraw avatar Aug 29 '22 16:08 Pangoraw

I just think that having a single status field is neater and probably easier to maintain (especially if we keep adding flags/status when adding functionality to Pluto) than having a separate bool field for each possible status the cell can have. A potential nice thing would be to like automatically create HTML classes from the status Set/Dict based on the keys that are present rather than independently checking for each single flag explicitly.

disberd avatar Aug 31 '22 09:08 disberd

Let's go with this approach for now, this can be made more robust in the future if needed (recovering the same notebook file). Sorry for the delay and thanks for the work @disberd :tada:

Pangoraw avatar Feb 20 '23 21:02 Pangoraw

~Does this still work with https://github.com/fonsp/Pluto.jl/pull/2303 ? I suspect that the tests in that PR won't pass if you change update_run to update_save_run in the tests, because this PR runs the old algorithm again.~ Never mind it doesn't affect thesave_notebook function. 👍

This PR uses the algorithm that we used before https://github.com/fonsp/Pluto.jl/pull/2303. Is that a problem?

fonsp avatar Feb 28 '23 13:02 fonsp

@disberd @Pangoraw

fonsp avatar Mar 11 '23 10:03 fonsp