Fix reading inputs rendered in the same evaluation (#1531)

This commit is contained in:
Jonatan Kłosko 2022-11-15 15:13:05 +01:00 committed by GitHub
parent 9c9c54de65
commit 42d2386a2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 9 deletions

View file

@ -1018,15 +1018,31 @@ defmodule Livebook.Session.Data do
end
defp add_cell_output({data, _} = data_actions, cell, output) do
data_actions
|> set!(
notebook: Notebook.add_cell_output(data.notebook, cell.id, output),
input_values:
{0, output}
|> Cell.find_inputs_in_output()
|> Map.new(fn attrs -> {attrs.id, attrs.default} end)
|> Map.merge(data.input_values)
)
new_input_values =
{0, output}
|> Cell.find_inputs_in_output()
|> Map.new(fn attrs -> {attrs.id, attrs.default} end)
{data, _} =
data_actions =
data_actions
|> set!(
notebook: Notebook.add_cell_output(data.notebook, cell.id, output),
input_values: Map.merge(new_input_values, data.input_values)
)
if data.cell_infos[cell.id].eval.status == :evaluating do
# When a cell renders an input, it should also be able to read
# that input during the same evaluation, so we make it seem as
# if it already existed prior to the evaluation
data_actions
|> update_cell_eval_info!(cell.id, fn eval_info ->
update_in(eval_info.data.input_values, &Map.merge(new_input_values, &1))
end)
else
data_actions
end
end
defp finish_cell_evaluation(data_actions, cell, section, metadata) do

View file

@ -1819,6 +1819,30 @@ defmodule Livebook.Session.DataTest do
assert {:ok, %{dirty: true}, []} = Data.apply_operation(data, operation)
end
test "stores default values for new inputs in the pre-evaluation data" do
input = %{id: "i1", type: :text, label: "Text", default: "hey"}
data =
data_after_operations!([
{:insert_section, @cid, 0, "s1"},
{:insert_cell, @cid, "s1", 0, :code, "c1", %{}},
{:set_runtime, @cid, connected_noop_runtime()},
evaluate_cells_operations(["setup"]),
{:queue_cells_evaluation, @cid, ["c1"]}
])
operation = {:add_cell_evaluation_output, @cid, "c1", {:input, input}}
assert {:ok,
%{
cell_infos: %{
"c1" => %{
eval: %{data: %{input_values: %{"i1" => "hey"}}}
}
}
}, _} = Data.apply_operation(data, operation)
end
end
describe "apply_operation/2 given :add_cell_evaluation_response" do