Skip to content

Commit

Permalink
added hide-code to the API; updated code generation to make code hi…
Browse files Browse the repository at this point in the history
…ding configurable by `kinds.edn`
  • Loading branch information
daslu committed Dec 23, 2023
1 parent 371c704 commit b1d3a5c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 93 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).

## [4-alpha9-SNAPHSOT]
- added `hide-code` to the API
- updated code generation to make code hiding configurable by `kinds.edn`

## [4-alpha8] - 2023-12-12
- hiding code for some kinds

Expand Down
18 changes: 16 additions & 2 deletions dev/scicloj/kindly/gen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
(str (name k) ": " (escape v))))
"\"" \newline
" ([] " kind-kw ")" \newline
" ([value] (attach-kind-to-value value " kind-kw ")))" \newline)))
(if (:hide-code attrs)
(str
" ([value] (hide-code (attach-kind-to-value value " kind-kw "))) ")
(str
" ([value] (attach-kind-to-value value " kind-kw ")) "))
")" \newline)))

(defn kind-fns [all-kinds]
(str/join (str \newline \newline)
Expand All @@ -42,7 +47,7 @@
(defn kind-ns [all-kinds]
(str "(ns scicloj.kindly.v4.kind
\"Kinds for visualization\"
(:require [scicloj.kindly.v4.api :refer [attach-kind-to-value]])
(:require [scicloj.kindly.v4.api :refer [attach-kind-to-value hide-code]])
(:refer-clojure :exclude " (excludes all-kinds) "))
" (kind-fns all-kinds) \newline))
Expand Down Expand Up @@ -72,6 +77,15 @@
(vary-meta value assoc :kindly/kind kind)
(attach-kind-to-value [value] kind)))
(defn hide-code
\"Annotate whether the code of this value should be hidden\"
([value]
(hide-code value true))
([value bool]
(if (instance? clojure.lang.IObj value)
(vary-meta value assoc :kindly/hide-code true)
(hide-code [value]))))
(defn consider
\"Prefer using the functions in the kind namespace instead\"
[value kind]
Expand Down
118 changes: 60 additions & 58 deletions resources/kinds.edn
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,66 @@
"Press me"])]}]]]

["data visualization formats"
[[md {:display-as "a Markdown string"
:example "## Hello *World*"}]
[code {:display-as "the code only, not the result"
:example (+ 1 2)}]
[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}]}]}
[[md {:display-as "a Markdown string"
:example "## Hello *World*"
:hide-code true}]
[code {:display-as "the code only, not the result"
:example (+ 1 2)
:hide-code true}]
[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"}]
[vega-lite {:display-as "VegaLite chart"
Expand Down
9 changes: 9 additions & 0 deletions src/scicloj/kindly/v4/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
(vary-meta value assoc :kindly/kind kind)
(attach-kind-to-value [value] kind)))

(defn hide-code
"Annotate whether the code of this value should be hidden"
([value]
(hide-code value true))
([value bool]
(if (instance? clojure.lang.IObj value)
(vary-meta value assoc :kindly/hide-code true)
(hide-code [value]))))

(defn consider
"Prefer using the functions in the kind namespace instead"
[value kind]
Expand Down
64 changes: 31 additions & 33 deletions src/scicloj/kindly/v4/kind.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns scicloj.kindly.v4.kind
"Kinds for visualization"
(:require [scicloj.kindly.v4.api :refer [attach-kind-to-value]])
(:require [scicloj.kindly.v4.api :refer [attach-kind-to-value hide-code]])
(:refer-clojure :exclude [test seq vector set map]))

;; ## simple behaviours
Expand All @@ -9,93 +9,90 @@
"display-as: a formatted string
example: {:key1 \"value1\", :key2 \"value2\"}"
([] :kind/pprint)
([value] (attach-kind-to-value value :kind/pprint)))
([value] (attach-kind-to-value value :kind/pprint)) )

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


;; ## web dev

(defn html
"display-as: HTML
example: <div><h3>Hello ><em>World</em></h3><div>"
example: <div><h3>Hello ><em>World</em></h3><div>"
([] :kind/html)
([value] (attach-kind-to-value value :kind/html)))
([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] (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] (attach-kind-to-value value :kind/reagent)) )


;; ## data visualization formats

(defn md
"display-as: a Markdown string
example: ## Hello *World*"
example: ## Hello *World*
hide-code: true"
([] :kind/md)
([value] (-> value
(attach-kind-to-value :kind/md)
(vary-meta
assoc :kindly/hide-code? true))))
([value] (hide-code (attach-kind-to-value value :kind/md))) )

(defn code
"display-as: the code only, not the result
example: (+ 1 2)"
example: (+ 1 2)
hide-code: true"
([] :kind/code)
([value] (-> value
(attach-kind-to-value :kind/code)
(vary-meta
assoc :kindly/hide-code? true))))
([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] (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] (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"
([] :kind/echarts)
([value] (attach-kind-to-value value :kind/echarts)))
([value] (attach-kind-to-value value :kind/echarts)) )

(defn cytoscape
"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"
([] :kind/cytoscape)
([value] (attach-kind-to-value value :kind/cytoscape)))
([value] (attach-kind-to-value value :kind/cytoscape)) )

(defn plotly
"display-as: a plot
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] (attach-kind-to-value value :kind/plotly)) )


;; ## specific types
Expand All @@ -104,14 +101,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] (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] (attach-kind-to-value value :kind/dataset)) )


;; ## clojure specific
Expand All @@ -120,13 +117,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] (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] (attach-kind-to-value value :kind/test)) )


;; ## plain structures
Expand All @@ -135,25 +132,25 @@ 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] (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] (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] (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] (attach-kind-to-value value :kind/map)) )


;; ## other recursive structures
Expand All @@ -162,10 +159,11 @@ example: {:key1 \"value1\", :key2 \"value2\"}"
"display-as: a table
example: {:headers [:a], :rows [{:a 1} {:a 2}]}"
([] :kind/table)
([value] (attach-kind-to-value value :kind/table)))
([value] (attach-kind-to-value value :kind/table)) )

(defn portal
"display-as: portal
example: {:key1 \"value1\", :key2 [:div [:h3 \"Hello \" [:em \"World\"]]]}"
([] :kind/portal)
([value] (attach-kind-to-value value :kind/portal)))
([value] (attach-kind-to-value value :kind/portal)) )

0 comments on commit b1d3a5c

Please sign in to comment.