Skip to main content

A bash kernel for Jupyter

Project description

https://mybinder.org/badge_logo.svg

A Jupyter kernel for bash

Installation

This requires IPython 3.

pip install bash_kernel
python -m bash_kernel.install

To use it, run one of:

jupyter notebook
# In the notebook interface, select Bash from the 'New' menu
jupyter qtconsole --kernel bash
jupyter console --kernel bash

pipx and “externally managed” environments

A recent-ish PEP 668 recommends that users install Python applications with pipx rather than global installs with pip. This is strongly suggested/enforced in current Linux distros. Because bash_kernel needs an extra step to actually work after installing with pip or pipx, this causes some inconvenience.

First, one must install the Jupyter ecosystem with pipx, and then inject bash_kernel (and any other bits of the jupyter ecosystem you use, like papermill) into the same pipx venv.

pipx install --include-deps jupyter
pipx inject --include-apps --include-deps jupyter bash_kernel

One then must manually find the corresponding venv, activate it, and run python -m bash_kernel.install within that virtual env. If done outside it, this won’t work as bash_kernel is not installed in the global environment.

cd ~/.local/pipx/venvs/jupyter/
source bin/activate
python -m bash_kernel.install
deactivate

Of course, one can also install bash_kernel to the global environement thusly:

pip install --break-system-packages juptyer bash_kernel
python -m bash_kernel.install

Requirements of Bash

Bash kernel directly interacts with bash, and therefore requires a functioning interactive build of bash. In nearly all cases this will be the default, however some distributions remove GNU readline or other interactivity features of bash. Almost always, these features are provided in a separate, more complete bash package, which should be installed. See for example https://github.com/takluyver/bash_kernel/issues/142.

Displaying Rich Content

To use specialized content (images, html, etc) this file defines (in build_cmds()) bash functions that take the contents as standard input. Currently, display (images), displayHTML (html) and displayJS (javascript) are supported.

Example:

cat dog.png | display
echo "<b>Dog</b>, not a cat." | displayHTML
echo "alert('Hello from bash_kernel\!');" | displayJS

Updating Rich Content Cells

If one is doing something that requires dynamic updates, one can specify a unique display_id, which should be a string name. On each update, the contents will be replaced by the new value. Example:

display_id="id_${RANDOM}"
((ii=0))
while ((ii < 10)) ; do
    echo "<div>${ii}</div>" | displayHTML $display_id
    ((ii = ii+1))
    sleep 1
done

The same works for images and javascript content.

Remember to create always a new id each time the cell is executed, otherwise it will try to display on an HTML element that no longer exists (they are erased each time a cell is re-run).

Programmatically Generating Rich Content

Alternatively one can simply generate the rich content to a file in /tmp (or $TMPDIR) and then output the corresponding (to the mimetype) context prefix "_TEXT_SAVED_*" constant. So one can write programs (C++, Go, Rust, etc.) that generates rich content appropriately, when within a notebook.

The environment variable “NOTEBOOK_BASH_KERNEL_CAPABILITIES” will be set with a comma separated list of the supported types (currently “image,html,javascript”) that a program can check for.

To output to a particular “display_id”, to allow update of content (e.g: dynamically updating/generating a plot from a command line program), prefix the filename with “(<display_id>)”. E.g: a line to display the contents of /tmp/myHTML.html to a display id “id_12345” would look like:

bash_kernel: saved html data to: (id_12345) /tmp/myHTML.html

More Information

For details of how this works, see the Jupyter docs on wrapper kernels, and Pexpect’s docs on the replwrap module.

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

bash_kernel-0.10.0.tar.gz (628.9 kB view details)

Uploaded Source

Built Distribution

bash_kernel-0.10.0-py2.py3-none-any.whl (617.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file bash_kernel-0.10.0.tar.gz.

File metadata

  • Download URL: bash_kernel-0.10.0.tar.gz
  • Upload date:
  • Size: 628.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for bash_kernel-0.10.0.tar.gz
Algorithm Hash digest
SHA256 2ed5a0a416c418d1d751e71505bd6c889e12157444b50c4287a685da775c28ca
MD5 d79bcdb405c28e2b597b87b9f5348b9f
BLAKE2b-256 20025bf4e039573da57dfcb1b5ccc132c473a7e1d69d31d57a9205d03ddc57e5

See more details on using hashes here.

File details

Details for the file bash_kernel-0.10.0-py2.py3-none-any.whl.

File metadata

  • Download URL: bash_kernel-0.10.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 617.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for bash_kernel-0.10.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 da28513230f5307c7a49e87352366304dc52b66faa207d6caed1b2bdec03c1fa
MD5 e5e0eeb72d9ee19bb99f8477cfb4fd2d
BLAKE2b-256 50103753926476ac077d1f4548369c0ff2a5600e2c8fa5aef3cfcc5b10cf6155

See more details on using hashes here.

Supported by

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