2

I would like to execute a python code block to produce new headlines (i.e. modify the current org file programmatically). If I produce raw results with a replace argument, the replace argument seems to be inactive in this case. For example, if I run 3 times the following code block:

#+name: test_block
#+begin_src python :results output raw replace 
print("* Test")
#+end_src

the obtained result is:

#+RESULTS: test_block
* Test
* Test
* Test

And I would expect only one * Test line with the replace argument. The code works as expected if I remove the raw argument (but there is a : before the headline (* test) which is not a headline anymore).

8
  • 5
    Try drawer instead of raw: with raw, Org mode does not know where the results end and so it does not know what to erase.
    – NickD
    Commented Nov 2, 2020 at 11:57
  • Thanks @NickD for your suggestion. However in my specific use case drawers are not working either. What I want to do is to generate headlines programatically. In the example code block of my question the print instruction should be print("*** Test"). And in this case drawers are added at each run.
    – Lgen
    Commented Nov 2, 2020 at 12:57
  • I don't understand the use case. Maybe you can expand your question and explain? Do you expect the babel block to actually modify the structure of the Org mode document that contains it? IOW, after you execute the block, there will be a new "real" third-level headline?
    – NickD
    Commented Nov 2, 2020 at 14:26
  • Just to clarify: the answer to the question in your tile "How to replace raw code block output" is "There is no way other than manually deleting the previous output". Org mode itself does not have any way to delete such output.
    – NickD
    Commented Nov 2, 2020 at 14:33
  • @NickD: I tried to be more precise in my question (I wanted to be as less specific as possible in the first version but it seems difficult to understand). Currently, I am manually deleting the previous output before each run but I wanted to know if it is possible to avoid that.
    – Lgen
    Commented Nov 2, 2020 at 14:50

1 Answer 1

0

It's an old question... and my post is not quite an answer to the OP's question... but maybe someone will find my solution useful... A way to achieve what OP wanted to achieve is to generate an org-file structure, save it in an external file and then include it in the original file. Providing you want to use python in your code block you might have:

* How to generate a content of an org-file:

#+begin_src python :exports none :results none :eval yes
  import os
  from pathlib import Path
  
  fname = "toInclude.org" ;
  dpath = "./orgBabelOut/autogenerate/"
  fpath = dpath + fname

  Path(dpath).mkdir(parents=True, exist_ok=True)

  f = open(fpath, "w")

  with open(fpath, "w") as file:
      for i in range(2):
      file.write('* Section ' + str(i+1) + '\n')

  f.close()
#+end_src

#+INCLUDE: ./orgBabelOut/autogenerate/toInclude.org :minlevel 1

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.