Commit fd4cf9de authored by Philippe Veber's avatar Philippe Veber
Browse files

tk/Dataframe: slightly more flexible API for html rendering

parent f3da8080
......@@ -140,12 +140,15 @@ let from_file ?(header = `Read_in_file) path =
let cols = List.zip_exn labels cols in
Ok { nrows ; ncols ; cols }
let to_html ?(cell_renderers = []) d =
type html_formatter =
int -> string -> Html_types.td_content Tyxml.Html.elt
let to_html ?(formatters = []) d =
let open Tyxml.Html in
let default_cell s = txt s in
let default_cell _ s = txt s in
let cols = List.map d.cols ~f:(fun (label, col) ->
let cell_renderer =
List.Assoc.find cell_renderers ~equal:String.equal label
List.Assoc.find formatters ~equal:String.equal label
|> Option.value ~default:default_cell
in
label, cell_renderer, col
......@@ -167,7 +170,7 @@ let to_html ?(cell_renderers = []) d =
| String_opts t -> Option.value ~default t.(i)
in
let row i =
List.map cols ~f:(fun (_, renderer, col) -> td [renderer (elem col i)])
List.map cols ~f:(fun (_, renderer, col) -> td [renderer i (elem col i)])
|> tr
in
table ~thead (List.init (nrows d) ~f:row)
......@@ -24,10 +24,10 @@ val from_file :
string ->
(t, [> `Msg of string]) result
type html_formatter =
int -> string -> Html_types.td_content Tyxml.Html.elt
val to_html :
?cell_renderers:
(string *
(string -> [< Html_types.td_content > `PCDATA] Tyxml.Html.elt))
list ->
?formatters:(string * html_formatter) list ->
t ->
[> Html_types.table ] Tyxml.Html.elt
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment