Skip to content

Commit

Permalink
[Fix #118] Display Video output with Kindly
Browse files Browse the repository at this point in the history
Issue: Wolfram may return `(Video <path to file> ...)` and we want to be able to display it nicely in a Clay or compatible notebook.

Fix: Add ns with a fn to try add display metadata to all results, call it from eval.
  • Loading branch information
holyjak committed Oct 1, 2024
1 parent 906b969 commit d628350
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
;; `env CLOJARS_USERNAME=<tbd> CLOJARS_PASSWORD=<clojars-token> clojure -T:build deploy`
{:deps {io.github.clojure/tools.build {:git/tag "v0.10.4" :git/sha "31388ff"}
slipset/deps-deploy {:mvn/version "0.2.2"}
org.scicloj/clay {:mvn/version "2-beta15"}
org.scicloj/clay {:mvn/version "2-beta17"} ; for the build
;; We need wolframite + JLink to render Wolf code in the docs
org.scicloj/wolframite {:local/root "."}
wolfram/jlink {:local/root "./symlink-jlink.jar"}} ; FYI jlink only needed for build-site
Expand Down
4 changes: 3 additions & 1 deletion src/wolframite/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
[wolframite.base.package :as package]
[wolframite.base.parse :as parse]
[wolframite.impl.jlink-instance :as jlink-instance]
[wolframite.impl.kindly-support :as kindly-support]
[wolframite.impl.protocols :as proto]
[wolframite.runtime.defaults :as defaults]
[wolframite.runtime.jlink :as jlink]
Expand Down Expand Up @@ -192,7 +193,8 @@
(:opts jlink-inst)
eval-opts)
expr' (if (string? expr) (express/express expr with-eval-opts) expr)]
(cep/cep expr' with-eval-opts))
(some-> (cep/cep expr' with-eval-opts)
kindly-support/maybe-add-kindly-meta))
(throw (IllegalStateException. "Not initialized, call start! first")))))

;; TODO Should we expose this, or will just folks shoot themselves in the foot with it?
Expand Down
20 changes: 20 additions & 0 deletions src/wolframite/impl/kindly_support.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns wolframite.impl.kindly-support
"Add Kindly annotations to eval results to improve their display in compatible tools, such as Clay")

(defn- head [expr]
(when (list? expr)
(first expr)))

(defn- video->url
"Extract the url from `(Video \"url..\" ...)`"
[expr]
{:pre [(= (head expr) 'Video)]}
(second expr))

(defn maybe-add-kindly-meta [expr]
{:pre [expr]}
(->> (cond
(= (head expr) 'Video)
{:kind/video true
:kindly/options {:kindly/f video->url}})
(with-meta expr)))
12 changes: 12 additions & 0 deletions test/wolframite/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@
(wl/eval "Map[Function[{x}, x + 1],{1,3}]")
(wl/eval (w/Map (w/fn [x] (w/+ x 1)) [1 3]))))))

(deftest kindly-support
(wl/start!)
(let [res (wl/eval '(Video "file:///my-fake.mps"))
{:keys [kindly/options] :as m}
(meta res)
view-fn (:kindly/f options)]
(is (= 'Video (first res)) "Sanity check: expect Wolfram to return (Video ..)")
(is (:kind/video m))
(is (= "file:///my-fake.mps" (view-fn res))
"Correct metadata is attached, including view fn to extract the video url")))


(comment
(wl/->wl (w/Map (w/fn [] (w/+ (w/Slot 1) 1)) [1 3]))
(wl/eval (w/Map (w/fn [x] (w/Plus x 1)) [1 2 3]))
Expand Down

0 comments on commit d628350

Please sign in to comment.