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

Add a :formatter option to Kino.DataTable.new/2 #441

Merged
merged 10 commits into from
Jun 12, 2024

Conversation

kipcole9
Copy link
Contributor

See the discusssion in #440.

Adds a :formatter option to Kino.DataTable.new/2 which is a 2-arity function passed the key (column name) and cell value.

The special key :__header__ is passed when formatting the column headings.

Make Kino.DataTable.value_to_string/2 private again
Make default formatter be &value_to_string/2 (no module)
Comment on lines 93 to 96
def update(kino, tabular, opts \\ []) do
{data_rows, data_columns, count, inspected} = prepare_data(tabular, opts)
Kino.Table.update(kino, {data_rows, data_columns, count, inspected})
formatter = Keyword.get(opts, :formatter, &value_to_string/2)
Kino.Table.update(kino, {data_rows, data_columns, count, inspected, formatter})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update is rather designed for updating data and I wouldn't expect formatter to change after the table is created. So let's support it only in new :)

Comment on lines 46 to 50
* `:formatter` - a 2-arity function that is used to format the data
in the table. The first parameter passed is the `key` (column name) and
the second is the value to be formatted. When formatting column headings
the key is the special value `:__header__`. Defaults to the builtin
formatter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change the function to return {:ok, string} | :default. This way if someone wants to customize a specific value, like dates, they can do that without reimplementing the default logic for other values.

With that, the :formatter option wouldn't have a default value, if present we call the formatter first, otherwise we don't, in both cases we follow up with the default logic if applicable.

@jonatanklosko
Copy link
Member

Hey @kipcole9! I like the idea, just a small note on the API and we can ship it :D

@jonatanklosko
Copy link
Member

Ah and as for tests, please add one in test/kino/data_table_test.exs. For example here:

test "correctly paginates sliceable data" do
entries = %{x: 1..30, y: 1..30}
kino = Kino.DataTable.new(entries, keys: [:x, :y])
data = connect(kino)
assert %{
content: %{
columns: [%{key: "0", label: ":x"}, %{key: "1", label: ":y"}],
data: [["1", "1"], ["2", "2"], ["3", "3"] | _] = rows,
total_rows: 30
}
} = data

The data: [...] carries the already formatted values.

@kipcole9
Copy link
Contributor Author

@jonatanklosko, thanks for the review and great suggestions. I've made the requested changes, tests are passing. Now I just need to add a new test as you proposed.

@kipcole9
Copy link
Contributor Author

Test case added so I think this is now ready for the next review.

@jonatanklosko
Copy link
Member

@kipcole9 perfect, last two nitpicks!

@kipcole9
Copy link
Contributor Author

Fixed as requested. Let me know if there's anything else?

Copy link
Member

@jonatanklosko jonatanklosko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, thank you!

@jonatanklosko jonatanklosko merged commit 357ce80 into livebook-dev:main Jun 12, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants