livebook/lib/livebook_web/live/output.ex

308 lines
8.2 KiB
Elixir
Raw Normal View History

defmodule LivebookWeb.Output do
2023-02-23 02:34:54 +08:00
use LivebookWeb, :html
import LivebookWeb.Helpers
alias LivebookWeb.Output
@doc """
Renders a list of cell outputs.
"""
def outputs(assigns) do
~H"""
2023-02-23 02:34:54 +08:00
<div
:for={{idx, output} <- Enum.reverse(@outputs)}
class="max-w-full"
id={"output-wrapper-#{@dom_id_map[idx] || idx}"}
data-el-output
data-border={border?(output)}
>
<%= render_output(output, %{
id: "output-#{idx}",
session_id: @session_id,
session_pid: @session_pid,
input_values: @input_values,
client_id: @client_id
}) %>
</div>
"""
end
defp border?({:stdout, _text}), do: true
defp border?({:text, _text}), do: true
defp border?({:error, _message, _type}), do: true
2022-08-30 19:15:55 +08:00
defp border?({:grid, _, info}), do: Map.get(info, :boxed, false)
defp border?(_output), do: false
defp render_output({:stdout, text}, %{id: id}) do
text = if(text == :__pruned__, do: nil, else: text)
live_component(Output.StdoutComponent, id: id, text: text)
end
defp render_output({:text, text}, %{id: id}) do
assigns = %{id: id, text: text}
~H"""
<Output.TextComponent.render id={@id} content={@text} />
"""
end
defp render_output({:markdown, markdown}, %{id: id, session_id: session_id}) do
live_component(Output.MarkdownComponent,
id: id,
session_id: session_id,
content: markdown
)
end
defp render_output({:image, content, mime_type}, %{id: id}) do
assigns = %{id: id, content: content, mime_type: mime_type}
~H"""
<Output.ImageComponent.render id={@id} content={@content} mime_type={@mime_type} />
"""
end
defp render_output({:js, js_info}, %{id: id, session_id: session_id, client_id: client_id}) do
live_component(LivebookWeb.JSViewComponent,
id: id,
js_view: js_info.js_view,
session_id: session_id,
client_id: client_id,
timeout_message: "Output data no longer available, please reevaluate this cell"
)
end
defp render_output({:frame, outputs, _info}, %{
id: id,
session_id: session_id,
session_pid: session_pid,
input_values: input_values,
client_id: client_id
}) do
live_component(Output.FrameComponent,
id: id,
outputs: outputs,
session_id: session_id,
session_pid: session_pid,
input_values: input_values,
client_id: client_id
)
end
2022-08-04 06:24:15 +08:00
defp render_output({:tabs, outputs, info}, %{
id: id,
session_id: session_id,
session_pid: session_pid,
input_values: input_values,
client_id: client_id
2022-08-04 06:24:15 +08:00
}) do
{labels, active_idx} =
2022-08-30 19:15:55 +08:00
if info.labels == :__pruned__ do
2022-08-04 06:24:15 +08:00
{[], nil}
else
labels =
Enum.zip_with(info.labels, outputs, fn label, {output_idx, _} -> {output_idx, label} end)
active_idx = get_in(outputs, [Access.at(0), Access.elem(0)])
{labels, active_idx}
end
assigns = %{
id: id,
active_idx: active_idx,
labels: labels,
outputs: outputs,
session_id: session_id,
session_pid: session_pid,
input_values: input_values,
client_id: client_id
2022-08-04 06:24:15 +08:00
}
# After pruning we don't render labels and we render only those
# outputs that are kept during pruning
~H"""
<div id={@id}>
<div class="tabs mb-2" id={"#{@id}-tabs"} phx-update="append">
2023-02-23 02:34:54 +08:00
<button
:for={{output_idx, label} <- @labels}
id={"#{@id}-tabs-#{output_idx}"}
class={["tab", output_idx == @active_idx && "active"]}
phx-click={
JS.remove_class("active", to: "##{@id}-tabs .tab.active")
|> JS.add_class("active")
|> JS.add_class("hidden", to: "##{@id}-tab-contents > *:not(.hidden)")
|> JS.remove_class("hidden", to: "##{@id}-tab-content-#{output_idx}")
}
>
<%= label %>
</button>
2022-08-04 06:24:15 +08:00
</div>
<div id={"#{@id}-tab-contents"} phx-update="append">
2023-02-23 02:34:54 +08:00
<% # We use data-keep-attribute, because we know active_idx only on the first render %>
<div
:for={{output_idx, output} <- @outputs}
id={"#{@id}-tab-content-#{output_idx}"}
data-tab-content={output_idx}
class={[output_idx != @active_idx && "hidden"]}
data-keep-attribute="class"
>
<.outputs
outputs={[{output_idx, output}]}
dom_id_map={%{}}
session_id={@session_id}
session_pid={@session_pid}
input_values={@input_values}
client_id={@client_id}
/>
</div>
2022-08-04 06:24:15 +08:00
</div>
</div>
"""
end
defp render_output({:grid, outputs, info}, %{
id: id,
session_id: session_id,
session_pid: session_pid,
input_values: input_values,
client_id: client_id
2022-08-04 06:24:15 +08:00
}) do
2022-08-30 19:15:55 +08:00
columns = info[:columns] || 1
gap = info[:gap] || 8
2022-08-04 06:24:15 +08:00
assigns = %{
id: id,
2022-08-30 19:15:55 +08:00
columns: columns,
gap: gap,
2022-08-04 06:24:15 +08:00
outputs: outputs,
session_id: session_id,
session_pid: session_pid,
input_values: input_values,
client_id: client_id
2022-08-04 06:24:15 +08:00
}
~H"""
<div id={@id} class="overflow-auto tiny-scrollbar">
<div
id={"#{@id}-grid"}
2022-08-30 19:15:55 +08:00
class="grid grid-cols-2 w-full"
style={"grid-template-columns: repeat(#{@columns}, minmax(0, 1fr)); gap: #{@gap}px"}
2022-08-04 06:24:15 +08:00
phx-update="append"
>
2023-02-23 02:34:54 +08:00
<div :for={{output_idx, output} <- @outputs} id={"#{@id}-grid-item-#{output_idx}"}>
<.outputs
outputs={[{output_idx, output}]}
dom_id_map={%{}}
session_id={@session_id}
session_pid={@session_pid}
input_values={@input_values}
client_id={@client_id}
/>
</div>
2022-08-04 06:24:15 +08:00
</div>
</div>
"""
end
2023-01-05 04:44:04 +08:00
defp render_output({:input, attrs}, %{
id: id,
input_values: input_values,
session_pid: session_pid,
client_id: client_id
2023-01-05 04:44:04 +08:00
}) do
live_component(Output.InputComponent,
id: id,
attrs: attrs,
input_values: input_values,
session_pid: session_pid,
client_id: client_id
)
end
defp render_output({:control, attrs}, %{
id: id,
input_values: input_values,
session_pid: session_pid,
client_id: client_id
}) do
live_component(Output.ControlComponent,
id: id,
attrs: attrs,
input_values: input_values,
session_pid: session_pid,
client_id: client_id
)
end
defp render_output({:error, formatted, {:missing_secret, secret_name}}, %{
session_id: session_id
}) do
2022-10-04 14:46:55 +08:00
assigns = %{
message: formatted,
secret_name: secret_name,
session_id: session_id
}
~H"""
<div class="-m-4 space-x-4 py-4">
<div
class="flex items-center justify-between border-b px-4 pb-4 mb-4"
style="color: var(--ansi-color-red);"
>
<div class="flex space-x-2 font-editor">
<.remix_icon icon="close-circle-line" />
<span>Missing secret <%= inspect(@secret_name) %></span>
</div>
2023-02-23 02:34:54 +08:00
<.link
patch={~p"/sessions/#{@session_id}/secrets?secret_name=#{@secret_name}"}
class="button-base button-gray"
>
Add secret
</.link>
</div>
<%= render_formatted_error_message(@message) %>
</div>
"""
end
defp render_output({:error, formatted, _type}, %{}) do
render_formatted_error_message(formatted)
end
defp render_output(output, %{}) do
render_error_message("""
Unknown output format: #{inspect(output)}. If you're using Kino,
you may want to update Kino and Livebook to the latest version.
""")
end
defp render_error_message(message) do
assigns = %{message: message}
~H"""
2022-08-02 21:51:02 +08:00
<div
2022-10-07 18:00:39 +08:00
class="whitespace-pre-wrap break-words font-editor text-red-600"
2022-08-02 21:51:02 +08:00
role="complementary"
aria-label="error message"
phx-no-format
><%= @message %></div>
"""
end
defp render_formatted_error_message(formatted) do
assigns = %{message: formatted}
~H"""
<div
2022-10-07 18:00:39 +08:00
class="whitespace-pre-wrap break-words font-editor text-gray-500"
role="complementary"
aria-label="error"
phx-no-format
><%= ansi_string_to_html(@message) %></div>
"""
end
end