Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: sample data for empty data #427

Merged
1 commit merged into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/kino/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,10 @@ defmodule Kino.Table do

ctx = assign(ctx, state: state, key_to_string: key_to_string)

{page_length, sample_data} =
case orientation do
:rows -> {length(data), List.first(data)}
:columns -> {hd(data) |> length(), Enum.map(data, &List.first(&1))}
end

has_sample_data = if is_list(sample_data), do: Enum.any?(sample_data)
{page_length, sample_data} = sample_data(data, orientation)

columns =
if has_sample_data do
if sample_data != [] do
sample_data
|> infer_types()
|> Enum.zip_with(columns, fn type, column ->
Expand Down Expand Up @@ -296,4 +290,8 @@ defmodule Kino.Table do
relocates: ctx.assigns.relocates
}
end

defp sample_data([], _), do: {0, []}
defp sample_data(data, :rows), do: {length(data), hd(data)}
defp sample_data(data, :columns), do: {length(hd(data)), Enum.map(data, &List.first(&1))}
end
Loading