#!/bin/bash
At work recently, I had to call an internal REST API for some data I needed to process. To try the API out, I fired up Ubuntu in my Windows Subsystem for Linux and ran a cURL command to try out the interface. That went well, so I created a new Jupyter Notebook in which to call the API–I wanted to take the response data, load them into a pandas dataframe, and create a chart. Easy, right?
So, I called the API with requests and promptly received a SSL “bad handshake” error. Like many others, I struggled to resolve this error. Clearly, the server hosting that API was misconfigured in some way. However, I didn’t own the server and had no real recourse to get the issue fixed, so I decided to call cURL directly from my notebook; this led me to the bash magic command.
With the bash magic command, you can tell Jupyter Notebook to run all the commands in your cell as if you were executing them at a bash command prompt…even if you’re running Jupyter Notebook on a Windows operating system. How cool is that?! Furthermore, with the out argument, you can pipe all your cell output to a variable for easy processing. Check this out:
(Note: I’m using the free lyrics API from lyrics.ovh in my examples below)
%%bash --out lyrics1
curl https://private-anon-cd823708f8-lyricsovh.apiary-proxy.com/v1/the%20beatles/here%20comes%20the%20sun
The Bash magic command returns output as a String. Since the output is really JSON, I can easily convert it to JSON with the loads function:
import json
json_lyrics1 = json.loads(lyrics1)
print(json_lyrics1['lyrics'])
What if you want to call Bash commands in a loop?
Suppose I wanted to get the lyrics of multiple Beatles songs…now what do I do? Well, here’s one hack I came up with: do in-line calls to the shell.
song_list = ['yesterday', 'yellow%20submarine', 'eleanor%20rigby']
song_lyrics = []
for song in song_list:
lyric = !wsl curl -s 'https://private-anon-cd823708f8-lyricsovh.apiary-proxy.com/v1/the%20beatles/{song}'
song_lyrics.append(lyric)
song_lyrics
Here are a few things to note with my shell operation:
- Since my operating system is Windows 10, I’m actually shelling out to the Windows command shell, not bash. However, since I have WSL installed on my machine, I can use wsl.exe to run commands in that shell. So, I’m basically calling a shell within a shell to ultimately execute my bash operation.
- With the braces syntax, I can pass the value of my song variable to my shell command.
- I pass the silent argument (-s) to cURL to suppress the noise cURL would normally send back to Jupyter Notebook. This allows me to pass just the JSON response to my variable lyric.
One challenge with this approach is that the shell command returns a SList. Basically, a list of strings. I should be able to join those lists together, though, and then convert them to JSON with the loads function:
song_list = ['yesterday', 'yellow%20submarine', 'eleanor%20rigby']
song_lyrics = []
for song in song_list:
lyric = !wsl curl -s 'https://private-anon-cd823708f8-lyricsovh.apiary-proxy.com/v1/the%20beatles/{song}'
json_lyrics = json.loads(''.join(lyric))
song_lyrics.append(json_lyrics)
song_lyrics
And now I have a list of JSON objects (or dictionaries) to work with. Awesome!
For more on the bash magic command, check out this excellent article. Go here to get all my example code.
Recent Comments