pshnb


Namepshnb JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/answerdotai/pshnb
SummaryAI magic
upload_time2024-12-16 04:32:40
maintainerNone
docs_urlNone
authorJeremy Howard
requires_python>=3.9
licenseApache Software License 2.0
keywords nbdev jupyter notebook python bash
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Using `psh` magics


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

`pshnb` adds `%psh` and `%%psh` functions to Jupyter and IPython, which
execute expressions in a persistent shell.

## Installation

Install pshnb with:

    pip install pshnb

Once that’s complete, you can install the magics to all IPython and
Jupyter sessions automatically by running in your terminal:

    pshnb_install

## What’s the point?

In jupyter and ipython, you can run a shell command using the `!`
prefix:

``` python
!pwd
```

    /Users/jhoward/Documents/GitHub/pshnb

However, each time you run this command, a new shell is created and then
removed, so for instance, `cd` doesn’t actually do anything if you run
another command afterwards:

``` python
!cd ..
!pwd
```

    /Users/jhoward/Documents/GitHub/pshnb

As you see from the `!pwd` output, the directory hasn’t actually
changed!

`%psh`, on the other hand, creates a *persistent* shell, which solves
this problem:

``` python
%psh pwd
```

    /Users/jhoward/Documents/GitHub/pshnb

``` python
%psh cd ..
%psh pwd
```

    /Users/jhoward/Documents/GitHub

With `%psh`, you can implement, and document in notebooks, multi-step
stateful shell interactions, including setting environment variables,
sourcing scripts, and changing directories.

## Features

### Cell magic

You can use the `%%psh` cell magic to run multi-line shell commands,
such as here-docs. For instance:

``` python
%%psh
cat > tmp << EOF
hi
there
EOF
```

This creates a file called `tmp` containing two lines. Let’s check it
worked, and then remove it – as you see, you can also use the cell magic
to run multiple commands:

``` python
%%psh
cat tmp
rm tmp
```

    hi
    there

### Variable expansion

You can pipe commands together just like in a regular shell, and use
standard unix utilities like `head` to process the output. For instance,
here we show just the first 3 lines of the directory listing:

``` python
%psh ls | head -3
```

You can use Python variables in your shell commands by prefixing them
with `@{}`. For instance, here we create a variable `n` and then display
it using `echo`:

``` python
n = 2
```

``` python
%psh echo @{n}
```

    2

Here we use `n` to show just the first two entries from the directory
listing:

``` python
%psh ls | head -@{n}
```

    ContextKit
    FastHTML-Gallery

### Background tasks

You can run commands in the background in the shell by adding `&` at the
end of a command. The parentheses `(...)` group commands together to run
as one unit. In this example, we first print “starting”, and then create
a background process that will wait for 1 second (using `sleep 1`) and
then print “finished”. The shell immediately shows us “starting” and
tells us it created background process number 1 (with a process ID):

``` python
%%psh
echo starting
(sleep 1; echo finished) &
```

    starting
    [1] 99418

For this demonstration, we wait for 1.1 seconds (slightly longer than
the background process needs). During this time, the background process
will complete in the background. But we won’t see any output from it
yet.

``` python
from time import sleep
sleep(1.1)
```

The next time we run *any* `psh` magic we will also see any output that
has occurred in our persistent shell since the last command. Run `%psh`
by itself to *only* see those updates, e.g here we see that “finished”
was printed, and the shell tells us that background job 1 completed
successfully.

``` python
%psh
```

    finished

    [1]+  Done                    ( sleep 1; echo finished )

### Flags

You can get help on the `%psh` magic’s options using `-h`.

``` python
%psh -h
```

    ::

      %psh [-h] [-r] [-o] [-x] [-X] [-s] [-S] [-t TIMEOUT] [command ...]

    Run line or cell in persistent shell

    positional arguments:
      command               The command to run

    options:
      -h, --help            Show this help
      -r, --reset           Reset the shell interpreter
      -o, --obj             Return this magic object
      -x, --expand          Enable variable expansion
      -X, --no-expand       Disable variable expansion
      -s, --sudo            Enable sudo
      -S, --no-sudo         Disable sudo
      -t TIMEOUT, --timeout TIMEOUT
                            Set timeout in seconds

You can reset the shell to its initial state using the `-r` flag. Let’s
first check our current directory:

``` python
%psh pwd
```

    /Users/jhoward/Documents/GitHub

Now let’s reset the shell:

``` python
%psh -r
```

As you can see, after resetting we’re back in our starting directory:

``` python
%psh pwd
```

    /Users/jhoward/Documents/GitHub/pshnb

The `-s` flag enables `sudo` mode, which runs commands as the root user,
and `-S` disables it. For instance, here we first enable `sudo` mode:

