diff --git a/lib/kino/explorer.ex b/lib/kino/explorer.ex index d11604a..a2dccb3 100644 --- a/lib/kino/explorer.ex +++ b/lib/kino/explorer.ex @@ -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 @@ -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" diff --git a/test/kino/explorer_test.exs b/test/kino/explorer_test.exs index 984f7f4..cc3c3c1 100644 --- a/test/kino/explorer_test.exs +++ b/test/kino/explorer_test.exs @@ -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)