Run code in orgmode source blocks
With Babel you can easily execute code for a variety of languages from within orgmode.
To configure Babel for the languages I wanted to execute, I put the following in my .emacs.d/init.el
config.
;; Configure babel for these languages
(org-babel-do-load-languages
'org-babel-load-languages
'((python . t)
(shell . t)
(dot . t)
(org . t)
(scheme . t)
))
;; run code blocks without prompting first
(setq org-confirm-babel-evaluate nil)
Then make a new .org file and try running a snippet in the file.
#+BEGIN_SRC python :results output
for x in range(0,3):
print("Hello world")
#+END_SRC
Execute the code using C-c C-c (Control-c Control-c) with the cursor in the SRC block.
The results are included in the file like magic.
#+RESULTS:
: Hello world
: Hello world
: Hello world
Graphviz
It's particularly handy for Graphviz. You get a nicely interactive way to draw Graphs.
Execute the block the same way with C-c C-c and you get a link to the graph.
#+BEGIN_SRC dot :file graphviz.png
digraph architecture {
node [color=green] [shape=box]; "A"
node [color=blue] [shape=oval]; "B", "C"
"A" -> "B";
"B" -> "C";
"C" -> "A";
"C" -> "D";
"D" -> "E";
"E" -> "B";
}
#+END_SRC
#+RESULTS:
[[file:graphviz.png]]
/posts/orgmode-babel-graphviz.png