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

后端 未结 1 1658
星月不相逢
星月不相逢 2021-01-20 06:33

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/l

相关标签:
1条回答
  • 2021-01-20 06:45

    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?

    0 讨论(0)
提交回复
热议问题