Packages
livebook
0.19.5
0.19.8
0.19.7
0.19.6
0.19.5
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.6
0.18.5
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
0.17.3
0.17.2
0.17.1
0.17.0
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.7
0.14.6
0.14.5
0.14.4
0.14.3
0.14.2
0.14.1
0.14.0
0.14.0-rc.1
0.14.0-rc.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
Automate code & data workflows with interactive notebooks
Current section
Files
Jump to
Current section
Files
lib/livebook/notebook/cell.ex
defmodule Livebook.Notebook.Cell do
# Data structure representing a single cell in a notebook.
#
# Cell is the smallest structural unit in a notebook, in other words
# it is a block. Depending on the cell type, it may consist of text
# content, outputs or a specific UI.
alias Livebook.Utils
alias Livebook.Notebook.Cell
@type id :: Utils.id()
@type t :: Cell.Markdown.t() | Cell.Code.t() | Cell.Smart.t()
@type type :: :markdown | :code | :smart
@type indexed_output :: {non_neg_integer(), Livebook.Runtime.output()}
@setup_cell_id_prefix "setup"
@setup_cell_id "setup"
@doc """
Returns an empty cell of the given type.
"""
@spec new(type()) :: t()
def new(type)
def new(:markdown), do: Cell.Markdown.new()
def new(:code), do: Cell.Code.new()
def new(:smart), do: Cell.Smart.new()
@doc """
Returns an atom representing the type of the given cell.
"""
@spec type(t()) :: type()
def type(cell)
def type(%Cell.Code{}), do: :code
def type(%Cell.Markdown{}), do: :markdown
def type(%Cell.Smart{}), do: :smart
@doc """
Checks if the given cell can be evaluated.
"""
@spec evaluable?(t()) :: boolean()
def evaluable?(cell)
def evaluable?(%Cell.Code{}), do: true
def evaluable?(%Cell.Smart{}), do: true
def evaluable?(_cell), do: false
@doc """
Extracts all inputs from the given indexed output.
"""
@spec find_inputs_in_output(indexed_output()) :: list(Livebook.Runtime.input_output())
def find_inputs_in_output(output)
def find_inputs_in_output({_idx, %{type: :input} = input}) do
[input]
end
def find_inputs_in_output({_idx, %{type: :control, attrs: %{type: :form, fields: fields}}}) do
for {_field, input} <- fields, input != nil, do: input
end
def find_inputs_in_output({_idx, output}) when output.type in [:frame, :tabs, :grid] do
Enum.flat_map(output.outputs, &find_inputs_in_output/1)
end
def find_inputs_in_output({_idx, %{type: :frame_update, update: {_update_type, new_outputs}}}) do
Enum.flat_map(new_outputs, &find_inputs_in_output/1)
end
def find_inputs_in_output(_output), do: []
@doc """
Extracts all asset infos from the given non-indexed output.
"""
@spec find_assets_in_output(Livebook.Runtime.output()) :: list(asset_info :: map())
def find_assets_in_output(output)
def find_assets_in_output(%{type: :js} = output), do: [output.js_view.assets]
def find_assets_in_output(output) when output.type in [:frame, :tabs, :grid] do
Enum.flat_map(output.outputs, &find_assets_in_output/1)
end
def find_assets_in_output(%{type: :frame_update, update: {_update_type, new_outputs}}) do
Enum.flat_map(new_outputs, &find_assets_in_output/1)
end
def find_assets_in_output(_output), do: []
@doc """
Checks if the given cell is any of the setup code cells.
"""
@spec setup?(t()) :: boolean()
def setup?(cell)
def setup?(%Cell.Code{id: @setup_cell_id_prefix <> _}), do: true
def setup?(_cell), do: false
@doc """
The fixed identifier of the main setup cell.
"""
@spec main_setup_cell_id() :: id()
def main_setup_cell_id(), do: @setup_cell_id
@doc """
The identifier of extra setup cell for the given language.
"""
@spec extra_setup_cell_id(atom()) :: id()
def extra_setup_cell_id(language), do: "#{@setup_cell_id_prefix}-#{language}"
end