From b1d3a5cc34bb6e6c30ad9b4bb63b2f7fa5692bca Mon Sep 17 00:00:00 2001 From: daslu Date: Sat, 23 Dec 2023 14:20:56 +0200 Subject: [PATCH] added `hide-code` to the API; updated code generation to make code hiding configurable by `kinds.edn` --- CHANGELOG.md | 4 ++ dev/scicloj/kindly/gen.clj | 18 ++++- resources/kinds.edn | 118 ++++++++++++++++---------------- src/scicloj/kindly/v4/api.cljc | 9 +++ src/scicloj/kindly/v4/kind.cljc | 64 +++++++++-------- 5 files changed, 120 insertions(+), 93 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a648db9..0da04ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/dev/scicloj/kindly/gen.clj b/dev/scicloj/kindly/gen.clj index cade25c..cc39188 100644 --- a/dev/scicloj/kindly/gen.clj +++ b/dev/scicloj/kindly/gen.clj @@ -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) @@ -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)) @@ -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] diff --git a/resources/kinds.edn b/resources/kinds.edn index dfb5643..a00e8ba 100644 --- a/resources/kinds.edn +++ b/resources/kinds.edn @@ -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" diff --git a/src/scicloj/kindly/v4/api.cljc b/src/scicloj/kindly/v4/api.cljc index d8c44e0..382db0e 100644 --- a/src/scicloj/kindly/v4/api.cljc +++ b/src/scicloj/kindly/v4/api.cljc @@ -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] diff --git a/src/scicloj/kindly/v4/kind.cljc b/src/scicloj/kindly/v4/kind.cljc index e75201c..aa03002 100644 --- a/src/scicloj/kindly/v4/kind.cljc +++ b/src/scicloj/kindly/v4/kind.cljc @@ -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 @@ -9,54 +9,51 @@ "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:

Hello >World

" +example:

Hello >World

" ([] :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 @@ -64,7 +61,7 @@ example: {:description \"A basic bar chart example, with value labels shown upon 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 @@ -72,14 +69,14 @@ example: {:description \"A simple bar chart with embedded data.\", :data {:value 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 @@ -87,7 +84,7 @@ 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 @@ -95,7 +92,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] (attach-kind-to-value value :kind/plotly)) ) ;; ## specific types @@ -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 @@ -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 @@ -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 @@ -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)) ) +