wiki/text.org-mode.babel.html

72 lines
26 KiB
HTML
Raw Permalink Normal View History

2022-07-29 15:41:17 +00:00
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width"/><meta charSet="utf-8"/><title>The basics of org-babel</title><script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script id="MathJax-script" async="" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script><script type="text/x-mathjax-config">
MathJax = {
tex: {
inlineMath: [ [&#x27;$&#x27;,&#x27;$&#x27;], [&#x27;\(&#x27;,&#x27;\)&#x27;] ],
displayMath: [ [&#x27;$$&#x27;,&#x27;$$&#x27;], [&#x27;[&#x27;,&#x27;]&#x27;] ]
},
options = {
processHtmlClass = &quot;math&quot;
}
}
</script><meta name="next-head-count" content="6"/><link rel="preload" href="/wiki/_next/static/css/52fc2ba29703df73922c.css" as="style"/><link rel="stylesheet" href="/wiki/_next/static/css/52fc2ba29703df73922c.css" data-n-g=""/><noscript data-n-css=""></noscript><link rel="preload" href="/wiki/_next/static/chunks/main-ae4733327bd95c4ac325.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/webpack-50bee04d1dc61f8adf5b.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/framework.9d524150d48315f49e80.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/commons.0e1c3f9aa780c2dfe9f0.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/pages/_app-8e3d0c58a60ec788aa69.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/940643274e605e7596ecea1f2ff8d83317a3fb76.4841a16762f602a59f00.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/pages/%5B%5B...slug%5D%5D-1aa198f87ede1cd0e1dc.js" as="script"/></head><body><div id="__next"><main><h1>The basics of org-babel</h1><section class="post-metadata"><span>Date: <!-- -->2021-05-19 18:54:37 +08:00</span><span>Date modified: <!-- -->2021-06-04 11:11:18 +08:00</span></section><nav class="toc"><ol class="toc-level toc-level-1"><li class="toc-item toc-item-h1"><a href="/wiki/text.org-mode.babel#functional-and-scripting-mode" class="toc-link toc-link-h1">Functional and scripting mode</a></li><li class="toc-item toc-item-h1"><a href="/wiki/text.org-mode.babel#functional-mode-values-and-passing-them-around" class="toc-link toc-link-h1">Functional mode values and passing them around</a></li><li class="toc-item toc-item-h1"><a href="/wiki/text.org-mode.babel#source-code-evaluation" class="toc-link toc-link-h1">Source code evaluation</a></li><li class="toc-item toc-item-h1"><a href="/wiki/text.org-mode.babel#creating-dynamic-content-with-meta-programming" class="toc-link toc-link-h1">Creating dynamic content with meta-programming</a></li><li class="toc-item toc-item-h1"><a href="/wiki/text.org-mode.babel#executing-code-blocks-in-the-same-session" class="toc-link toc-link-h1">Executing code blocks in the same session</a></li></ol></nav><p>org-babel (see <a href="/wiki/2020-04-17-21-41-30">Org mode: Babel</a>) is the framework that enables Org mode features for reproducible research.
It has a variety of applications that Org mode can do in this field so let&#x27;s enumerate them.
</p><h1 id="functional-and-scripting-mode">Functional and scripting mode</h1><p>Babel works in two modes: functional and scripting mode.
</p><ul><li><p>Functional mode returns a value either from the last statement or the return statement.
The value can then be used in other source code blocks and appropriately converted into Org mode equivalents.
If the return value is a vector type, it will be printed as tables in Org mode which will then be rendered as a vector when used in another source code block.
</p></li><li><p>Scripting mode simply prints the output.
Do keep in mind different languages have different ways of capturing the output.
</p></li></ul><p>The default mode is in functional mode but you can change it by setting <code class="inline-code">:results</code> header argument with the values from the <a href="https://orgmode.org/manual/Results-of-Evaluation.html">collection class</a>.
</p><h1 id="functional-mode-values-and-passing-them-around">Functional mode values and passing them around</h1><p>With functional mode, the value return will appear as an appropriate element in the Org mode buffer.
The following examples are in <a href="https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html">Python</a>.
</p><p>Scalar values appear as strings...
</p><pre class="src-block"><code class="language-python">return &quot;The quick brown fox jumps over the lazy dog.&quot;
</code></pre><pre class="fixed-width">The quick brown fox jumps over the lazy dog.</pre><p>
...and vector values print as tables.
</p><pre class="src-block"><code class="language-python">return [
[&quot;Monty&quot;, 45],
[&quot;Soup&quot;, 54],
[&quot;Cabbages&quot;, 63]
]
</code></pre><table><tbody><tr><td>Monty</td><td>45</td></tr><tr><td>Soup</td><td>54</td></tr><tr><td>Cabbages</td><td>63</td></tr></tbody></table><p>To pass values between different code blocks, you have to give the blocks a name.
The previous code block was given a name <code class="inline-verbatim">data</code> and passed it to the next block.
</p><pre class="src-block"><code class="language-python">return o[0]
</code></pre><table><tbody><tr><td>Monty</td><td>45</td></tr></tbody></table><h1 id="source-code-evaluation">Source code evaluation</h1><p>The header property for executing a source code block is <code class="inline-verbatim">eval</code>.
It can accept either the following values:
</p><ul><li><p><code class="inline-verbatim">no</code> to disable evaluation of the code block.
</p></li><li><p><code class="inline-verbatim">query</code> will prompt to enable evaluation first.
</p></li><li><p><code class="inline-verbatim">never-export</code> will not show the results in export but can still interact with it from the raw source.
</p></li></ul><p>By default, it will enable evaluation so you have to explicitly define it (or set it in your Emacs config).
</p><h1 id="creating-dynamic-content-with-meta-programming">Creating dynamic content with meta-programming</h1><p>With <a href="https://orgmode.org/worg/org-contrib/babel/intro.html">Babel</a>, you can call named code blocks anywhere from blocks to inline.
This creates a &quot;function&quot; with Babel using different languages.
The following block creates <code class="inline-code">init</code> function with a default value for its argument.
</p><pre class="src-block"><code class="language-python">return f&quot;Hello {name}&quot;
</code></pre><p>You can then call the <code class="inline-code">init</code> function inline with <code class="inline-code">call_init[${HEADER_ARGS}](${ARGS})</code> which should contain &quot;call<sub>init</sub>[:results raw]() Hello world&quot;.
For blocks, you can use the <code class="inline-code">#+call</code> block with a similar syntax to inline functions — i.e., <code class="inline-code">#+call: init[${HEADER_ARGS}](${ARGS})</code>.
</p><pre class="fixed-width">Hello world</pre><p>
You can also use it inside of code blocks with <code class="inline-code">&lt;&lt;init&gt;&gt;</code> which makes it perfect for code blocks templates like configuring paper output for Lilypond blocks.
Though, you have to set <code class="inline-code">:noweb yes</code> in the header arguments or configure it in <code class="inline-code">org-babel-default-header-args</code> as one of the default.
</p><pre class="src-block"><code class="language-shell">echo -n &lt;&lt;init(name=&quot;Canavan&quot;)&gt;&gt;
</code></pre><pre class="fixed-width">Hello Canavan</pre><p>
Babel functions are commonly used for inserting dynamic values.
Very helpful in reducing places you need to edit (not to mention less prone to errors).
</p><h1 id="executing-code-blocks-in-the-same-session">Executing code blocks in the same session</h1><p>Each of the source code block runs on an individual session.
However, you can connect source code blocks in the same session with <code class="inline-code">:session &lt;SESSION NAME&gt;</code>.
This allows you to cut code blocks and add more detailed explanations for them.
</p><p>Let&#x27;s start with a simple example where we want to demonstrate some Python shenanigans.
Here&#x27;s one Python code block.
</p><pre class="src-block"><code class="language-python">x = 30
print(x)
</code></pre><pre class="fixed-width">30</pre><p>
Then here&#x27;s another code block in the same session.
</p><pre class="src-block"><code class="language-python">for i in range(5):
x += 5
print(x)
</code></pre><pre class="fixed-width">35
40
45
50
55</pre><p>
In certain code where the output can still change (for example, try executing the previous code block again), this may not be the desired behavior.
To correct this, simply execute <code class="inline-code">org-babel-execute-buffer</code>.
</p><section><h2>Backlinks</h2><ul><li><a href="/wiki/2020-04-17-21-41-30">Org mode: Babel</a></li></ul></section></main></div><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"metadata":{"date":"\"2021-05-19 18:54:37 +08:00\"","date_modified":"\"2021-06-04 11:11:18 +08:00\"","language":"en","source":""},"title":"The basics of org-babel","hast":{"type":"root","children":[{"type":"element","tagName":"nav","properties":{"className":"toc"},"children":[{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-1"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"functional-and-scripting-mode"},"children":[{"type":"text","value":"Functional and scripting mode"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/text.org-mode.babel#functional-and-scripting-mode"},"children":[{"type":"text","value":"Functional and scripting mode"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"functional-mode-values-and-passing-them-around"},"children":[{"type":"text","value":"Functional mode values and passing them around"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/text.org-mode.babel#functional-mode-values-and-passing-them-around"},"children":[{"type":"text","value":"Functional mode values and passing them around"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"source-code-evaluation"},"children":[{"type":"text","value":"Source code evaluation"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/text.org-mode.babel#source-code-evaluation"},"children":[{"type":"text","value":"Source code evaluation"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"creating-dynamic-content-with-meta-programming"},"children":[{"type":"text","value":"Creating dynamic content with meta-programming"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/text.org-mode.babel#creating-dynamic-content-with-meta-programming"},"children":[{"type":"text","value":"Creating dynamic content with meta-programming"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"executing-code-blocks-in-the-same-session"},"children":[{"type":"text","value":"Executing code blocks in the same session"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/text.org-mode.babel#executing-code-blocks-in-the-same-session"},"children":[{"type":"text","value":"Executing code blocks in the same session"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"org-babel (see "},{"type":"element","tagName":"a","properties":{"href":"/2020-04-17-21-41-30"},"children":[{"type":"text","value":"Org mode: Babel"}]},{"type":"text","value":") is the framework that enables Org mode features for reproducible research.\nIt has a variety of applications that Org mode can do in this field so let's enumerate them.\n"}]},{"type":"element","tagName":"h1","properties":{"id":"functional-and-scripting-mode"},"children":[{"type":"text","value":"Functional and scripting mode"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Babel works in two modes: functional and scripting mode.\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"te