In [29]:
import itertools
import requests

In [30]:
resp = requests.get('https://gifs.mylesb.ca/api.json')

if not resp.ok:
    print('💩 it failed.')
else:
    print('🎉 it worked!')


🎉 it worked!

In [31]:
gifs = resp.json()

print(len(gifs))


191

In [32]:
def search(query):
    query = query.lower()
    
    for gif in gifs:
        if any(query in str(value).lower() for value in gif.values()):
            yield gif

for gif in itertools.islice(search('coffee'), 1):
    print(gif)


{'date': '2017-12-07T19:16:39', 'height': 311, 'html_url': 'https://gifs.mylesb.ca/coffee/coffee-time/', 'image_url': 'https://gifs.mylesb.ca/coffee/coffee-time/image.gif', 'json_url': 'https://gifs.mylesb.ca/coffee/coffee-time/api.json', 'slug': 'coffee/coffee-time', 'width': 268}

In [35]:
from IPython.display import display
from ipywidgets import widgets

result_template = '<a href="{html_url}"><img src="{image_url}" /></a>'
search_field = widgets.Text(description='Search')
display(search_field)

def handle_submit(sender):
    gifs = search(search_field.value)
    
    for gif in gifs:
        display(widgets.HTML(value=result_template.format(**gif)))

search_field.on_submit(handle_submit)



In [ ]: