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

fixes #9 #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject huri "0.10.0-SNAPSHOT"
(defproject huri "0.11.0-SNAPSHOT"
:description "Tools for the lazy data scientist"
:url "https://github.com/sbelak/huri"
:license {:name "Eclipse Public License"
Expand All @@ -11,4 +11,5 @@
[net.cgrand/xforms "0.9.3"]
[cheshire "5.7.1"]
[com.taoensso/timbre "4.10.0"]
[gorilla-renderable "2.0.0"]])
[gorilla-renderable "2.0.0"]
[hiccup "1.0.5"]])
56 changes: 56 additions & 0 deletions src/huri/datatable.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(ns huri.datatable
(:require [huri.core]
[hiccup.element]
[gorilla-repl.html]))

(defn df->hiccup
"Transforms a dataframe into hiccup, which in a browser renders the data with JQuery datatables"
([df]
(df->hiccup {} 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 (huri.core/cols df) (range))
order (let [[sort-col sort-direction] (-> sort-by
(or (ffirst headers))
(huri.core/ensure-seq))]
(format "order: [[%s, '%s']]" (plumbing.core/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 (clojure.string/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))])]]
(hiccup.element/javascript-tag (format "$('#%s').DataTable({%s});"
id dt-options)))
))))


(defn datatable
"renders a dataframe with datatables in gorilla repl"
([df]
(datatable {} df))
([opts df]
(->> df
(hiccup.core/html)
(df->hiccup)
(gorilla-repl.html/html-view)

))
)