ipython-magic

Pipe Ipython magic output to a variable?

跟風遠走 提交于 2019-12-01 02:43:53
I want to run a bash script in my ipython Notebook and save the output as a string in a python variable for further manipulation. Basically I want to pipe the output of the bash magic to a variable, For example the output of something like this: %%bash some_command [options] foo bar What about using this: myvar = !some_command --option1 --option2 foo bar instead of the %%bash magic? Using the ! symbol runs the following command as a shell command, and the results are all stored in myvar . For running multiple commands and collecting the output of all of them, just put together a quick shell

How to store the result from %%timeit cell magic?

烈酒焚心 提交于 2019-11-30 19:02:35
I can't figure out how to store the result from cell magic - %%timeit ? I've read: Can you capture the output of ipython's magic methods? Capture the result of an IPython magic function and in this questions answers only about line magic. In line mode ( % ) this works: In[1]: res = %timeit -o np.linalg.inv(A) But in cell mode ( %% ) it does not : In[2]: res = %%timeit -o A = np.mat('1 2 3; 7 4 9; 5 6 1') np.linalg.inv(A) It simply executes the cell, no magic. Is it a bug or I'm doing something wrong? You can use the _ variable (stores the last result) after the %%timeit -o cell and assign it

How to store the result from %%timeit cell magic?

时光毁灭记忆、已成空白 提交于 2019-11-30 02:49:46
问题 I can't figure out how to store the result from cell magic - %%timeit ? I've read: Can you capture the output of ipython's magic methods? Capture the result of an IPython magic function and in this questions answers only about line magic. In line mode ( % ) this works: In[1]: res = %timeit -o np.linalg.inv(A) But in cell mode ( %% ) it does not : In[2]: res = %%timeit -o A = np.mat('1 2 3; 7 4 9; 5 6 1') np.linalg.inv(A) It simply executes the cell, no magic. Is it a bug or I'm doing

How to (intermittently) skip certain cells when running IPython notebook?

喜夏-厌秋 提交于 2019-11-29 20:55:27
I usually have to rerun (most parts of) a notebook when reopen it, in order to get access to previously defined variables and go on working. However, sometimes I'd like to skip some of the cells, which have no influence to subsequent cells (e.g., they might comprise a branch of analysis that is finished) and could take very long time to run. These cells can be scattered throughout the notebook, so that something like "Run All Below" won't help much. Is there a way to achieve this? Ideally, those cells could be tagged with some special flags, so that they could be "Run" manually, but would be

How to run IPython script from the command line - syntax error with magic functions, %

最后都变了- 提交于 2019-11-29 14:55:10
问题 I want to run IPython from the command line. However, I get a syntax error on the first line, importing pylab with the magic function %pylab is giving a syntax error on the %. The command I am using is simply ipython -i script.py . Any ideas how to solve this? 回答1: You need to name your file script.ipy. When it ends in .ipy it can contain ipython syntax. From ipython --help : Usage ipython [subcommand] [options] [files] If invoked with no options, it executes all the files listed in sequence

Share data between IPython Notebooks

会有一股神秘感。 提交于 2019-11-28 19:23:52
If I have several IPython notebooks running on the same server. Is there any way to share data between them? For example, importing a variable from another notebook? Thanks! This works for me : The %store command lets you pass variables between two different notebooks. data = 'this is the string I want to pass to different notebook' %store data Now, in a new notebook… %store -r data print(data) this is the string I want to pass to different notebook I've successfully tested with sklearn dataset : from sklearn import datasets dataset = datasets.load_iris() %store dataset in notebook to read

How to (intermittently) skip certain cells when running IPython notebook?

筅森魡賤 提交于 2019-11-28 17:07:46
问题 I usually have to rerun (most parts of) a notebook when reopen it, in order to get access to previously defined variables and go on working. However, sometimes I'd like to skip some of the cells, which have no influence to subsequent cells (e.g., they might comprise a branch of analysis that is finished) and could take very long time to run. These cells can be scattered throughout the notebook, so that something like "Run All Below" won't help much. Is there a way to achieve this? Ideally,

Using magic commands outside of Interactive Shell in IPython

╄→尐↘猪︶ㄣ 提交于 2019-11-28 10:21:37
Is there a way to use "magic commands" from IPython from an outside file? For example if I have a file, "rcode.py" with the code: %load_ext rmagic %R a=c(1,2,3);b=c(2,3,4);print(summary(lm(a~b))) This gives me a SyntaxError for the first line when I run it using ipython rcode.py in the command line. However when I type these lines straight into the interactive shell with ipython it runs fine. Is this because you only do magic in the interactive shell? Thanks! If you name your file with a .ipy extension, ipython will parse it properly. You can simply make a symlink if you want: $ ln -s rcode.py

How to pass a variable to magic ´run´ function in IPython

人盡茶涼 提交于 2019-11-28 04:16:36
I want to do something like the following: In[1]: name = 'long_name_to_type_every_now_and_then.py' In[2]: %run name but this actually tries to run 'name.py' , which is not what I want to do. Is there a general way to turn variables into strings? Something like the following: In[3]: %run %name% IPython expands variables with $name , bash-style. This is true for all magics , not just %run . So you would do: In [1]: filename = "myscript.py" In [2]: %run $filename ['myscript.py'] myscript.py contains: import sys print(sys.argv) Via Python's fancy string formatting, you can even put expressions

How to use pipe in IPython

一笑奈何 提交于 2019-11-27 22:09:49
In a Linux terminal, when the output of one command is too long to read in one page, I can do this: cat file | less so that I can read and scroll up and down the output from the cat file. How can I do this in IPython? For example, I tried this and it didn't work: whos | less My original problem is that the output from whos is too much to be seen by doing Shift+Page Up and I don't want to change the scroll buffer. In IPython, you can use %page obj to show the object obj using your standard pager (usually less ). Alternatively, you can increase the scroll buffer of your terminal, which might be