emacs org-babel results unexpected output - emacs

I'm using emacs 25.2.1 with spacemacs and scimax and trying to create a jupyter-like notebook. After some experimenting, I'm confused by the output I get from the :results output tag from the final src block:
#+name: OOP
#+BEGIN_SRC elisp :results silent :session OOP
(pyvenv-activate "~/Desktop/py2_venv")
#+END_SRC
#+BEGIN_SRC ipython :session OOP :results silent
class Dog():
def __init__(self, breed, name, spots):
self.breed = breed
self.name = name
#expect boolean true/false
self.spots = spots
#+END_SRC
#+BEGIN_SRC ipython :session OOP :results output
my_dog = Dog(breed='lab', name='Sammy', spots='False')
print(type(my_dog))
print(my_dog.breed)
my_dog.name
my_dog.spots
#+END_SRC
#+RESULTS:
**:RESULTS:
# Out[23]:
# output
<class '__main__.Dog'>
lab
# text/plain
: 'False'**
:END:
It's my understanding that all output should be displayed with the output option in session mode. Additionally I shouldn't need to use print(). I'm confused why my_dog.name is missing from output, but my_dog.spots is not. Any ideas what I'm missing?

:results output gives back the stdin, which will be the print output.
If you use :results value or :results value raw you will receive the last evaluation and not the side effect :
#+BEGIN_SRC ipython :session OOP :results value
my_dog = Dog(breed='lab', name='Sammy', spots='False')
print(type(my_dog))
print(my_dog.breed)
my_dog.name
my_dog.spots
#+END_SRC
In order to get both evaluations you will need to type a bit more python.
Have a look at :
https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html
also the following answer in Emacs SE:
https://emacs.stackexchange.com/a/45121/17548

Related

What can cause org-auto-tangle to result nil for all noweb code block evaluations

