Skip to content

Instantly share code, notes, and snippets.

@rmoehn
Created October 22, 2016 04:50
Show Gist options
  • Save rmoehn/5daf562df44d51cdbcb25aff5bf55c65 to your computer and use it in GitHub Desktop.
Save rmoehn/5daf562df44d51cdbcb25aff5bf55c65 to your computer and use it in GitHub Desktop.
Clojure is the new Perl – Loading Markdown files into Jupyter Notebooks/IPython Notebooks using Specter and Cheshire
(ns user
(:require [clojure.java.io :as io]
[clojure.string :as string]
[cheshire.core :as cheshire]
[com.rpl.specter :as s]))
;;; Dependencies: [org.clojure/clojure "1.9.0-alpha13"]
;;; [cheshire "5.6.3"]
;;; [com.rpl/specter "0.13.0"]]
;; Credits: https://github.com/nathanmarz/specter/blob/master/src/clj/com/rpl/specter/impl.cljc
(s/defnav s-butlast []
(select* [this structure next-fn] (next-fn (butlast structure)))
(transform* [this structure next-fn]
(let [structurev (vec structure)
newpart (next-fn (vec (butlast structure)))
res (concat newpart (list (last structurev)))]
(if (vector? structure)
(vec res)
res))))
(defn md-line-seq [md-dir cmd]
(let [[_ md-name] (re-matches #"%load_md ([^.].*\.md)" (first cmd))]
(->> (io/file md-dir md-name)
slurp
string/split-lines
(s/transform [s-butlast s/ALL]
#(str % \newline)))))
;; p … path
(defn subst-md [nb-p md-dir-p out-p]
(let [orig (cheshire/parse-string (slurp nb-p) true)
proc (->> orig
(s/transform
[:cells s/ALL :source #(re-matches #"%load_md [^.].*\.md"
(or (first %) ""))]
(partial md-line-seq md-dir-p)))]
(spit out-p (cheshire/generate-string proc {:pretty true}))))
(comment
(subst-md "../notebooks/OffSwitchCartPole.ipynb" "../interruptibility"
"../notebooks/ProcessedOSCP.ipynb")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment