将源代码从一个代码块中输入到Emacs组织模式中的另一个代码块 [英] Make the source code from one code block the input to another code block in Emacs org-mode
问题描述
我正在开始使用组织模式,并且我想要做的一些事情似乎应该是可能的,但我无法想象出来。
I'm getting started with org-mode and there's something I'd like to do that seems like it should be possible, but I'm having trouble figuring out.
让我来描述场景:我有一些要在远程服务器上执行的SQL代码。我目前有一个python脚本,将SQL代码作为一个字符串,并为我做这个。没有组织模式,我的工作流程将是从一个文件开始:
Let me describe the scenario: I have some SQL code that I want to execute on a remote server. I currently have a python script that takes SQL code as a string and does this for me. Without org-mode, my work flow would be to start with a file like so:
echo "SELECT name, grade FROM students" >> basic_query.sql
然后我会运行:
$ python run_query.py basic_query.sql
为此,在组织模式设置中,我可以为SQL创建一个代码块:
To do this is in the org-mode setting, I could create a code block for the SQL:
#+NAME: basic_query
#+BEGIN_SRC SQL
SELECT name, grade FROM students
#+END_SRC
然后我将有一个python调用函数的代码块:
And then I'd have a code block for the python calling function:
#+BEGIN_SRC python :export results
import sql_helper
query_status = sql_helper.run_query(<<basic_query>>)
#+END_SRC
我可以用来创建一个表,进一步处理,绘图等。注意<< >>
事情是不对的,显然---这只是滥用符号来表示我正在努力做什么。
Which I might use to create a table, process further, plot etc. Note the << >>
thing is not right, obviously---it's just an abuse of notation to indicate what I'm trying to do.
推荐答案
如果您已经设置了emacs / org-mode,以便启用python代码((python。t)
在 org-babel-do-load-languages
),你几乎在那里,我把你的例子改成了
If you have set up emacs/org-mode so that python code is enabled ((python . t)
in org-babel-do-load-languages
), you are almost there, I changed your example to
#+NAME: basic_query
#+BEGIN_SRC SQL
SELECT name, grade FROM students
#+END_SRC
#+BEGIN_SRC python :export results :noweb yes :tangle yes
import sql_helper
query = """
<<basic_query>>
"""
query_status = sql_helper.run_query(query)
#+END_SRC
我的python有点生锈,至少如果我纠结到
My python is a little rusty, but at least if I tangle this to
import sql_helper
query = """
SELECT name, grade FROM students
"""
query_status = sql_helper.run_query(query)
python不再抱怨语法,而是关于缺少的模块sql_helper ...
python does no longer complain about the syntax, but about the missing module sql_helper...
这篇关于将源代码从一个代码块中输入到Emacs组织模式中的另一个代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!