# How to "force" a Pluto recalculation

**URL:** <https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532>\
**Category:** Pluto\
**Tags:** question\
**Created:** [May 5, 2022, 11:11am UTC](https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532 "2022-05-05T11:11:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 5, 2022, 11:11am UTC](https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532/1 "2022-05-05T11:11:55Z")

</div>

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 😁.  
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**

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

```

**Set the env variable:**

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

```

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [May 5, 2022, 1:01pm UTC](https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532/2 "2022-05-05T13:01:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 5, 2022, 8:29pm UTC](https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532/3 "2022-05-05T20:29:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dictino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dictino/32/217281_2.png) [@Dictino](https://discourse.julialang.org/u/Dictino)\
**Post date:** [May 7, 2022, 9:08pm UTC](https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532/4 "2022-05-07T21:08:12Z")

</div>

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:

```julia
begin
    ENV
    do_something_with_PrettyTables
end

```

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 8, 2022, 9:18am UTC](https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532/5 "2022-05-08T09:18:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [May 8, 2022, 10:52am UTC](https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532/6 "2022-05-08T10:52:20Z")

</div>

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:

```julia
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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![maajdl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maajdl/32/22838_2.png) [@maajdl](https://discourse.julialang.org/u/maajdl)\
**Post date:** [May 8, 2022, 8:47pm UTC](https://discourse.julialang.org/t/how-to-force-a-pluto-recalculation/80532/7 "2022-05-08T20:47:22Z")

</div>

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