I'm using literate programming for some configuration files and would like to have some parts from elisp code block evaluations. I tried evaluating named code blocks with :noweb tangle but they always results nil and I do not see any errors in the *Messages*. Here's a simplified hello world example and the results I got.
Org file
#+title: Hello
#+PROPERTY: header-args :tangle hello.txt :cache no :exports none
#+auto_tangle: t
#+name: hello-world-output
#+begin_src emacs-lisp :tangle no :eval no-export :results output
(print "Hello world")
#+end_src
#+name: hello-world-value
#+begin_src emacs-lisp :tangle no :eval no-export :results value
"Hello world"
#+end_src
#+begin_src text :noweb tangle
<<hello-world-output>> -> <<hello-world-output()>>
<<hello-world-value>> -> <<hello-world-value()>>
#+end_src
Tangled results
(print "Hello world") -> nil
"Hello world" -> nil
I also checked that org-link-elisp-confirm-function and org-confirm-babel-evaluate both have nil value, so they should not be preventing evaluation.
EDIT: I forgot to mention that I used org-auto-tangle. Issue doesn't occur when calling org-bable-tangle directly.
I had the same issue and below solved it for me.
According to the code of org-auto-tangle the code will not be evaluated by default. In order to have the code auto evaluated you need to add your org file to the org-auto-tangle-babel-safelist. I've posted the definition of the variable and a link to the README with an example of how to set the variable.
(defvar org-auto-tangle-babel-safelist '()
"List of full path of files for which code blocks need to be evaluated.
By default, code blocks are not evaluated during the auto-tangle to avoid
possible code execution from unstrusted source. To enable code blocks evaluation
for a specific file, add its full path to this list.")
https://github.com/yilkalargaw/org-auto-tangle#babel-auto-tangle-safelist

Org babel print elisp variable

Using org, I'd like to execute Lisp code which prints the value of a variable into a results block. E.g. I'm trying to print the value of org-babel-default-header-args.
I've tried this:
#+BEGIN_SRC elisp :exports both
(print 'org-babel-default-header-args)
#+END_SRC
#+BEGIN_SRC elisp :exports both
(org-babel-default-header-args)
#+END_SRC
#+BEGIN_SRC elisp :exports both
(symbol-value 'org-babel-default-header-args)
#+END_SRC
The closest thing I've gotten to work is this:
#+BEGIN_SRC elisp :exports both
(describe-variable 'org-babel-default-header-args)
#+END_SRC
But that prints out some extra text. I'd like to literally just print the value of the variable.
To print the value of a variable foo, use
(print foo)
without quoting it. Quote inhibits evaluation: that's exactly what you don't want to do here.

Org-mode: overwriting globals #+PROPERTIES: in block headers?

It is often the case that I want to have :results silent for all the code blocks in an Org-mode document. Do simplify my block headers, I define this in my Org-mode document:
#+PROPERTY: header-args :results silent
That works properly: all the codeblocks uses the :results silent option in the blocks' header.
However, if I specify :results output in one of the code block, it will still be silent. I would have expected that it would overwrite the global setting, but it doesn't seem so.
Am I right by saying that or is there something I am missing to get this behavior?
Here is an example of what I would like to do:
#+PROPERTY: header-args :results silent
...
#+BEGIN_SRC clojure
;; this one is silent
(def foo "bar)
#+END_SRC
...
#+BEGIN_SRC clojure :results output
;; this one is being outputted
(def foo "bar)
#+END_SRC
This seems to be a bug, present even in the most up-to-date version of org-mode. I've reported it on the org-mode mailing list.
EDIT: Charles Berry pointed out on the ML that this is not a bug. The opposite of "silent" is "replace", so the second source block should read:
#+BEGIN_SRC elisp :results output replace
;; this one is being outputted
(princ "foo")
#+END_SRC
See http://thread.gmane.org/gmane.emacs.orgmode/108001/focus=108008
and the :results section in the manual http://orgmode.org/org.html#results
Note that you get to pick one value for every section (collection, type, format, handling) - if you don't pick a value for a section, a default value is picked. In the above, there was no explicit value for "handling", so the default value from the property still controlled.

org-babel: want all noweb block references to appear verbatim on export

Consider the following MVE in org-mode -- it contains my full question in detail. But, in summary, with some code blocks, some noweb references to other code blocks are substituted inline when I export the document, and, with other code blocks, the noweb references, in double broket quotes, are copied verbatim into the exported PDF. I do not know what causes this difference in behavior and I don't know how to control it, but I'd like to. I'd like to be able to specify that some blocks have behavior 1 (references substituted) and other blocks have behavior 2 (references verbatim).
The PDF that results from org-export is at this link
#+BEGIN_COMMENT
The emacs lisp block must export results, even though the results are none,
otherwise the block will not be eval'ed on export, and we will get
unacceptable confirmation requests for all the subsequent python blocks.
#+END_COMMENT
#+BEGIN_SRC emacs-lisp :exports results :results none
(setq org-confirm-babel-evaluate nil)
#+END_SRC
** PyTests
Define the test and cases. This code must be tangled out to an external file
so =py.test= can see it.
When I /export/ this to PDF, the noweb references, namely =<<imports>>= and
=<<definitions>>=, are substituted inline, so the typeset version of this
block in the PDF shows ALL the code. This is not what I want.
#+NAME: test-block
#+BEGIN_SRC python :noweb yes :tangle test_foo.py
<<imports>>
<<definitions>>
def test_smoke ():
np.testing.assert_approx_equal (foo_func (), foo_constant)
#+END_SRC
#+RESULTS: test-block
: None
The following blocks import prerequisites and do a quick smoke test:
** Do Some Imports
#+NAME: imports
#+BEGIN_SRC python
import numpy as np
#+END_SRC
#+RESULTS: imports
: None
** Define Some Variables
However, in the typeset PDF, the noweb reference =<<foo-func>>= in the block
below is /not/ substituted in-line, but rather appears verbatim. I want /all/
noweb references to appear verbatim in the exported, typeset, PDF document,
just like this one.
#+NAME: definitions
#+BEGIN_SRC python
foo_constant = 42.0
<<foo-func>>
#+END_SRC
#+RESULTS: definitions
** Define Some Functions
*** Foo Function is Really Interesting
#+NAME: foo-func
#+BEGIN_SRC python
def foo_func () :
return 42.000
#+END_SRC
#+RESULTS: foo-func
: None
We want results from pytest whether it succeeds or fails, hence the /OR/ with
=true= in the shell
#+BEGIN_SRC sh :results output replace :exports both
py.test || true
#+END_SRC
#+RESULTS:
: ============================= test session starts ==============================
: platform darwin -- Python 2.7.10, pytest-2.8.0, py-1.4.30, pluggy-0.3.1
: rootdir: /Users/bbeckman/foo, inifile:
: collected 1 items
:
: test_foo.py .
:
: =========================== 1 passed in 0.06 seconds ===========================
Found the appropriate references here
Here is a corrected PDF exported from the following .org file.
And here is the corrected MVE (it, itself, explains the correction):
#+BEGIN_COMMENT
The emacs lisp block must export results, even though the exports are none,
otherwise the block will not be eval'ed on export, and we will get unacceptable
confirmation requests for all the subsequent python blocks.
#+END_COMMENT
#+BEGIN_SRC emacs-lisp :exports results :results none
(setq org-confirm-babel-evaluate nil)
#+END_SRC
** PyTests
Define the test and cases. This code must be tangled out to an external file
so =py.test= can see it.
When I /export/ this to PDF, the noweb references, namely =<<imports>>= and
=<<definitions>>=, are *NOT* substituted inline, but typeset verbatim. This
is what I want. You get this behavior by saying =:noweb no-export= in the
header.
#+NAME: test-block
#+BEGIN_SRC python :tangle test_foo.py :noweb no-export :exports code :results none
dummy_for_org_mode = True
<<imports>>
<<definitions>>
def test_smoke ():
np.testing.assert_approx_equal (foo_func (), foo_constant)
#+END_SRC
The following blocks import prerequisites and do a quick smoke test:
** Do Some Imports
#+NAME: imports
#+BEGIN_SRC python :exports code :results none
import numpy as np
#+END_SRC
** Define Some Variables and Functions
In this block, I want the noweb reference =<<foo-func>>= in the block to be
substituted in-line and not to appear verbatim. Do that by saying
=:noweb yes= in the header.
#+NAME: definitions
#+BEGIN_SRC python :noweb yes :exports code :results none
foo_constant = 42.0
<<foo-func>>
#+END_SRC
** Define Some Functions
*** Foo Function is Really Interesting
Here, I want to talk about the implementation of foo function in detail, but I
don't want its code to be exported again, just to appear in the original
=.org= file as I reminder or note to me.
#+NAME: foo-func
#+BEGIN_SRC python :exports none :results none
def foo_func () :
return 42.000
#+END_SRC
** Run the Tests
We want results from pytest whether it succeeds or fails, hence the /OR/ with
=true= in the shell
#+BEGIN_SRC sh :results output replace :exports both
py.test || true
#+END_SRC
#+RESULTS:
: ============================= test session starts ==============================
: platform darwin -- Python 2.7.10, pytest-2.8.0, py-1.4.30, pluggy-0.3.1
: rootdir: /Users/bbeckman/foo, inifile:
: collected 1 items
:
: test_foo.py .
:
: =========================== 1 passed in 0.08 seconds ===========================

Any way to make org-babel properly indent noweb tangled code?

Tangling this:
#+BEGIN_SRC C :tangle no :noweb-ref begin
int main() {
printf("Line 1\n");
#+END_SRC
#+BEGIN_SRC C :tangle no :noweb-ref middle
printf("Second\n");
#+END_SRC
#+BEGIN_SRC C :tangle no :noweb-ref end
}
#+END_SRC
#+BEGIN_SRC C :tangle ~/test.c :noweb no-export
<<begin>>
<<middle>>
<<end>>
#+END_SRC
Yields this:
int main() {
printf("Line 1\n");
printf("Second\n");
}
I have org-src-preserve-indentation turned on, but it can't preserve what isn't there. The code editing windows can't set it correctly if it doesn't see the parts from the previous source code blocks. Finally, I don't want to have to go through all the previous snippets to figure out what the indentation should start at every time I start a new source code block.
Current hack is to tangle the source code, open the tangled file in a new buffer, select all and run c-indent-line-or-region, but I'm hoping there's something better than that.
Org-mode version: 8.2.5h
As mentioned, hooking into the org-babel-post-tangle-hook is the way to go. I use the following:
(defun tnez/src-cleanup ()
(indent-region (point-min) (point-max)))
(add-hook 'org-babel-post-tangle-hook 'tnez/src-cleanup)