Skip to content

Commit

Permalink
Inspect non-utf8 binaries (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko authored Oct 31, 2023
1 parent 69941f7 commit 433ce0c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/kino/explorer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,18 @@ defmodule Kino.Explorer do
end

defp records_to_data(columns, records) do
Enum.map(columns, fn column -> Map.fetch!(records, column.key) |> Enum.map(&to_string/1) end)
Enum.map(columns, fn column ->
records |> Map.fetch!(column.key) |> Enum.map(&value_to_string(column.type, &1))
end)
end

defp value_to_string("binary", value) do
inspect_opts = Inspect.Opts.new([])
if String.printable?(value, inspect_opts.limit), do: value, else: inspect(value)
end

defp value_to_string(_type, value) do
to_string(value)
end

defp summaries(df, groups) do
Expand Down Expand Up @@ -194,6 +205,7 @@ defmodule Kino.Explorer do

defp type_of(:boolean, _), do: "boolean"
defp type_of(:string, [data]), do: type_of_sample(data)
defp type_of(:binary, _), do: "binary"
defp type_of(_, _), do: "text"

defp type_of_sample("http" <> _rest), do: "uri"
Expand Down
15 changes: 15 additions & 0 deletions test/kino/explorer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ defmodule Kino.ExplorerTest do
} = data
end

test "correctly data frames with binary non-utf8 column values" do
df =
Explorer.DataFrame.new([x: [1, 2], y: [<<110, 120>>, <<200, 210>>]], dtypes: [y: :binary])

widget = Kino.Explorer.new(df)
data = connect(widget)

assert %{
features: [:export, :pagination, :sorting],
content: %{
data: [["1", "2"], ["nx", "<<200, 210>>"]]
}
} = data
end

test "supports lazy data frames" do
df = Explorer.Datasets.iris() |> Explorer.DataFrame.lazy()
widget = Kino.Explorer.new(df)
Expand Down

0 comments on commit 433ce0c

Please sign in to comment.