Skip to content

Commit

Permalink
made the support for options argument kind-specific (configurable)
Browse files Browse the repository at this point in the history
  • Loading branch information
daslu committed Dec 23, 2023
1 parent 133f8e2 commit 0a96cce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 46 deletions.
6 changes: 4 additions & 2 deletions dev/scicloj/kindly/gen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
(str
" ([value] (hide-code (attach-kind-to-value value " kind-kw "))) ")
(str
" ([value] (attach-kind-to-value value " kind-kw ")) ")) \newline
" ([value options] (" kind " [options value]))"
" ([value] (attach-kind-to-value value " kind-kw ")) "))
(when (:support-options attrs)
(str \newline
" ([value options] (" kind " [options value]))"))
")" \newline)))

(defn kind-fns [all-kinds]
Expand Down
9 changes: 6 additions & 3 deletions resources/kinds.edn
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@
[echarts {:display-as "a chart"
:example [["a" "b" "c" "d"]
[1 2 3 4]]
:docs "https://echarts.apache.org/en/option.html"}]
:docs "https://echarts.apache.org/en/option.html"
:support-options true}]
[cytoscape {:display-as "a graph"
:example {:nodes #{1 2 3 4 5}
:edges #{[1 2] [3 5] [4 2] [4 3]}}
:docs "https://js.cytoscape.org/#notation/elements-json"
:json-schema "https://raw.githubusercontent.com/AZaitzeff/cytoscape_js_schema/main/cytoscape_schema.json"}]
:json-schema "https://raw.githubusercontent.com/AZaitzeff/cytoscape_js_schema/main/cytoscape_schema.json"
:support-options true}]
[plotly {:display-as "a plot"
:example [{:x [1, 2, 3, 4, 5],
:y [1, 2, 4, 8, 16]}]
Expand Down Expand Up @@ -135,7 +137,8 @@
["other recursive structures"
[[table {:display-as "a table"
:example {:headers [:a]
:rows [{:a 1} {:a 2}]}}]
:rows [{:a 1} {:a 2}]}
:support-options true}]
[portal {:display-as "portal"
:example {:key1 "value1",
:key2 ^{:kindly/kind :kind/hiccup} [:div [:h3 "Hello " [:em "World"]]]}}]]]]
66 changes: 25 additions & 41 deletions src/scicloj/kindly/v4/kind.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
"display-as: a formatted string
example: {:key1 \"value1\", :key2 \"value2\"}"
([] :kind/pprint)
([value] (attach-kind-to-value value :kind/pprint))
([value options] (pprint [options value])))
([value] (attach-kind-to-value value :kind/pprint)) )

(defn hidden
"display-as: do not display
example: [\"SECRET\"]"
([] :kind/hidden)
([value] (attach-kind-to-value value :kind/hidden))
([value options] (hidden [options value])))
([value] (attach-kind-to-value value :kind/hidden)) )


;; ## web dev
Expand All @@ -26,22 +24,19 @@ example: [\"SECRET\"]"
"display-as: HTML
example: <div><h3>Hello ><em>World</em></h3><div>"
([] :kind/html)
([value] (attach-kind-to-value value :kind/html))
([value options] (html [options value])))
([value] (attach-kind-to-value value :kind/html)) )

