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 function to show data in browser #9

Open
behrica opened this issue Sep 18, 2017 · 5 comments
Open

add function to show data in browser #9

behrica opened this issue Sep 18, 2017 · 5 comments

Comments

@behrica
Copy link

behrica commented Sep 18, 2017

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)

@sbelak
Copy link
Owner

sbelak commented Sep 18, 2017

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))))

@behrica
Copy link
Author

behrica commented Sep 18, 2017

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

Will look at yours know.

@behrica
Copy link
Author

behrica commented Sep 18, 2017

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

@sbelak
Copy link
Owner

sbelak commented Sep 18, 2017

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.

@behrica
Copy link
Author

behrica commented Sep 18, 2017

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 ?

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

No branches or pull requests

2 participants