How to "force" a Pluto recalculation

Hello

It would help me to “force” a recalculation of a Pluto Notebook.

This is simply because I use the PrettyTables.jl package.
It displays dataframes as “old fashion” tables.
It makes uses of ENV[“LINES”] to determine the maximum number of lines to be displayed.
Therefore, in my Pluto notebook, I created a slider to set this variable (see below).
However, this does not trigger a recalculation and nothing changes.
I could of course run each cell individually to refresh them :grin:.
I could also include a reference to maxrows in all these cells.

But it would be easier for me to force a full recalculation …

Would you know how to do that?
If it make sense …

Thanks,

Michel

Create a slider to chose the maximum number of lines

@bind maxrows Slider(1:40, show_value=true, default=10)

Set the env variable:

begin
	ENV["LINES"] = maxrows
	md"""**Maximum rows to display in tables ($maxrows)**"""
end
1 Like

Ctrl-a followed by Shift-Enter; see here: https://github.com/fonsp/Pluto.jl/issues/556

2 Likes

Thanks mbaz, this is already useful and simple !

However, I would like to do that automatically in Julia, not manually.
Is there a function that triggers a recalculation?

Thanks
Michel

You can try to put ENV at the begining of the cell you want to refresh so pluto knows that this cell depends on ENV:

begin
    ENV
    do_something_with_PrettyTables
end
1 Like

Yes indeed, this is a solution.
The advantage is also that I could chose which cells to refresh and avoid useless refresh of others.
Inconvenient is that I g-have to take care …
and also that this wouls need at least a comment in the code, to remember why this line …

Thanks

You could also exploit javascript to do that automatically, I think knowing a bit of JS can really open up loads of possibilities in Pluto.

For example, putting a cell with the following code inside:

html"""
<script>
	// Get current cell handle and ID
	let cell = currentScript.closest('pluto-cell')
	let id = cell.getAttribute('id')
	// Find all cells below the current one and extract their ids
	let cells_below = document.querySelectorAll(`pluto-cell[id='${id}'] ~ pluto-cell`)
	let cells_below_ids = [...cells_below].map((el) => el.getAttribute('id'))
	// Use the pluto internal function to re-run all selected cells
	cell._internal_pluto_actions.set_and_run_multiple(cells_below_ids)
	</script>
"""

Triggers a re-run of all cells below it in the notebook upon execution.
You can easily do a more complex filtering of which cells you want to re-run and also put the statement at the end of a cell with a let ... end block to have it automatically re-run depending on other julia variables. Similar to the previous example:

let
    ENV
   html"""
<script>
	// Get current cell handle and ID
	let cell = currentScript.closest('pluto-cell')
	let id = cell.getAttribute('id')
	// Find all cells below the current one and extract their ids
	let cells_below = document.querySelectorAll(`pluto-cell[id='${id}'] ~ pluto-cell`)
	let cells_below_ids = [...cells_below].map((el) => el.getAttribute('id'))
	// Use the pluto internal function to re-run all selected cells
	cell._internal_pluto_actions.set_and_run_multiple(cells_below_ids)
	</script>
"""
end

The important thing is that the html""" """ macro is the last command of the cell so its output is what is sent to the pluto cell output. If you have it in the middle of a let or begin block, no javascript output is sent to the cell output and nothing happens.

3 Likes

Good idea !
I guess this could be wrapped into a function that updates the maxrows (ENV) variable !
(for the first version)
Thanks
Michel