ipython-magic

How do I provide inline input to an IPython (notebook) shell command?

和自甴很熟 提交于 2019-12-13 14:01:39
问题 I want to put together an IPython notebook with some shell commands and their input. In the bash prompt I can use "here-document" syntax: bash-3.2$ mysql -u root <<END_IPUT > use mydb; > show tables; > END_INPUT How do I get the same effect in IPython, and specifically in a jupyter notebook? I know how to execute shell commands as IPython as "line magics" or "cell magics", e.g.: In [7]: !! ls -tF Out[7]: ['Demo-notebook.ipynb', 'createdb.sql', ... I've looked at IPython as a system shell,

Import .py flie into Jupyter Notebook line by line

白昼怎懂夜的黑 提交于 2019-12-12 01:15:30
问题 I prefer to write my python code on VSCode because of its intellisense and autocomplete features. But I would rather see and debug the code on a Jupyter notebook to better visualize the data I am working with. I know I can load any file into Jupyter by using the magical %load or %loadpy commands. But they load the entire file into a single cell. Since I wanted to see the intermediary results of certain operations, I would like to import the file in such a way that each line on the file is

IPython - importing/populating namespace with custom magic

北战南征 提交于 2019-12-11 11:27:22
问题 The %pylab magic in IPython imports a bunch of functions into the user's workspace, which is very convenient. Looking at the code, it's not at all obvious how this is done. What I have so far is a magic function in my startup folder: from IPython.core.magic import register_line_magic @register_line_magic def import_my_functions(line): """ Import functions into namespace somehow.... e.g. import numpy as np """ It then should be possible: In[1]: %import_my_functions imported the following:

Ignore IPython magic in python

天涯浪子 提交于 2019-12-11 08:46:35
问题 What is the best way to ignore IPython magic when running scripts using the python interpreter? I often include IPython magic in my script files because it work with the code interactively. For example, with the autoreload magic, I don't have to keep reload -ing the modules after I make some changes and fix bugs: %load_ext autoreload %autoreload 2 However, when I try to run this script using a usual python interpreter, I get an error: File "<string>", line 1 %load_ext autoreload ^ SyntaxError

timing in python with %timeit %%timeit how to keep/retain values for later use

无人久伴 提交于 2019-12-10 18:38:47
问题 I am trying to measure the execution time of different parts of my code, a few lines each. I am doing this with %%timeit, however after I execute a cell, I find that the values calculated for variables in the cell are not kept in memory for next cells, as in the following example. Why does this happen? Is there a way to retain the values so I can use them in the rest of the program? In [1]: %%timeit ...: dog='dog' 100000000 loops, best of 3: 16.2 ns per loop In [2]: print (dog) --------------

Jupyter Notebook time profiling

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 15:25:24
问题 So I installed jupyter notebook through anaconda and I am working on python 3 kernel. I am trying to do Time profiling using %time magic command as show here The problem is that it only prints the Wall Time not the CPU Time using %time or %%time does not help %time prints the wall time for the first line only %%time prints the wall time for the whole cell I am not sure if there any specif config to print the CPU time EDIT To clarify using %%time should print two metrics for the whole cell

Pipe Ipython magic output to a variable?

痴心易碎 提交于 2019-12-09 02:10:28
问题 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 回答1: 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

How to show source code of a package function in IPython notebook

╄→гoц情女王★ 提交于 2019-12-08 15:29:42
问题 For teaching purposes I want an IPython notebook that displays (as output from a cell) the function source code, but I want to be able to reference this in multiple notebooks. Hence I would like to display the function code, in a similar way to using the %psource magic, but appropriately syntax highlighted. This is a similar question to this question, but I want to be able to apply it to a single function within a file, rather than to the complete file at once. Using the suggestion from the

Magic function `bash ` not found

廉价感情. 提交于 2019-12-08 06:44:34
问题 I have a bunch of simulations that I want to run on a high-performance cluster, on which I should make reservations to get computing time. Since the reservations are bounded by time, I am developing an automation script that I can scp into the cluster and run. This script will then download the relevant simulation files, run them, and upload the results. Part of this automation script is in bash ( cp , scp , etc) and the rest is in python. In order to develop this automation, I am using an

How to add a custom flag to IPython's magic commands? (.ipy files)

允我心安 提交于 2019-12-06 15:59:49
Is it possible to add a custom flag to IPython's magic command? To be more specific, I want to use the %run command with a homemade flag: %run script.ipy --flag "option" and be able to use "option" inside the script. For .py files, the answer is provided here: Command Line Arguments In Python As indicated in the comments, this question isn't just about handling commandline arguments in a Python script. It's about handling them in an .ipy file run via %run . If I create test.ipy as import sys print(sys.argv) and run it from shell, I see the commandline arguments: 1223:~/mypy$ python3 test.ipy