Skip to content

Commit

Permalink
fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Dec 16, 2024
1 parent 249f874 commit 7c81e33
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 5 deletions.
50 changes: 46 additions & 4 deletions 00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@
"output_type": "stream",
"text": [
"00_core.ipynb\r\n",
"LICENSE\r\n",
"MANIFEST.in\r\n",
"CHANGELOG.bak\r\n",
"CHANGELOG.md\r\n",
"\n"
]
}
Expand Down Expand Up @@ -169,8 +169,8 @@
"output_type": "stream",
"text": [
"00_core.ipynb\r\n",
"LICENSE\r\n",
"MANIFEST.in\n"
"CHANGELOG.bak\r\n",
"CHANGELOG.md\n"
]
}
],
Expand Down Expand Up @@ -280,6 +280,7 @@
" @no_var_expand\n",
" def psh(self, line, cell=None):\n",
" \"Run line or cell in persistent shell\"\n",
" if not cell and not line: line = 'echo'\n",
" if cell: cell = shell_replace(cell, self.shell)\n",
" if line: line = shell_replace(line, self.shell)\n",
" args = self.psh.parser.parse_args(line.split())\n",
Expand Down Expand Up @@ -484,6 +485,47 @@
"%psh ls | head -@{n}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3de04eee",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"starting\r\n",
"[1] 99418\n"
]
}
],
"source": [
"%%psh\n",
"echo starting\n",
"(sleep 1; echo finished) &"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58be93c1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"finished\r\n",
"\r\n",
"[1]+ Done ( sleep 1; echo finished )\n"
]
}
],
"source": [
"%psh"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ 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:

Expand All @@ -88,6 +90,8 @@ 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:
Expand Down Expand Up @@ -120,6 +124,50 @@ listing:
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
Expand Down
110 changes: 109 additions & 1 deletion index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@
"## Features"
]
},
{
"cell_type": "markdown",
"id": "f723a8d0",
"metadata": {},
"source": [
"### Cell magic"
]
},
{
"cell_type": "markdown",
"id": "7458388b",
Expand Down Expand Up @@ -218,6 +226,14 @@
"rm tmp"
]
},
{
"cell_type": "markdown",
"id": "18dbf666",
"metadata": {},
"source": [
"### Variable expansion"
]
},
{
"cell_type": "markdown",
"id": "aa737da1",
Expand Down Expand Up @@ -301,7 +317,99 @@
},
{
"cell_type": "markdown",
"id": "c7acc94b",
"id": "09de8f02",
"metadata": {},
"source": [
"### Background tasks"
]
},
{
"cell_type": "markdown",
"id": "e47e6917",
"metadata": {},
"source": [
"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):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3de04eee",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"starting\r\n",
"[1] 99418\n"
]
}
],
"source": [
"%%psh\n",
"echo starting\n",
"(sleep 1; echo finished) &"
]
},
{
"cell_type": "markdown",
"id": "dd6fde98",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97498951",
"metadata": {},
"outputs": [],
"source": [
"from time import sleep\n",
"sleep(1.1)"
]
},
{
"cell_type": "markdown",
"id": "fc7a24c6",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58be93c1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"finished\r\n",
"\r\n",
"[1]+ Done ( sleep 1; echo finished )\n"
]
}
],
"source": [
"%psh"
]
},
{
"cell_type": "markdown",
"id": "69f0197a",
"metadata": {},
"source": [
"### Flags"
]
},
{
"cell_type": "markdown",
"id": "c8314764",
"metadata": {},
"source": [
"You can get help on the `%psh` magic's options using `-h`."
Expand Down
1 change: 1 addition & 0 deletions pshnb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def _timeout(self, timeout=2):
@no_var_expand
def psh(self, line, cell=None):
"Run line or cell in persistent shell"
if not cell and not line: line = 'echo'
if cell: cell = shell_replace(cell, self.shell)
if line: line = shell_replace(line, self.shell)
args = self.psh.parser.parse_args(line.split())
Expand Down

0 comments on commit 7c81e33

Please sign in to comment.