def wait_for_notebook_to_execute(self):
        """Wait for notebook to finish executing before continuing."""
        while True:
            r = requests.get('http://{0}/api/notebook/job/{1}'.format(
                             self.zeppelin_url, self.notebook_id))

            if r.status_code == 200:
                try:
                    data = r.json()['body']
                    if all(paragraph['status'] in ['FINISHED', 'ERROR'] for paragraph in data):
                        break
                    time.sleep(5)
                    continue
                except KeyError as e:
                    print(e)
                    print(r.json())

            elif r.status_code == 500:
                print('Notebook is still busy executing. Checking again in 60 seconds...')
                time.sleep(60)
                continue

            else:
                print('ERROR: Unexpected return code: {}'.format(r.status_code))
                sys.exit(1)
Wait for notebook to finish executing before continuing.