Ipython bash/shell cell magics: can I have persistent variables between cells?

為{幸葍}努か 提交于 2019-12-20 03:13:09

问题


This is my first post on SO, so please tell me if I am doing something wrong.

I am interested in using different programming languages in ipython, similar to babel/literal programming in emacs org mode. What I like about emacs org mode is that one can have multiple "cells" connecting to the same R/bash session. This allows me to re-use variables/functions created in an earlier part of the document, even if I do something else in between.

I have found that this is possible in ipython with the Rmagic. As an example

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

I would very much like to be able to do something similar in bash. However, no matter whether I use "%%script bash" or %%sx, variables are not persistent. Here is what I am trying to do:

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]: 

Is there anyway to have the same base session in multiple cells? Or at least to somehow pass on variables. Of course, I could play around with passing variables into python and then back into the next bash cell, but I have a feeling that there must be a better way. Thank you for your help!

PS: I looked for a solution, but I didn't find anything either here or through googling. There is some stuff like this: IPython Notebook previous cell content, but it doesn't seem to be helpful for my case.


回答1:


One option, though limited, allows the stdout and stderr variables to be set. This gives you the availability of setting and passing two variables in between cells. For example:

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 = $1"
       echo "arg2 takes on var2 = $2"
# Resulting in:
       arg1 takes on var1 = Hello

       arg2 takes on var2 = Hi

Note that the eol character seems to make its way into the variable.

For details on using stdout and stderr see http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Script%20Magics.ipynb

For details on passing variables back in see Can I access python variables within a `%%bash` or `%%script` ipython notebook cell?



来源:https://stackoverflow.com/questions/30214808/ipython-bash-shell-cell-magics-can-i-have-persistent-variables-between-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!