Skip to main content

An IPython magic to run system commands interactively

Project description

Setup

First, you need to load the extension with:

%load_ext interactive_system_magic

Now, you can start using it.

Running programs

If you make a code cell with:

%prog echo blah

then it will run the program echo blah and put the result in the output:

blah

So far, this is just like the ! system call syntax. However, it also allows you to send input into a program by using it in cell mode:

%%prog bc --quiet
1+1

which will call bc --quiet (the --quiet part is to suppress the info on startup) and then pass in 1+1 on the stdin. bc will read this and output the result:

2

If you want to use this to demonstrate what the user would see if they ran this manually on a terminal, i.e. to include the input as well as the output, you can use the interactive -i flag:

%%prog -i bc --quiet
1+1

which gives:

1+1
2

Finally, if you want to be able to respond to prompts in the program’s output, then you can use special syntax to specify the prompt you are looking for, and the thing to respond to it with. In this case, we are calling the python program and waiting for its >>> prompt and then sending print("hello")

%%prog -i -d [] python -q
[>>> ]print("hello")

which gives:

>>> print("hello")
hello
>>>

Run Python scripts

As a shortcut, you can also run any Python scripts you have using the same interpreter that IPython or the Jupyter notebook is running with. If you make a Python script with (or use an existing one of course):

%%writefile foo.py

print("This is a script")

you can call it with:

%run_python_script foo.py

and get:

..code-block:

This is a script

This magic supports the same cell-mode commands and interactive options as %%prog.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

interactive-system-magic-0.2.0.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

interactive_system_magic-0.2.0-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page