import requests
import urllib
import matplotlib.pyplot as plt
from IPython.display import display, Image
from IPython.core.display import HTML 
import requests
%matplotlib inline

Print Debugging with a print_debug statement

def display_image(url):
    print_debug("About to display an image")
    print_debug("The url is", url)
    response = urllib.request.urlopen(url).read()
    img = Image(response, width=200)
    display(img)
display_image("https://i.ytimg.com/vi/z_AbfPXTKms/hqdefault.jpg")
About to display an image
The url is https://i.ytimg.com/vi/z_AbfPXTKms/hqdefault.jpg

jpeg

response = requests.get("https://api.spotify.com/v1/search?query=lil&offset=0&limit=20&type=artist")
data = response.json()
data.keys()
dict_keys(['artists'])
data['artists'].keys()
dict_keys(['href', 'total', 'items', 'limit', 'offset', 'previous', 'next'])
artists = data['artists']['items']
artists[0].keys()
dict_keys(['type', 'external_urls', 'id', 'name', 'followers', 'href', 'uri', 'images', 'popularity', 'genres'])
artists[0]['images']
[{'height': 1239,
  'url': 'https://i.scdn.co/image/cf012139c3b8681b46a66bae70558a8a336ab231',
  'width': 1000},
 {'height': 793,
  'url': 'https://i.scdn.co/image/fffd48d60e27901f6e9ce99423f045cb2b893944',
  'width': 640},
 {'height': 248,
  'url': 'https://i.scdn.co/image/bf03141629c202e94b206f1374a39326a9d8c6ca',
  'width': 200},
 {'height': 79,
  'url': 'https://i.scdn.co/image/521f99f2469883b8806a69a3a2487fdd983bd621',
  'width': 64}]
# Change all of your 'print' statements
# to 'print_debug' and then to turn them off
# you can just change 'True' below to 'False'
# so that it never runs
# This is USUALLY a waste of time but
# hey you might as well learn to do it
def print_debug(*things_to_print):
    debug_mode = False
    if debug_mode:
        print(*things_to_print)
print_debug("We are debugging")
x = 500
print_debug("We are debugging the variable x:", x)
def display_artist_image(artist):
    print_debug("1. Inside display_artist_image, looking at an artist")
    print_debug(artist)
    #print("2. display_artist_image: The url is", artist['images'][0]['url'])
    # print("2. The first image is", artist['images'][0])
    print_debug("2. The images are", artist['images'])
    if len(artist['images']) < 1:
        return
    display_image(artist['images'][0]['url'])
