diff --git a/00_core.ipynb b/00_core.ipynb index f941b00..09150b2 100644 --- a/00_core.ipynb +++ b/00_core.ipynb @@ -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" ] } @@ -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" ] } ], @@ -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", @@ -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, diff --git a/README.md b/README.md index 5dfe967..6452bdf 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: @@ -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 diff --git a/index.ipynb b/index.ipynb index 624ec10..aaa4edb 100644 --- a/index.ipynb +++ b/index.ipynb @@ -167,6 +167,14 @@ "## Features" ] }, + { + "cell_type": "markdown", + "id": "f723a8d0", + "metadata": {}, + "source": [ + "### Cell magic" + ] + }, { "cell_type": "markdown", "id": "7458388b", @@ -218,6 +226,14 @@ "rm tmp" ] }, + { + "cell_type": "markdown", + "id": "18dbf666", + "metadata": {}, + "source": [ + "### Variable expansion" + ] + }, { "cell_type": "markdown", "id": "aa737da1", @@ -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`." diff --git a/pshnb/core.py b/pshnb/core.py index 5d31b01..763e2b5 100644 --- a/pshnb/core.py +++ b/pshnb/core.py @@ -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())