(defn hiccup
"display-as: HTML
example: [:div [:h3 \"Hello \" [:em \"World\"]]]"
([] :kind/hiccup)
([value] (attach-kind-to-value value :kind/hiccup))
([value options] (hiccup [options value])))
([value] (attach-kind-to-value value :kind/hiccup)) )

(defn reagent
"display-as: A reagent component inside HTML
example: [(fn [] [:button {:on-click (fn [ev] (js/alert \"You pressed it\"))} \"Press me\"])]"
([] :kind/reagent)
([value] (attach-kind-to-value value :kind/reagent))
([value options] (reagent [options value])))
([value] (attach-kind-to-value value :kind/reagent)) )


;; ## data visualization formats
Expand All @@ -51,39 +46,36 @@ example: [(fn [] [:button {:on-click (fn [ev] (js/alert \"You pressed it\"))} \"
example: ## Hello *World*
hide-code: true"
([] :kind/md)
([value] (hide-code (attach-kind-to-value value :kind/md)))
([value options] (md [options value])))
([value] (hide-code (attach-kind-to-value value :kind/md))) )

(defn code
"display-as: the code only, not the result
example: (+ 1 2)
hide-code: true"
([] :kind/code)
([value] (hide-code (attach-kind-to-value value :kind/code)))
([value options] (code [options value])))
([value] (hide-code (attach-kind-to-value value :kind/code))) )

(defn vega
"display-as: a chart
example: {:description \"A basic bar chart example, with value labels shown upon pointer hover.\", :axes [{:orient \"bottom\", :scale \"xscale\"} {:orient \"left\", :scale \"yscale\"}], :width 400, :scales [{:name \"xscale\", :type \"band\", :domain {:data \"table\", :field \"category\"}, :range \"width\", :padding 0.05, :round true} {:name \"yscale\", :domain {:data \"table\", :field \"amount\"}, :nice true, :range \"height\"}], :padding 5, :marks [{:type \"rect\", :from {:data \"table\"}, :encode {:enter {:x {:scale \"xscale\", :field \"category\"}, :width {:scale \"xscale\", :band 1}, :y {:scale \"yscale\", :field \"amount\"}, :y2 {:scale \"yscale\", :value 0}}, :update {:fill {:value \"steelblue\"}}, :hover {:fill {:value \"red\"}}}} {:type \"text\", :encode {:enter {:align {:value \"center\"}, :baseline {:value \"bottom\"}, :fill {:value \"#333\"}}, :update {:x {:scale \"xscale\", :signal \"tooltip.category\", :band 0.5}, :y {:scale \"yscale\", :signal \"tooltip.amount\", :offset -2}, :text {:signal \"tooltip.amount\"}, :fillOpacity [{:test \"datum === tooltip\", :value 0} {:value 1}]}}}], :signals [{:name \"tooltip\", :value {}, :on [{:events \"rect:pointerover\", :update \"datum\"} {:events \"rect:pointerout\", :update \"{}\"}]}], :height 200, :data [{:name \"table\", :values [{:category \"A\", :amount 28} {:category \"B\", :amount 55} {:category \"C\", :amount 43} {:category \"D\", :amount 91} {:category \"E\", :amount 81} {:category \"F\", :amount 53} {:category \"G\", :amount 19} {:category \"H\", :amount 87}]}]}
docs: https://vega.github.io/vega/docs/
json-schema: https://vega.github.io/schema/vega/v5.json"
([] :kind/vega)
([value] (attach-kind-to-value value :kind/vega))
([value options] (vega [options value])))
([value] (attach-kind-to-value value :kind/vega)) )

(defn vega-lite
"display-as: VegaLite chart
example: {:description \"A simple bar chart with embedded data.\", :data {:values [{\"a\" \"A\", \"b\" 28} {\"a\" \"B\", \"b\" 55} {\"a\" \"C\", \"b\" 43} {\"a\" \"D\", \"b\" 91} {\"a\" \"E\", \"b\" 81} {\"a\" \"F\", \"b\" 53} {\"a\" \"G\", \"b\" 19} {\"a\" \"H\", \"b\" 87} {\"a\" \"I\", \"b\" 52}]}, :mark \"bar\", :encoding {\"x\" {\"field\" \"a\", \"type\" \"nominal\", \"axis\" {\"labelAngle\" 0}}, \"y\" {\"field\" \"b\", \"type\" \"quantitative\"}}}
docs: https://vega.github.io/vega-lite/docs/
json-schema: https://vega.github.io/schema/vega-lite/v5.json"
([] :kind/vega-lite)
([value] (attach-kind-to-value value :kind/vega-lite))
([value options] (vega-lite [options value])))
([value] (attach-kind-to-value value :kind/vega-lite)) )

(defn echarts
"display-as: a chart
example: [[\"a\" \"b\" \"c\" \"d\"] [1 2 3 4]]
docs: https://echarts.apache.org/en/option.html"
docs: https://echarts.apache.org/en/option.html
support-options: true"
([] :kind/echarts)
([value] (attach-kind-to-value value :kind/echarts))
([value options] (echarts [options value])))
Expand All @@ -92,7 +84,8 @@ docs: https://echarts.apache.org/en/option.html"
"display-as: a graph
example: {:nodes #{1 4 3 2 5}, :edges #{[4 3] [4 2] [1 2] [3 5]}}
docs: https://js.cytoscape.org/#notation/elements-json
json-schema: https://raw.githubusercontent.com/AZaitzeff/cytoscape_js_schema/main/cytoscape_schema.json"
json-schema: https://raw.githubusercontent.com/AZaitzeff/cytoscape_js_schema/main/cytoscape_schema.json
support-options: true"
([] :kind/cytoscape)
([value] (attach-kind-to-value value :kind/cytoscape))
([value options] (cytoscape [options value])))
Expand All @@ -103,8 +96,7 @@ example: [{:x [1 2 3 4 5], :y [1 2 4 8 16]}]
docs: https://plotly.com/javascript/getting-started/
json-schema: https://plotly.com/chart-studio-help/json-chart-schema/"
([] :kind/plotly)
([value] (attach-kind-to-value value :kind/plotly))
([value options] (plotly [options value])))
([value] (attach-kind-to-value value :kind/plotly)) )


;; ## specific types
Expand All @@ -113,16 +105,14 @@ json-schema: https://plotly.com/chart-studio-help/json-chart-schema/"
"display-as: an image
example: https://raw.githubusercontent.com/scicloj/graphic-design/live/icons/Kindly.svg"
([] :kind/image)
([value] (attach-kind-to-value value :kind/image))
([value options] (image [options value])))
([value] (attach-kind-to-value value :kind/image)) )

(defn dataset
"display-as: a table
example: (->> (System/getProperties) (map (fn [[k v]] {:k k, :v (apply str (take 40 (str v)))})) (tech.v3.dataset/->>dataset {:dataset-name \"My Truncated System Properties\"}))
docs: https://github.com/techascent/tech.ml.dataset"
([] :kind/dataset)
([value] (attach-kind-to-value value :kind/dataset))
([value options] (dataset [options value])))
([value] (attach-kind-to-value value :kind/dataset)) )


;; ## clojure specific
Expand All @@ -131,15 +121,13 @@ docs: https://github.com/techascent/tech.ml.dataset"
"display-as: the name of a var
example: (def testvar 100)"
([] :kind/var)
([value] (attach-kind-to-value value :kind/var))
([value options] (var [options value])))
([value] (attach-kind-to-value value :kind/var)) )

(defn test
"display-as: success or failure
example: (deftest unity-test (is (= 1 1)))"
([] :kind/test)
([value] (attach-kind-to-value value :kind/test))
([value options] (test [options value])))
([value] (attach-kind-to-value value :kind/test)) )


;; ## plain structures
Expand All @@ -148,36 +136,33 @@ example: (deftest unity-test (is (= 1 1)))"
"display-as: a sequence
example: (range 5)"
([] :kind/seq)
([value] (attach-kind-to-value value :kind/seq))
([value options] (seq [options value])))
([value] (attach-kind-to-value value :kind/seq)) )

(defn vector
"display-as: a sequence
example: (vec (range 5))"
([] :kind/vector)
([value] (attach-kind-to-value value :kind/vector))
([value options] (vector [options value])))
([value] (attach-kind-to-value value :kind/vector)) )

(defn set
"display-as: a bag
example: (set (range 5))"
([] :kind/set)
([value] (attach-kind-to-value value :kind/set))
([value options] (set [options value])))
([value] (attach-kind-to-value value :kind/set)) )

(defn map
"display-as: associated values
example: {:key1 \"value1\", :key2 \"value2\"}"
([] :kind/map)
([value] (attach-kind-to-value value :kind/map))
([value options] (map [options value])))
([value] (attach-kind-to-value value :kind/map)) )


;; ## other recursive structures

(defn table
"display-as: a table
example: {:headers [:a], :rows [{:a 1} {:a 2}]}"
example: {:headers [:a], :rows [{:a 1} {:a 2}]}
support-options: true"
([] :kind/table)
([value] (attach-kind-to-value value :kind/table))
([value options] (table [options value])))
Expand All @@ -186,6 +171,5 @@ example: {:headers [:a], :rows [{:a 1} {:a 2}]}"
"display-as: portal
example: {:key1 \"value1\", :key2 [:div [:h3 \"Hello \" [:em \"World\"]]]}"
([] :kind/portal)
([value] (attach-kind-to-value value :kind/portal))
([value options] (portal [options value])))
([value] (attach-kind-to-value value :kind/portal)) )

0 comments on commit 0a96cce

Please sign in to comment.