# display_artist_image("http://www.google.com/cats.jpg")
unpopular_response = requests.get("https://api.spotify.com/v1/search?query=lil&offset=1200&limit=20&type=artist")
unpopular_data = unpopular_response.json()
unpopular_artists = unpopular_data['artists']['items']
unpopular_artists
[{'external_urls': {'spotify': 'https://open.spotify.com/artist/4d5e17qy0VPTjsdfICnkO4'},
  'followers': {'href': None, 'total': 36},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/4d5e17qy0VPTjsdfICnkO4',
  'id': '4d5e17qy0VPTjsdfICnkO4',
  'images': [{'height': 640,
    'url': 'https://i.scdn.co/image/2f460a72975d1f0701c56482aad3f820ac5635c6',
    'width': 640},
   {'height': 300,
    'url': 'https://i.scdn.co/image/6cabe0ef1ac8fe662b366871f908e4714ad9406a',
    'width': 300},
   {'height': 64,
    'url': 'https://i.scdn.co/image/984c525d4772cc4ca88bf4bfed215be361c6bda3',
    'width': 64}],
  'name': 'Lil Chuuuch',
  'popularity': 1,
  'type': 'artist',
  'uri': 'spotify:artist:4d5e17qy0VPTjsdfICnkO4'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/16yXrzdQsBFRalQjN7Wk6t'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/16yXrzdQsBFRalQjN7Wk6t',
  'id': '16yXrzdQsBFRalQjN7Wk6t',
  'images': [],
  'name': 'Lil Chewy',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:16yXrzdQsBFRalQjN7Wk6t'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/20n9on68w2cI76FuX4tTEs'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/20n9on68w2cI76FuX4tTEs',
  'id': '20n9on68w2cI76FuX4tTEs',
  'images': [{'height': 640,
    'url': 'https://i.scdn.co/image/8166b56bbdfad479a749ac5e1f55b1867d5abdd8',
    'width': 640},
   {'height': 300,
    'url': 'https://i.scdn.co/image/e0a47aafa90cea0cd44c7f0f6547f459717402b6',
    'width': 300},
   {'height': 64,
    'url': 'https://i.scdn.co/image/86a83c7e7cf85272676fc7753d9bdb781d450de8',
    'width': 64}],
  'name': 'Lil Kees',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:20n9on68w2cI76FuX4tTEs'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/3PhvxkEuvXaIQh9ngiP4te'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/3PhvxkEuvXaIQh9ngiP4te',
  'id': '3PhvxkEuvXaIQh9ngiP4te',
  'images': [{'height': 640,
    'url': 'https://i.scdn.co/image/5de36a16a2f9e3fbc1d1657829b029e7a2ef79b5',
    'width': 640},
   {'height': 300,
    'url': 'https://i.scdn.co/image/62ef41a6095098b6f6ee0ea570f543fb79acda69',
    'width': 300},
   {'height': 64,
    'url': 'https://i.scdn.co/image/167c43c01ce4b012eaa5f9d0672e4b21af3a0c48',
    'width': 64}],
  'name': 'Lil Chaz',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:3PhvxkEuvXaIQh9ngiP4te'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/5wL5QDxL7ZFJwHdf2RFI80'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/5wL5QDxL7ZFJwHdf2RFI80',
  'id': '5wL5QDxL7ZFJwHdf2RFI80',
  'images': [],
  'name': 'Lil Ruc',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:5wL5QDxL7ZFJwHdf2RFI80'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/3DIXQ22R7o1Sedd9XKubVL'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/3DIXQ22R7o1Sedd9XKubVL',
  'id': '3DIXQ22R7o1Sedd9XKubVL',
  'images': [],
  'name': 'Lil Base',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:3DIXQ22R7o1Sedd9XKubVL'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/7wS3Af6UGKK8kEjyBnVXkz'},
  'followers': {'href': None, 'total': 12},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/7wS3Af6UGKK8kEjyBnVXkz',
  'id': '7wS3Af6UGKK8kEjyBnVXkz',
  'images': [{'height': 640,
    'url': 'https://i.scdn.co/image/77e3528535fc4f88a7a697b5f06f751893a2293b',
    'width': 640},
   {'height': 300,
    'url': 'https://i.scdn.co/image/c494b8537bc5511cfc98406d53a8ddd3785eabe7',
    'width': 300},
   {'height': 64,
    'url': 'https://i.scdn.co/image/6f15aef98c029ea0d3ae4d4328e6fadf01525bb1',
    'width': 64}],
  'name': 'Lil David',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:7wS3Af6UGKK8kEjyBnVXkz'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/7mPJnJU8LZBTRbqCCUabve'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/7mPJnJU8LZBTRbqCCUabve',
  'id': '7mPJnJU8LZBTRbqCCUabve',
  'images': [],
  'name': 'Lil Nitty',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:7mPJnJU8LZBTRbqCCUabve'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/4uvqX12agXsiJZ3svWo4LW'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/4uvqX12agXsiJZ3svWo4LW',
  'id': '4uvqX12agXsiJZ3svWo4LW',
  'images': [],
  'name': 'Lil Kmac',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:4uvqX12agXsiJZ3svWo4LW'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/22VXcrpKKr500AOElz6AFL'},
  'followers': {'href': None, 'total': 5},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/22VXcrpKKr500AOElz6AFL',
  'id': '22VXcrpKKr500AOElz6AFL',
  'images': [],
  'name': 'Lil Woody',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:22VXcrpKKr500AOElz6AFL'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/06um7mcE6UqiCWUCHtSzKr'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/06um7mcE6UqiCWUCHtSzKr',
  'id': '06um7mcE6UqiCWUCHtSzKr',
  'images': [{'height': 640,
    'url': 'https://i.scdn.co/image/130169ce8313dd2f8cf3aa960f1ba4fa7c35ee43',
    'width': 640},
   {'height': 300,
    'url': 'https://i.scdn.co/image/b9c280adfd0c5da873a1476dfaee6f824222684d',
    'width': 300},
   {'height': 64,
    'url': 'https://i.scdn.co/image/ea55a8c93d9b632617ab4c4df8726f88aabb7173',
    'width': 64}],
  'name': 'Lil Tyreke',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:06um7mcE6UqiCWUCHtSzKr'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/5bWYTNdlv0sc3xKJiukdN9'},
  'followers': {'href': None, 'total': 5},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/5bWYTNdlv0sc3xKJiukdN9',
  'id': '5bWYTNdlv0sc3xKJiukdN9',
  'images': [],
  'name': 'Lil Muggsy',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:5bWYTNdlv0sc3xKJiukdN9'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/0bIJ87vdm7bwlJ80F2On7B'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/0bIJ87vdm7bwlJ80F2On7B',
  'id': '0bIJ87vdm7bwlJ80F2On7B',
  'images': [],
  'name': 'Lil Mbone',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:0bIJ87vdm7bwlJ80F2On7B'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/5cMdEsNuGJNpKIPkwZZtcx'},
  'followers': {'href': None, 'total': 30},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/5cMdEsNuGJNpKIPkwZZtcx',
  'id': '5cMdEsNuGJNpKIPkwZZtcx',
  'images': [{'height': 640,
    'url': 'https://i.scdn.co/image/2992df10421065215585fccfd700fea7d5df5e58',
    'width': 640},
   {'height': 300,
    'url': 'https://i.scdn.co/image/99d78f0a204529ba1fba86ae17bdd84338a875d1',
    'width': 300},
   {'height': 64,
    'url': 'https://i.scdn.co/image/46421c8fe53cc83bdcc881a80cbd50930c823578',
    'width': 64}],
  'name': 'Lil Nardy',
  'popularity': 3,
  'type': 'artist',
  'uri': 'spotify:artist:5cMdEsNuGJNpKIPkwZZtcx'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/135iQfqImdgPiaOZyGuOu0'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/135iQfqImdgPiaOZyGuOu0',
  'id': '135iQfqImdgPiaOZyGuOu0',
  'images': [{'height': 640,
    'url': 'https://i.scdn.co/image/815c7b8f4c2cccb415080e97420e5968eb73efe4',
    'width': 640},
   {'height': 300,
    'url': 'https://i.scdn.co/image/42b8846a5973c2e4d21df76107137befe6c24b9f',
    'width': 300},
   {'height': 64,
    'url': 'https://i.scdn.co/image/06a789c74e54202cf5686e2fd36f114b966e0277',
    'width': 64}],
  'name': 'Lil Tahj',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:135iQfqImdgPiaOZyGuOu0'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/7zSzTMKvzdyqGvG2t8TjNH'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/7zSzTMKvzdyqGvG2t8TjNH',
  'id': '7zSzTMKvzdyqGvG2t8TjNH',
  'images': [],
  'name': 'Lil Chan',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:7zSzTMKvzdyqGvG2t8TjNH'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/6FXueg4skBqUzEti7oayBm'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/6FXueg4skBqUzEti7oayBm',
  'id': '6FXueg4skBqUzEti7oayBm',
  'images': [],
  'name': 'Lil Cone',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:6FXueg4skBqUzEti7oayBm'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/1336jOf1wgqwX7OhtRIQ2e'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/1336jOf1wgqwX7OhtRIQ2e',
  'id': '1336jOf1wgqwX7OhtRIQ2e',
  'images': [],
  'name': 'Lil Dramma',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:1336jOf1wgqwX7OhtRIQ2e'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/1CAB2vZ8oghZz1a49fW633'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/1CAB2vZ8oghZz1a49fW633',
  'id': '1CAB2vZ8oghZz1a49fW633',
  'images': [{'height': 640,
    'url': 'https://i.scdn.co/image/6882d628412157c2ce70cfac22b7a4105ef590bd',
    'width': 640},
   {'height': 300,
    'url': 'https://i.scdn.co/image/b57cd677ff5fe5b66856031f943a3bbe0e5843b5',
    'width': 300},
   {'height': 64,
    'url': 'https://i.scdn.co/image/8a2de299660c7bfedd465743591a194ea0b692c7',
    'width': 64}],
  'name': 'Lil Scott',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:1CAB2vZ8oghZz1a49fW633'},
 {'external_urls': {'spotify': 'https://open.spotify.com/artist/4qMzbuLu4eg8nLffEd7jpt'},
  'followers': {'href': None, 'total': 0},
  'genres': [],
  'href': 'https://api.spotify.com/v1/artists/4qMzbuLu4eg8nLffEd7jpt',
  'id': '4qMzbuLu4eg8nLffEd7jpt',
  'images': [],
  'name': 'Lil Ree',
  'popularity': 0,
  'type': 'artist',
  'uri': 'spotify:artist:4qMzbuLu4eg8nLffEd7jpt'}]
print_debug("About to loop through the artists")
for artist in unpopular_artists:
    print_debug("Looking at an artist")
    print_debug(artist['name'])
    display_artist_image(artist)
    print_debug("Successfully displayed an image")
print_debug("Finished looping through the artists")

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg