7

I would like to pass the code from code block test_python to script. I do not want test_python to be evaluated, but simply to have the source code inserted into script when I evaluate script. The goal is to have a more generic extended version of script that will do some useful stuff to setup the environment and run the contents of test_python such that it can be run on different machines. I'd like to eventually hide the script block in a #+INCLUDEed org mode file, then just be able to have the test_python block and a #+CALL to script in the primary org mode.

A minimal example where I am trying to do this is:

#+NAME: test_python
#+BEGIN_SRC ipython :results none :exports none
  import numpy as np

  x0 = np.arange(-1, 1, 1/10.)
  x1 = np.arange(-1, 1, 1/10.)
  x0, x1 = np.meshgrid(x0, x1)
  y_truth = x0**2 - x1**2 + x1 - 1

  print('inside python')
#+END_SRC

#+NAME: script
#+HEADER: :var python_code=""
#+BEGIN_SRC bash :results output
  #!/bin/bash

  result=$(python <<EOF
  $python_code
  EOF
  )
  echo $python_code
  echo $result
  echo "done test"
#+END_SRC

#+NAME: script_generator
#+CALL: script[:var python_code=test_python]

Adding one more thing: I am actually tangling script to write to file. I just removed that for the sake of the example.

--- Edit

I had left out some of this to try to get to a minimal example, but it seems that will make the question more clear. Ultimately I want this to look like the following.

any org file I'm currently working in:

* Some cool org stuff
:PROPERTIES:
:OUT_SCRIPT: /ssh:otherMachine:~/scripts/test_python.sh
:END:

#+INCLUDE: ~/org/script_utils.org

#+NAME: test_python
#+BEGIN_SRC ipython :results none :exports none
  import numpy as np

  x0 = np.arange(-1, 1, 1/10.)
  x1 = np.arange(-1, 1, 1/10.)
  x0, x1 = np.meshgrid(x0, x1)
  y_truth = x0**2 - x1**2 + x1 - 1

  print('inside python')
#+END_SRC

#+NAME: script_generator
#+CALL: script[:var python_code=test_python,script_path=OUT_SCRIPT]

and

script_utils.org:

#+NAME: script
#+HEADER: :var python_code="", script_path=""
#+BEGIN_SRC bash :results output :tangle script_path
  #!/bin/bash

  result=$(python <<EOF
  $python_code
  EOF
  )
  echo $python_code
  echo $result
  echo "done test"
#+END_SRC
2
  • 2
    What about Noweb reference syntax (orgmode.org/manual/Noweb-Reference-Syntax.html)? This looks like a typical use case here.
    – Philopolis
    Commented May 31, 2021 at 12:26
  • @Philopolis your comment would be a good answer for this question, if you added a short explanation
    – Tyler
    Commented May 31, 2021 at 17:17

1 Answer 1

5

The Noweb reference syntax may help in such cases. Using this syntax, the body of a given code block can be executed inside the body of another code block. As a summary of the Org manual page:

  1. You have to NAME the first code block
  2. Add its <<NAME>> in a second code block, which will have :noweb yes as a header argument. Then, <<NAME>> will be expanded/interpreted as the contents of the first code block.

I give an example below, adapted from your own example:

#+NAME: test_python
#+BEGIN_SRC python :results none :exports none
import numpy as np

x0 = np.arange(-1, 1, 1/10.)
x1 = np.arange(-1, 1, 1/10.)
x0, x1 = np.meshgrid(x0, x1)
y_truth = x0**2 - x1**2 + x1 - 1

print('inside python')
#+END_SRC

#+NAME: script
#+BEGIN_SRC bash :results output :noweb yes
#!/bin/bash
result=$(python <<EOF
<<test_python>>
EOF
)
echo $result
echo "done test"
#+END_SRC

#+RESULTS: script
: inside python
: done test
3
  • This is almost it, but what is missing is being able to do this: ``` #+NAME: script_generator #+CALL: script[:var python_code=test_python] ``` Commented Jun 2, 2021 at 4:35
  • The instruction #+CALL: script (without additional argument) should work.
    – Philopolis
    Commented Jun 2, 2021 at 5:40
  • I expanded my question. I explicitly want to pass the name of the block/noweb-ref to the other block. Commented Jun 2, 2021 at 10:40

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.