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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file interactive-system-magic-0.2.0.tar.gz.

File metadata

  • Download URL: interactive-system-magic-0.2.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.5 Linux/5.15.0-1014-azure

File hashes

Hashes for interactive-system-magic-0.2.0.tar.gz
Algorithm Hash digest
SHA256 34fa2a0edb43c0785f01c7630f2661dd96b48a872c1047cfeedc4f839813c572
MD5 1a8b746fcc9e0b51dacd8b584cd36e2e
BLAKE2b-256 c8df542c7cedc6dcc5624b285692bf7ee6a1979a4646760f84f6e1d50d551f3b

See more details on using hashes here.

File details

Details for the file interactive_system_magic-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for interactive_system_magic-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa73310d079d9b7b9da92018c15fc4f341c7e498701317ae7fec57782a5d70ce
MD5 76bbed6222f1f7a636cda7614d4ab8ed
BLAKE2b-256 6bb877964f2ab746055d1578811b0ae5468b254f5892411faf1b9e02ddf256d2

See more details on using hashes here.

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