Ipython bash/shell 细胞魔法:我可以在细胞之间使用持久变量吗?
Ipython bash/shell cell magics: can I have persistent variables between cells?
这是我第一次 post SO,所以如果我做错了什么请告诉我。
我有兴趣在 ipython 中使用不同的编程语言,类似于 babel/literal 在 emacs org 模式中编程。我喜欢 emacs org 模式的一点是,一个人可以有多个 "cells" 连接到同一个 R/bash 会话。这允许我重新使用在文档的较早部分创建的 variables/functions,即使我在两者之间做了其他事情。
我发现 ipython 使用 Rmagic 可以做到这一点。举个例子
In [1]: %load_ext rpy2.ipython
In [2]: %%R
a <- 3
a
Out [2]: 3
In [3]: something_in_python = 'I am doing something unrelated now'
In [4]: %%R
cat('My variable a is still here, its value is: ', a) # a is still here!
Out [4]: My variable is still here, its value is: 3
我非常希望能够在 bash 中做类似的事情。但是,无论我使用“%%script bash”还是 %%sx,变量都不是持久的。这是我正在尝试做的事情:
In [1]: %%script bash
var1="hello"
echo $var1
Out[1]: hello
In [2]: %%script bash
echo $var1 # I need $var1 to be present in this cell too - but its gone!
Out[62]:
是否可以在多个单元格中进行相同的基本会话?或者至少以某种方式传递变量。当然,我可以尝试将变量传递到 python 然后返回到下一个 bash 单元格,但我觉得一定有更好的方法。感谢您的帮助!
PS:我在寻找解决方案,但在这里或通过谷歌搜索都没有找到任何东西。有一些像这样的东西:IPython Notebook previous cell content,但它似乎对我的情况没有帮助。
一个选项虽然有限,但允许设置 stdout 和 stderr 变量。这使您可以在单元格之间设置和传递两个变量。例如:
In [1]:%%bash --out var1 --err var2
echo "Hello"
echo "Hi" >&2
In [2]:print(var1)
print(var2)
# Resulting in:
Hello
Hi
In [3]:%%bash -s "$var1" "$var2"
echo "arg1 takes on var1 = "
echo "arg2 takes on var2 = "
# Resulting in:
arg1 takes on var1 = Hello
arg2 takes on var2 = Hi
请注意,eol 字符似乎进入了变量。
有关使用 stdout 和 stderr 的详细信息,请参阅 http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Script%20Magics.ipynb
有关将变量传回的详细信息,请参阅 Can I access python variables within a `%%bash` or `%%script` ipython notebook cell?
这是我第一次 post SO,所以如果我做错了什么请告诉我。
我有兴趣在 ipython 中使用不同的编程语言,类似于 babel/literal 在 emacs org 模式中编程。我喜欢 emacs org 模式的一点是,一个人可以有多个 "cells" 连接到同一个 R/bash 会话。这允许我重新使用在文档的较早部分创建的 variables/functions,即使我在两者之间做了其他事情。
我发现 ipython 使用 Rmagic 可以做到这一点。举个例子
In [1]: %load_ext rpy2.ipython
In [2]: %%R
a <- 3
a
Out [2]: 3
In [3]: something_in_python = 'I am doing something unrelated now'
In [4]: %%R
cat('My variable a is still here, its value is: ', a) # a is still here!
Out [4]: My variable is still here, its value is: 3
我非常希望能够在 bash 中做类似的事情。但是,无论我使用“%%script bash”还是 %%sx,变量都不是持久的。这是我正在尝试做的事情:
In [1]: %%script bash
var1="hello"
echo $var1
Out[1]: hello
In [2]: %%script bash
echo $var1 # I need $var1 to be present in this cell too - but its gone!
Out[62]:
是否可以在多个单元格中进行相同的基本会话?或者至少以某种方式传递变量。当然,我可以尝试将变量传递到 python 然后返回到下一个 bash 单元格,但我觉得一定有更好的方法。感谢您的帮助!
PS:我在寻找解决方案,但在这里或通过谷歌搜索都没有找到任何东西。有一些像这样的东西:IPython Notebook previous cell content,但它似乎对我的情况没有帮助。
一个选项虽然有限,但允许设置 stdout 和 stderr 变量。这使您可以在单元格之间设置和传递两个变量。例如:
In [1]:%%bash --out var1 --err var2
echo "Hello"
echo "Hi" >&2
In [2]:print(var1)
print(var2)
# Resulting in:
Hello
Hi
In [3]:%%bash -s "$var1" "$var2"
echo "arg1 takes on var1 = "
echo "arg2 takes on var2 = "
# Resulting in:
arg1 takes on var1 = Hello
arg2 takes on var2 = Hi
请注意,eol 字符似乎进入了变量。
有关使用 stdout 和 stderr 的详细信息,请参阅 http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Script%20Magics.ipynb
有关将变量传回的详细信息,请参阅 Can I access python variables within a `%%bash` or `%%script` ipython notebook cell?