On Thu, Sep 24, 2015 at 12:00 AM, Nick Dokos <ndo...@gmail.com> wrote:

> Matt Price <mopto...@gmail.com> writes:
>
> > I would, however, like to avoid the clumsy intermediate step and use
> something like this instead:
> > #+name: graph-from-tables
> > #+HEADER: :var nodes=students-table graph=students-graph horiz='t
> > #+BEGIN_SRC emacs-lisp :file ~/example-diagram.png :colnames yes
> :exports results
> >   (org-babel-execute:dot
> >    (concat
> >     "graph {\n"
> >     (when horiz "rankdir=LR;\n")       ;up-down or left-right
> >     (mapconcat
> >      (lambda (x)
> >        (format "%s [label=\"%s\" shape=%s style=\"filled\"
> fillcolor=\"%s\"]"
> >                (car x)
> >                (nth 1 x)
> >                (if (string= "" (nth 2 x)) "box" (nth 2 x))
> >                (if (string= "" (nth 3 x)) "none" (nth 3 x))
> >                )) nodes "\n")
> >     "\n"
> >     (mapconcat
> >     (lambda (x)
> >       (format "%s -- %s;"
> >               (car x) (nth 1 x) )) graph "\n")
> >     "}\n") params)
> > #+END_SRC
> >
>
> There are a couple of problems, one minor (the :colnames yes part
> truncates the graph so you have only two edges), and one major: the
> "params" argument is not defined.
>
> When a dot source block is passed to org-babel-execute:dot, the params
> argument I get [fn:1] is:
>
> ((:comments . #1="") (:shebang . #1#) (:cache . "no") (:padline . #1#)
>  (:noweb . "no") (:tangle . "no") (:exports . "results")
>  (:results . "file replace") (:session . "none") (:hlines . "no")
>  (:file . "Images/foobar.png") (:result-type . value)
>  (:result-params "file" "replace") (:rowname-names) (:colname-names))
>
> So I tried passing it to org-babel-execute:dot and somewhat
> to my surprise it worked [fn:2] - try evaluating the following in your
> *scratch* buffer (make sure there is an Images subdir under the
> current directory of that buffer):
>
> --8<---------------cut here---------------start------------->8---
>
> (org-babel-execute:dot
>  "graph {
> rankdir=LR;
> a [label=\"Omar\" shape=ellipse style=\"filled\" fillcolor=\"green\"]
> b [label=\"Hindia\" shape=ellipse style=\"filled\" fillcolor=\"orange\"]
> c [label=\"Yuvrai\" shape=ellipse style=\"filled\" fillcolor=\"purple\"]
> a -- b;
> a -- c;
> b -- c;
> }
> "
>  '((:comments . #1="") (:shebang . #1#) (:cache . "no") (:padline . #1#)
>  (:noweb . "no") (:tangle . "no") (:exports . "results")
>  (:results . "file replace") (:session . "none") (:hlines . "no")
>  (:file . "Images/foobar.png") (:result-type . value)
>  (:result-params "file" "replace") (:rowname-names) (:colname-names)))
> --8<---------------cut here---------------end--------------->8---
>
> But I would hardly call this method less clumsy than your earlier
> attempt.
>
> Footnotes:
>
> [fn:1]  I made a simple dot source block with your graph
>
> #+BEGIN_SRC dot :file Images/foobar.png
> graph {
> rankdir=LR;
> a [label="Omar" shape=ellipse style="filled" fillcolor="green"]
> b [label="Hindia" shape=ellipse style="filled" fillcolor="orange"]
> c [label="Yuvrai" shape=ellipse style="filled" fillcolor="purple"]
> a -- b;
> a -- c;
> b -- c;
> }
> #+END_SRC
>
> and instrumented org-babel-execute:dot under edebug - when I executed the
> code
> block, the debugger kicked in when the function got called and I could
> get the "params" argument.
>
> [fn:2] I thought the #1 and #1# constructs (which I don't understand at
>        all) would make it blow up.
>
> --
> Nick
>
> Many thanks, Nick, and especially to the pointer to edebug (so obvious
once you say it!).  With my original code, I stepped through the whole
function and I can't quite understand why it's not working. The code block
text is generated as expected, but somehow it's not getting written, or
perhaps not executed by dot, as hoped.  Here is the debugging output:

(nodes (quote (("a" "Omar" "ellipse" "green") ("b" "Hindia" "ellipse"
"orange") ("c" "Yuvrai" "ellipse" "purple"))))

(graph (quote (("a" "b") ("a" "c"))))

(horiz (quote t))

funcall-interactively: Buffer is read-only: #<buffer ob-dot.el> [4 times]

Result: ((:comments . #1="") (:shebang . #1#) (:cache . "no") (:padline .
#1#) (:noweb . "no") (:tangle . "no") (:exports . "results") (:results .
"file replace") (:var nodes ("a" "Omar" "ellipse" "green") ("b" "Hindia"
"ellipse" "orange") ("c" "Yuvrai" "ellipse" "purple")) (:var graph ("a"
"b") ("a" "c")) (:var horiz . t) (:colnames . "yes") (:file .
"~/example-diagram.png") (:hlines . "no") (:session . "none") (:result-type
. value) (:result-params "file" "replace") (:rowname-names) (:colname-names
(nodes "*node*" "*label*" "*shape*" "*fillcolor*") (graph "From" "To")))

Result: (:result-params "file" "replace")

Result: ("file" "replace")

Stop

Result: ((:comments . #1="") (:shebang . #1#) (:cache . "no") (:padline .
#1#) (:noweb . "no") (:tangle . "no") (:exports . "results") (:results .
"file replace") (:var nodes ("a" "Omar" "ellipse" "green") ("b" "Hindia"
"ellipse" "orange") ("c" "Yuvrai" "ellipse" "purple")) (:var graph ("a"
"b") ("a" "c")) (:var horiz . t) (:colnames . "yes") (:file .
"~/example-diagram.png") (:hlines . "no") (:session . "none") (:result-type
. value) (:result-params "file" "replace") (:rowname-names) (:colname-names
(nodes "*node*" "*label*" "*shape*" "*fillcolor*") (graph "From" "To")))
Stop

Result: (:file . "~/example-diagram.png")
Stop

Result: (:file . "~/example-diagram.png")
Stop

Result: "~/example-diagram.png"

Result: ((:comments . #1="") (:shebang . #1#) (:cache . "no") (:padline .
#1#) (:noweb . "no") (:tangle . "no") (:exports . "results") (:results .
"file replace") (:var nodes ("a" "Omar" "ellipse" "green") ("b" "Hindia"
"ellipse" "orange") ("c" "Yuvrai" "ellipse" "purple")) (:var graph ("a"
"b") ("a" "c")) (:var horiz . t) (:colnames . "yes") (:file .
"~/example-diagram.png") (:hlines . "no") (:session . "none") (:result-type
. value) (:result-params "file" "replace") (:rowname-names) (:colname-names
(nodes "*node*" "*label*" "*shape*" "*fillcolor*") (graph "From" "To")))
Stop

Result: nil
Stop

Result: nil
Stop

Stop

Result: "~/example-diagram.png"

Result: "png"

Result: "-Tpng"

Result: "-Tpng"

Result: ((:comments . #1="") (:shebang . #1#) (:cache . "no") (:padline .
#1#) (:noweb . "no") (:tangle . "no") (:exports . "results") (:results .
"file replace") (:var nodes ("a" "Omar" "ellipse" "green") ("b" "Hindia"
"ellipse" "orange") ("c" "Yuvrai" "ellipse" "purple")) (:var graph ("a"
"b") ("a" "c")) (:var horiz . t) (:colnames . "yes") (:file .
"~/example-diagram.png") (:hlines . "no") (:session . "none") (:result-type
. value) (:result-params "file" "replace") (:rowname-names) (:colname-names
(nodes "*node*" "*label*" "*shape*" "*fillcolor*") (graph "From" "To")))
Stop

Result: nil
Stop

Result: nil
Stop

Result: "dot"

Result: "/tmp/babel-2749DTL/dot-2749Nkt"

Result: "/tmp/babel-2749DTL/dot-2749Nkt"

Result: "graph {\nrankdir=LR;\na [label=\"Omar\" shape=ellipse
style=\"filled\" fillcolor=\"green\"]\nb [label=\"Hindia\" shape=ellipse
style=\"filled\" fillcolor=\"orange\"]\nc [label=\"Yuvrai\" shape=ellipse
style=\"filled\" fillcolor=\"purple\"]\na -- b;\na -- c;}\n"
Stop

Result: ((:comments . #1="") (:shebang . #1#) (:cache . "no") (:padline .
#1#) (:noweb . "no") (:tangle . "no") (:exports . "results") (:results .
"file replace") (:var nodes ("a" "Omar" "ellipse" "green") ("b" "Hindia"
"ellipse" "orange") ("c" "Yuvrai" "ellipse" "purple")) (:var graph ("a"
"b") ("a" "c")) (:var horiz . t) (:colnames . "yes") (:file .
"~/example-diagram.png") (:hlines . "no") (:session . "none") (:result-type
. value) (:result-params "file" "replace") (:rowname-names) (:colname-names
(nodes "*node*" "*label*" "*shape*" "*fillcolor*") (graph "From" "To")))
Stop

Result: "graph {\nrankdir=LR;\na [label=\"Omar\" shape=ellipse
style=\"filled\" fillcolor=\"green\"]\nb [label=\"Hindia\" shape=ellipse
style=\"filled\" fillcolor=\"orange\"]\nc [label=\"Yuvrai\" shape=ellipse
style=\"filled\" fillcolor=\"purple\"]\na -- b;\na -- c;}\n"
Stop

Result: nil
Stop

Result: nil
Stop

Stop

Stop

Result: "dot"

Result: "/tmp/babel-2749DTL/dot-2749Nkt"

Result: "/tmp/babel-2749DTL/dot-2749Nkt"

Result: "-Tpng"

Stop

Result: "~/example-diagram.png"

Result: "/home/matt/example-diagram.png"
Stop

Result: "dot /tmp/babel-2749DTL/dot-2749Nkt -Tpng -o
/home/matt/example-diagram.png"
Stop

Wrote /tmp/babel-2749DTL/ob-input-2749auz
Result: ""

Result: nil

Code block evaluation complete.
Mark set

-----
it looks about right until the very end; Manually running "dot
/tmp/babel-2749DTL/dot-2749Nkt -Tpng -o /home/matt/example-diagram.png"
from a shell works fine. However, the result of:

(org-babel-eval
     (concat cmd
         " " (org-babel-process-file-name in-file)
         " " cmdline
         " -o " (org-babel-process-file-name out-file)) "")

is "nil".  Any idea why that might be?

Again, many thanks,
Matt

Reply via email to