huri icon indicating copy to clipboard operation
huri copied to clipboard

add function to show data in browser

Open behrica opened this issue 8 years ago • 5 comments

I started to use huri and it's idea of a "data-set is sequences of maps" and one of the first things I wanted to add, for the "layz data scientist", is to show a "sequence of maps in a browser".

The code for thjis looks like this:

(defn df-to-browser! [df]
  (data-to-html.core/open-in-browser
   (hiccup.core/html
    (hiccup.table/to-table1d df
                             (zipmap (hc/cols df) (map name (hc/cols df))))))

  )

So it would add depencies to hiccup and friends:

                [data-to-html "0.1.3-SNAPSHOT"]
                 [hiccup-table "0.2.0"]

Let me know, if you think this could fit here, so I do a PR

Something similar could be done using "clojure.inspect" (while I like to html look better)

behrica avatar Sep 18 '17 08:09 behrica

Tables are definitely something that should be added. I actually have a rather provisional implementation built on top of (Datatables)[https://datatables.net/] that we used internally at GoOpti.

Here's the code if you can lift any ideas from it. PR very welcome.

(defn table
  ([df]
   (table {} df))
  ([opts df]        
   (let [{:keys [limit sort-by pagination? header-column? columns filtering?]
          :or {limit 5
               pagination? true
               filtering? false}} opts
         id (name (gensym "df"))
         headers (zipmap (cols df) (range))         
         order (let [[sort-col sort-direction] (-> sort-by
                                                   (or (ffirst headers))
                                                   ensure-seq)]
                 (format "order: [[%s, '%s']]" (safe-get headers sort-col)
                         (name (or sort-direction :asc))))
         pagination (format "bPaginate: %s" pagination?)
         info (format "info: %s" pagination?)
         filtering (format "bFilter: %s" filtering?)
         dt-options (s/join "," [order pagination info filtering])
         columns (or columns (keys headers))]
     (->> (list [:table.stripe.hover.cell-border {:id id}
                 [:thead
                  [:tr (for [h columns]
                         [:th (name h)])]]
                 [:tbody (for [row (if (= limit :all)
                                     df
                                     (take limit df))]
                           [:tr (map-indexed (fn [i cell]
                                               [(if (and header-column?
                                                         (zero? i))
                                                  :th
                                                  :td) cell])
                                             ((apply juxt columns) row))])]]
                (javascript-tag (format "$('#%s').DataTable({%s});"
                                        id dt-options)))
          html
          html-view))))

sbelak avatar Sep 18 '17 14:09 sbelak

I hacked as well an other version based on datatables. But more static, without any options.

Will look at yours know.

behrica avatar Sep 18 '17 15:09 behrica

I could not find function html-view. Just to be sure I use the smae, can you point me to it ?

behrica avatar Sep 18 '17 15:09 behrica

https://github.com/JonyEpsilon/gorilla-repl/blob/v0.3.5/src/gorilla_repl/html.clj

Looks like Gorilla changed a bit in the last year. I was using a custom version and wasn't tracking changes he was making.

sbelak avatar Sep 18 '17 15:09 sbelak

Ok, I see.

It's for rendering the table as part of Gorilla repl.

I use huri currently without Gorilla. Main reason is that current Gorilla repl and huri don't work together due to old dependencies of Gorilla. see #7

If I look at the upper "table" function, it would be idenpendent of Gorilla, removing the last line.

My first use case would be to have a function which produced from a "sequence of maps" a STANDALONE html file, which I could then render in a "(open-in-browser html)" function.

So maybe the right approach here could be (given the potential complexity of data table configuration) to take your code from above and make it return just the hicupp data structure, so removing the last two lines. Maybe call it "df->hiccup" or "df->dt" or similar.

This could then be reused for both cases:

  • produce standalone html file and open-in-browser
  • render in gorilla

The first function would then probbaly not be part of huri, the second yes.

What do you think ?

behrica avatar Sep 18 '17 18:09 behrica