DEV Community

Cover image for Jupyter and bash, better than peanut butter and chocolate
Sophia Parafina
Sophia Parafina

Posted on

Jupyter and bash, better than peanut butter and chocolate

As a developer advocate, I fall into the "show me" camp. That means I like to show code when writing about a service or feature. In articles, code is presented as copy and paste blocks, such as this:

text = "World!"
print("Hello",text)
Enter fullscreen mode Exit fullscreen mode

If I was doing a live demo, I could use a REPL if the language supported it.

Python 3.10.9 (main, Dec 15 2022, 18:18:30) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> text = "World!"
>>> print("Hello",text)
Hello World!
Enter fullscreen mode Exit fullscreen mode

I'm a terrible typist and standing behind a podium while speaking to the audience is painful to watch. One way I've approached this problem on the command line is to use read command in bash to display text, then execute the command or program when the enter/return key is pressed.

echo "This is a python program"
echo ""
echo "text = "World!"
echo "print("Hello",text)
echo ""
read -p -t 500 ""
python hello.py
Enter fullscreen mode Exit fullscreen mode

This works fairly well, especially when using the command line to demonstrate setting up a service. There are two drawbacks, first, depending on the complexity of the demonstration the bash script can be very long. The second drawback is that if a command fails, then you can't repeat it. For example, if you create a cloud service, then try to move to the next step before the service is deployed, the demonstration fails.

What I want is the functionality of a Jupyter notebook. The default Jupyter notebook only includes a Python kernel, which supports executing simple shell commands, e.g.:

!echo "Hello World!"
Enter fullscreen mode Exit fullscreen mode

However, you can install a bash kernel for Jupyter notebooks.

$ python -m bash_kernel.install
$ jupyter notebook
Enter fullscreen mode Exit fullscreen mode

In the notebook interface, choose the Bash and away we go.

menu item

The Python dependency and Jupyter setup can be a drawback. Aside from that, there are advantages. First, if a command fails, it can be repeated. Second, notebooks can be shared with the audience after the demonstration. The third and most import advantage is, your audience doesn't have to watch you type commands. If you have to give a live demonstration, consider using a Jupyter notebook.

Top comments (2)

Collapse
 
mpriour profile image
Matt Priour

Great advice.

Collapse
 
derlin profile image
Lucy Linder

I like the idea, may be handy for talks and presentations. Thanks for the tip!