livebook-dev / kino

Client-driven interactive widgets for Livebook
Apache License 2.0
361 stars 60 forks source link

Support custom data formatter for Kino.DataTable #440

Closed kipcole9 closed 3 months ago

kipcole9 commented 3 months ago

Currently Kino.DataTable formats data with either the String.Chars implementation or inspect/2. However the default presentation isn't localizable or customisable - and since Livebooks can be targeted at a wider audience than just Elixir developers, an option to provide a formatter function would seem a good improvement to user experience.

The simplest option would be to add an optional formatter function to Kino.DataTable.new/2. For example:

Kino.DataTable.new(
  data,
  keys: [:registered_name, :initial_call, :reductions, :stack_size],
  formatter: &MyMod.formatter/2
)

The formatter function would receive the column value and the key so that formatting can be contextual. The default would be the current value_to_string/1 function, slightly modified to accept (and ignore) the key.

If this idea is accepted I am very willing to create a PR. And of course if there is a better API thats good too.

josevalim commented 3 months ago

:+1:. @cristineguadelupe @jonatanklosko, do you foresee any issues?

kipcole9 commented 3 months ago

I have made the necessary changes in a fork of kino and implemented a livebook demonstrating an example using a formatter I built into a new release of ex_cldr.

Run in Livebook

If you find the overall outcome acceptable I'll submit a PR. I'm unsure how to create tests for this change and I couldn't see any for the current implementation so I hope the example livebook is acceptable.

josevalim commented 3 months ago

I dropped some nits, it looks great to me :)

josevalim commented 3 months ago

Closing in favor of PR.