``` python
%psh -s
```

Then we can check which user we’re running as:

``` python
%psh whoami
```

    root

As you can see, we’re now running as `root`. We can disable `sudo` mode:

``` python
%psh -S
```

And when we check again, we’re back to our regular user:

``` python
%psh whoami
```

    jhoward

You can set a timeout (in seconds) using the `-t` flag, which will raise
a `TIMEOUT` exception if a command takes too long. For instance, here we
set a 1-second timeout:

``` python
%psh -t 1
```

Then we try to run a command that sleeps for 2 seconds – since this is
longer than our timeout, we’ll get a timeout error:

``` python
try: get_ipython().run_line_magic('psh', 'sleep 2')
except TIMEOUT: print("timed out")
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/answerdotai/pshnb",
    "name": "pshnb",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "nbdev jupyter notebook python bash",
    "author": "Jeremy Howard",
    "author_email": "info@answer.ai",
    "download_url": "https://files.pythonhosted.org/packages/62/5b/80d9896f2e595d2aa27660400252de4ab796823fe491a0729731c4122f77/pshnb-0.0.2.tar.gz",
    "platform": null,
    "description": "# Using `psh` magics\n\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n`pshnb` adds `%psh` and `%%psh` functions to Jupyter and IPython, which\nexecute expressions in a persistent shell.\n\n## Installation\n\nInstall pshnb with:\n\n    pip install pshnb\n\nOnce that\u2019s complete, you can install the magics to all IPython and\nJupyter sessions automatically by running in your terminal:\n\n    pshnb_install\n\n## What\u2019s the point?\n\nIn jupyter and ipython, you can run a shell command using the `!`\nprefix:\n\n``` python\n!pwd\n```\n\n    /Users/jhoward/Documents/GitHub/pshnb\n\nHowever, each time you run this command, a new shell is created and then\nremoved, so for instance, `cd` doesn\u2019t actually do anything if you run\nanother command afterwards:\n\n``` python\n!cd ..\n!pwd\n```\n\n    /Users/jhoward/Documents/GitHub/pshnb\n\nAs you see from the `!pwd` output, the directory hasn\u2019t actually\nchanged!\n\n`%psh`, on the other hand, creates a *persistent* shell, which solves\nthis problem:\n\n``` python\n%psh pwd\n```\n\n    /Users/jhoward/Documents/GitHub/pshnb\n\n``` python\n%psh cd ..\n%psh pwd\n```\n\n    /Users/jhoward/Documents/GitHub\n\nWith `%psh`, you can implement, and document in notebooks, multi-step\nstateful shell interactions, including setting environment variables,\nsourcing scripts, and changing directories.\n\n## Features\n\n### Cell magic\n\nYou can use the `%%psh` cell magic to run multi-line shell commands,\nsuch as here-docs. For instance:\n\n``` python\n%%psh\ncat > tmp << EOF\nhi\nthere\nEOF\n```\n\nThis creates a file called `tmp` containing two lines. Let\u2019s check it\nworked, and then remove it \u2013 as you see, you can also use the cell magic\nto run multiple commands:\n\n``` python\n%%psh\ncat tmp\nrm tmp\n```\n\n    hi\n    there\n\n### Variable expansion\n\nYou can pipe commands together just like in a regular shell, and use\nstandard unix utilities like `head` to process the output. For instance,\nhere we show just the first 3 lines of the directory listing:\n\n``` python\n%psh ls | head -3\n```\n\nYou can use Python variables in your shell commands by prefixing them\nwith `@{}`. For instance, here we create a variable `n` and then display\nit using `echo`:\n\n``` python\nn = 2\n```\n\n``` python\n%psh echo @{n}\n```\n\n    2\n\nHere we use `n` to show just the first two entries from the directory\nlisting:\n\n``` python\n%psh ls | head -@{n}\n```\n\n    ContextKit\n    FastHTML-Gallery\n\n### Background tasks\n\nYou can run commands in the background in the shell by adding `&` at the\nend of a command. The parentheses `(...)` group commands together to run\nas one unit. In this example, we first print \u201cstarting\u201d, and then create\na background process that will wait for 1 second (using `sleep 1`) and\nthen print \u201cfinished\u201d. The shell immediately shows us \u201cstarting\u201d and\ntells us it created background process number 1 (with a process ID):\n\n``` python\n%%psh\necho starting\n(sleep 1; echo finished) &\n```\n\n    starting\n    [1] 99418\n\nFor this demonstration, we wait for 1.1 seconds (slightly longer than\nthe background process needs). During this time, the background process\nwill complete in the background. But we won\u2019t see any output from it\nyet.\n\n``` python\nfrom time import sleep\nsleep(1.1)\n```\n\nThe next time we run *any* `psh` magic we will also see any output that\nhas occurred in our persistent shell since the last command. Run `%psh`\nby itself to *only* see those updates, e.g here we see that \u201cfinished\u201d\nwas printed, and the shell tells us that background job 1 completed\nsuccessfully.\n\n``` python\n%psh\n```\n\n    finished\n\n    [1]+  Done                    ( sleep 1; echo finished )\n\n### Flags\n\nYou can get help on the `%psh` magic\u2019s options using `-h`.\n\n``` python\n%psh -h\n```\n\n    ::\n\n      %psh [-h] [-r] [-o] [-x] [-X] [-s] [-S] [-t TIMEOUT] [command ...]\n\n    Run line or cell in persistent shell\n\n    positional arguments:\n      command               The command to run\n\n    options:\n      -h, --help            Show this help\n      -r, --reset           Reset the shell interpreter\n      -o, --obj             Return this magic object\n      -x, --expand          Enable variable expansion\n      -X, --no-expand       Disable variable expansion\n      -s, --sudo            Enable sudo\n      -S, --no-sudo         Disable sudo\n      -t TIMEOUT, --timeout TIMEOUT\n                            Set timeout in seconds\n\nYou can reset the shell to its initial state using the `-r` flag. Let\u2019s\nfirst check our current directory:\n\n``` python\n%psh pwd\n```\n\n    /Users/jhoward/Documents/GitHub\n\nNow let\u2019s reset the shell:\n\n``` python\n%psh -r\n```\n\nAs you can see, after resetting we\u2019re back in our starting directory:\n\n``` python\n%psh pwd\n```\n\n    /Users/jhoward/Documents/GitHub/pshnb\n\nThe `-s` flag enables `sudo` mode, which runs commands as the root user,\nand `-S` disables it. For instance, here we first enable `sudo` mode:\n\n``` python\n%psh -s\n```\n\nThen we can check which user we\u2019re running as:\n\n``` python\n%psh whoami\n```\n\n    root\n\nAs you can see, we\u2019re now running as `root`. We can disable `sudo` mode:\n\n``` python\n%psh -S\n```\n\nAnd when we check again, we\u2019re back to our regular user:\n\n``` python\n%psh whoami\n```\n\n    jhoward\n\nYou can set a timeout (in seconds) using the `-t` flag, which will raise\na `TIMEOUT` exception if a command takes too long. For instance, here we\nset a 1-second timeout:\n\n``` python\n%psh -t 1\n```\n\nThen we try to run a command that sleeps for 2 seconds \u2013 since this is\nlonger than our timeout, we\u2019ll get a timeout error:\n\n``` python\ntry: get_ipython().run_line_magic('psh', 'sleep 2')\nexcept TIMEOUT: print(\"timed out\")\n```\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "AI magic",
    "version": "0.0.2",
    "project_urls": {
        "Homepage": "https://github.com/answerdotai/pshnb"
    },
    "split_keywords": [
        "nbdev",
        "jupyter",
        "notebook",
        "python",
        "bash"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "214117df3517e1a01b4bd5c4e8d3db7c6ab7e323e748b6690abd597dcc1a217a",
                "md5": "2f2f6b04f7273999df59b69ec559a96b",
                "sha256": "80bd389c1f4feb316f728b8500ef222dc08c62979c120a7c02d8178a5bbe0536"
            },
            "downloads": -1,
            "filename": "pshnb-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2f2f6b04f7273999df59b69ec559a96b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 10675,
            "upload_time": "2024-12-16T04:32:37",
            "upload_time_iso_8601": "2024-12-16T04:32:37.834189Z",
            "url": "https://files.pythonhosted.org/packages/21/41/17df3517e1a01b4bd5c4e8d3db7c6ab7e323e748b6690abd597dcc1a217a/pshnb-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "625b80d9896f2e595d2aa27660400252de4ab796823fe491a0729731c4122f77",
                "md5": "174d67274d8f27b07518e89f2ce87469",
                "sha256": "91cb3f37839777bc2191dc2e627b9527340cfdb61fd332b8be9db6d7d609c21c"
            },
            "downloads": -1,
            "filename": "pshnb-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "174d67274d8f27b07518e89f2ce87469",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 11518,
            "upload_time": "2024-12-16T04:32:40",
            "upload_time_iso_8601": "2024-12-16T04:32:40.108711Z",
            "url": "https://files.pythonhosted.org/packages/62/5b/80d9896f2e595d2aa27660400252de4ab796823fe491a0729731c4122f77/pshnb-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-16 04:32:40",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "answerdotai",
    "github_project": "pshnb",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pshnb"
}
        
Elapsed time: 1.55294s