A random item from the Museums Victoria's collections!

The Museums Victoria collections API includes a couple of very useful parameters:

  • sort – as well as the usual options 'relevance' and 'date', sort can be set to 'random'
  • hasimages – set to 'yes' to only get records with images attached

Putting these two things together, we can very easily create a random collection image viewer. Just run the cells below, then click on the 'Randomise!' button for serendipitous fun.

In [1]:
import requests
import random
from IPython.display import Image, display, HTML
import ipywidgets as widgets
In [2]:
SEARCH_URL = 'https://collections.museumsvictoria.com.au/api/search'
In [3]:
def display_random(b):
    out.clear_output()
    params = {
        'sort': 'random',
        'hasimages': 'yes'
    }
    # Get the data
    response = requests.get(SEARCH_URL, params=params, headers={'User-Agent': 'Mozilla/5.0'})
    # Randomly select a record from the randomly sorted data
    record = random.choice(response.json())
    # Display the record
    with out:
        display(HTML(f'<h3>{record["displayTitle"]}</h3>'))
        display(Image(url=record['media'][0]['small']['uri']))
        display(HTML(f'<a href="https://collections.museumsvictoria.com.au/{record["id"]}">More info...</a>'))

# Create a button to launch the randomness
go = widgets.Button(description='Randomise!')
out = widgets.Output()
go.on_click(display_random)
display(go)
display(out)

Created by Tim Sherratt for the GLAM Workbench. Support me by becoming a GitHub sponsor!