Skip to content

Commit

Permalink
Update livebook kino formatter example
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jun 10, 2024
1 parent 0157173 commit e05ff91
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions livebook/kino_data_table_formatting.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
```elixir
Mix.install(
[
{:kino, path: "/Users/kip/Development/kino", env: :dev},
{:ex_cldr, path: "/Users/kip/Development/cldr", override: true},
{:kino, github: "kipcole9/kino", env: :dev},
:ex_cldr_dates_times,
:jason
],
Expand All @@ -16,31 +15,33 @@ Mix.install(

## Define a Cldr Backend Module

This livebook uses a Kino DataTable formatter built into `ex_cldr`. As a result we need a Cldr backend module defined. Note that we also need a default locale defined in the configuration above.
This livebook uses a Kino DataTable formatter built into `ex_cldr` from version 2.39.0. As a result we need a Cldr backend module defined. Note that we also need a default locale defined in the configuration above.

```elixir
defmodule MyApp.Cldr do
use Cldr,
locales: [:en, :de, :fr],
locales: [:en, :de, :fr, :ja],
default_locale: :en,
providers: [Cldr.Number, Cldr.DateTime, Cldr.Calendar]
end
```

## Using a configured Kino DataTable formatter

`Cldr.Kino.DataTable.value_to_string/2` is the same as the default implementation with two changes:
[Cldr.Kino.DataTable.value_to_string/2](https://github.com/elixir-cldr/cldr/blob/v2.39.0/lib/cldr/kino/data_table.ex) is the same as the default implementation with two changes:

1. The formatter uses the `Cldr.Char` protocol instead of the `String.Chars` protocol. This is implemented in `ex_cldr` libraries for dates, times, numbers, units of measure, locales.
2. The key `:__column__` is passed when the request is to format a column heading. In the `Cldr.Kino.DataTable.value_to_string/2` implementation, column headings are converted to strings can capitalized.
1. The formatter uses the `Cldr.Chars` protocol instead of the `String.Chars` protocol. This is implemented in `ex_cldr` libraries for dates, times, numbers, units of measure, locales.
2. The key `:__column__` is passed when the request is to format a column heading. In the `Cldr.Kino.DataTable.value_to_string/2` implementation, column headings are converted to strings and capitalized.

Note lastly that there is compiler warning on Elixir 1.17 for `ex_cldr_calendars` - this is fixed on the main branch and will be release when I finalise implementation of the new calendar callbacks.

```elixir
# Change the locale between :fr and :en and the formatting is different.
# Change the locale between :fr, :ja and :en and the formatting is different.
# We use `Cldr.put_default_locale/1` because `Cldr.put_locale/1` sets
# the locale of the *current* process, and the Kino datatable runs in
# a different process and we have no place to pass the locale into that
# process.
Cldr.put_default_locale(:fr)
Cldr.put_default_locale(:en)

data = [
%{
Expand Down

0 comments on commit e05ff91

Please sign in to comment.