1

I try to use a example block as input to babel block, I want to export babel results only, in below example, I try to add exports none to example block but it doesn't work, the example content exported to html page as well.

How can I disable example block export?

* example
#+name: input
#+begin_example :exports none
5 1 3 2 0
#+end_example

#+begin_src python :results output :eval yes :var x=input :exports results
import matplotlib.pyplot as plt
import numpy as np

x = np.array(x.split())
x = x.astype(int)
fig,ax = plt.subplots()
ax.plot(x)
plt.savefig("test.png")
#+end_src

#+RESULTS:

[[./test.png]]

3 Answers 3

3

You could put the example in a separate section and tag it noexport:

* example input                                                        :noexport:
#+name: input
#+begin_example
5 1 3 2 0
#+end_example

* example code 
#+begin_src python :results output :eval yes :var x=input :exports results
  import matplotlib.pyplot as plt
  import numpy as np

  x = np.array(x.split())
  x = x.astype(int)
  fig,ax = plt.subplots()
  ax.plot(x)
  plt.savefig("./test.png")
#+end_src

#+RESULTS:
[[file:./test.png]]

BTW, the way that you have your headers does not really work to produce a result: the code block produces no output on its stdout so :results output makes it produce an empty result. The output file is a side effect of the code block execution, so Org mode (and babel) don't know anything about it. Did you add the result by hand?

A better way to do that is to use the following header line instead:

#+begin_src python :results file link :file test.png :eval yes :var x=input :exports results
...

That tells babel to produce a link to a file named test.png, but babel does not create the file: it expects the code block to produce it.

See e.g. this Emacs SE question for more details.

2
  • Great, is it possible for python to use the name file :file parameter to save the figure? now it's hardcode as plt.savefig("./test.png").
    – lucky1928
    Commented Mar 4, 2021 at 1:48
  • It should be possible, right? Unfortunately, I don't know how to do it. I hate repeating myself.
    – NickD
    Commented Mar 4, 2021 at 3:41
3

What I do for cases like this, where I do not want the input to some src block exported, is put the input (the example block in your case) in a drawer and set the option d:nil so as to not export drawers.

* example
:input:
#+name: input
#+begin_example
5 1 3 2 0
#+end_example
:end:
1

I don't think org offers any way to exclude example blocks from exporting. A possible work-around: if you don't want the example to show up in your export, does it need to be an example? You could make it a bash code block instead:

* example

#+name: input
#+begin_src bash :exports none
echo 5 1 3 2 0
#+end_src

#+begin_src python :results output :eval yes :var x=input :exports results
import matplotlib.pyplot as plt
import numpy as np

x = np.array(x.split())
x = x.astype(int)
fig,ax = plt.subplots()
ax.plot(x)
plt.savefig("test.png")
#+end_src
1
  • Good idea, I use example block since I wish do not change original text block then do processing in python code.
    – lucky1928
    Commented Mar 3, 2021 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.