5

我的 Babel 库中有许多 Org Babel 代码块,我经常按顺序调用它们。

是否可以制作一个按顺序调用这些其他代码块的 Org Babel 代码块?

4

2 回答 2

5

是的,我有几个 org-babel 文件在其中执行此操作。这是一种方法:

#+srcname: foo
#+begin_src python :exports code :tangle yes
  def foo():
      print "I'm foo()"
#+end_src

#+name: bar
#+begin_src python :exports code :tangle yes
  def bar():
      foo()
      print "I'm bar()'"
#+end_src

#+srcname: main
#+begin_src python :exports code :tangle yes
  foo()
  bar()
#+end_src

其输出是一个如下所示的文件:

def foo():
    print "I'm foo()"

def bar():
    foo()
    print "I'm bar()'"

foo()
bar()

如果 org 文件中的代码与您想要生成的顺序不同,您可以使用 noweb 标签按照您想要的顺序生成代码文件,如下所示:

#+name: bar
#+begin_src python :noweb-ref bar :tangle no
  def bar():
      foo()
      print "I'm bar()'"

#+end_src

#+srcname: foo
#+begin_src python :noweb-ref foo :tangle no
  def foo():
      print "I'm foo()"

#+end_src


#+begin_src python :noweb tangle :tangle yes

  <<foo>>
  <<bar>>

  foo()
  bar()
#+end_src

纠结这个的输出是:

def foo():
    print "I'm foo()"

def bar():
    foo()
    print "I'm bar()'"

foo()
bar()
于 2013-03-30T14:53:45.450 回答
5

是的你可以。只需使用 :var ,其中参数是另一个块执行的结果。

#+name: clean
#+begin_src ...
...
#+end_src

#+name: plot
#+begin_src :var data=clean
...
#+end_src
于 2013-03-30T21:55:23.693 回答