Fix frame rendering (#735)

* Fix frame rendering

* Add test for the dynamic frame
This commit is contained in:
Jonatan Kłosko 2021-12-03 18:59:08 +01:00 committed by GitHub
parent 6de1dd84c2
commit ac6b423e79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 7 deletions

View file

@ -642,8 +642,8 @@ defmodule Livebook.Session do
{:noreply, state}
end
def handle_info({:evaluation_output, cell_id, string}, state) do
operation = {:add_cell_evaluation_output, self(), cell_id, string}
def handle_info({:evaluation_output, cell_id, output}, state) do
operation = {:add_cell_evaluation_output, self(), cell_id, output}
{:noreply, handle_operation(state, operation)}
end

View file

@ -99,10 +99,20 @@ defmodule LivebookWeb.Output do
)
end
defp render_output({:frame_dynamic, pid}, %{id: id, socket: socket, input_values: input_values}) do
defp render_output({:frame_dynamic, pid}, %{
id: id,
socket: socket,
input_values: input_values,
cell_validity_status: cell_validity_status
}) do
live_render(socket, LivebookWeb.Output.FrameDynamicLive,
id: id,
session: %{"id" => id, "pid" => pid, "input_values" => input_values}
session: %{
"id" => id,
"pid" => pid,
"input_values" => input_values,
"cell_validity_status" => cell_validity_status
}
)
end

View file

@ -2,12 +2,27 @@ defmodule LivebookWeb.Output.FrameDynamicLive do
use LivebookWeb, :live_view
@impl true
def mount(_params, %{"pid" => pid, "id" => id, "input_values" => input_values}, socket) do
def mount(
_params,
%{
"pid" => pid,
"id" => id,
"input_values" => input_values,
"cell_validity_status" => cell_validity_status
},
socket
) do
if connected?(socket) do
send(pid, {:connect, self()})
end
{:ok, assign(socket, id: id, output: nil, input_values: input_values)}
{:ok,
assign(socket,
id: id,
output: nil,
input_values: input_values,
cell_validity_status: cell_validity_status
)}
end
@impl true
@ -20,7 +35,8 @@ defmodule LivebookWeb.Output.FrameDynamicLive do
id={"#{@id}-frame"}
socket={@socket}
runtime={nil}
input_values={@input_values} />
input_values={@input_values}
cell_validity_status={@cell_validity_status} />
<% else %>
<div class="text-gray-300">
Empty output frame

View file

@ -115,6 +115,7 @@ defmodule LivebookWeb.SessionLive.CellComponent do
<%= if @cell_view.outputs != [] do %>
<div class="mt-2" data-element="outputs-container">
<%# There is an akin render in LivebookWeb.Output.FrameDynamicLive %>
<LivebookWeb.Output.outputs
outputs={@cell_view.outputs}
id={"cell-#{@cell_view.id}-evaluation#{evaluation_number(@cell_view.evaluation_status, @cell_view.number_of_evaluations)}-outputs"}

View file

@ -214,6 +214,33 @@ defmodule LivebookWeb.SessionLiveTest do
end
end
describe "outputs" do
test "dynamic frame output renders output sent from the frame server",
%{conn: conn, session: session} do
frame_pid =
spawn(fn ->
output = {:text, "Dynamic output in frame"}
receive do
{:connect, pid} -> send(pid, {:connect_reply, %{output: output}})
end
end)
frame_output = {:frame_dynamic, frame_pid}
section_id = insert_section(session.pid)
cell_id = insert_text_cell(session.pid, section_id, :elixir)
# Evaluate the cell
Session.queue_cell_evaluation(session.pid, cell_id)
# Send an additional output
send(session.pid, {:evaluation_output, cell_id, frame_output})
{:ok, view, _} = live(conn, "/sessions/#{session.id}")
assert render(view) =~ "Dynamic output in frame"
end
end
describe "runtime settings" do
test "connecting to elixir standalone updates connect button to reconnect",
%{conn: conn, session: session} do