@pytest.fixture(scope='session')
def jupyter_notebook(request, log_file):
 from jupyter_core import paths
 config_dir = paths.jupyter_config_dir()
 body = '\nrequire(["base/js/namespace", "base/js/events"], function (IPython, events) {\n    events.on("kernel_ready.Kernel", function () {\n        IPython.notebook.execute_all_cells();\n    });\n});\n'
 custom = join(config_dir, 'custom')
 if (not exists(custom)):
  os.makedirs(custom)
 customjs = join(custom, 'custom.js')
 with open(customjs, 'w') as f:
  f.write(body)
 def clean_up_customjs():
  with open(customjs, 'w') as f:
   f.write('')
 request.addfinalizer(clean_up_customjs)
 notebook_port = pytest.config.option.notebook_port
 env = os.environ.copy()
 env['BOKEH_RESOURCES'] = 'server'
 notebook_dir = join(dirname(__file__), pardir, pardir)
 cmd = ['jupyter', 'notebook']
 argv = ['--no-browser', ('--port=%s' % notebook_port), ('--notebook-dir=%s' % notebook_dir)]
 jupter_notebook_url = ('http://localhost:%d' % notebook_port)
 try:
  proc = subprocess.Popen((cmd + argv), env=env, stdout=log_file, stderr=log_file)
 except OSError:
  write(('Failed to run: %s' % ' '.join((cmd + argv))))
  sys.exit(1)
 else:
  def stop_jupyter_notebook():
   write('Shutting down jupyter-notebook ...')
   proc.kill()
  request.addfinalizer(stop_jupyter_notebook)
  def wait_until(func, timeout=5.0, interval=0.01):
   start = time.time()
   while True:
    if func():
     return True
    if ((time.time() - start) > timeout):
     return False
    time.sleep(interval)
  def wait_for_jupyter_notebook():
   def helper():
    if (proc.returncode is not None):
     return True
    try:
     return requests.get(jupter_notebook_url)
    except ConnectionError:
     return False
   return wait_until(helper)
  if (not wait_for_jupyter_notebook()):
   write(('Timeout when running: %s' % ' '.join((cmd + argv))))
   sys.exit(1)
  if (proc.returncode is not None):
   write(('Jupyter notebook exited with code ' + str(proc.returncode)))
   sys.exit(1)
  return jupter_notebook_url

@ pytest . fixture ( scope = 'session' ) def jupyter_notebook ( request , log_file ) : from jupyter_core import paths config_dir = paths . jupyter_config_dir ( ) body = '\nrequire(["base/js/namespace", "base/js/events"], function (IPython, events) {\n    events.on("kernel_ready.Kernel", function () {\n        IPython.notebook.execute_all_cells();\n    });\n});\n' custom = join ( config_dir , 'custom' ) if ( not exists ( custom ) ) : os . makedirs ( custom ) customjs = join ( custom , 'custom.js' ) with open ( customjs , 'w' ) as f : f . write ( body ) def clean_up_customjs ( ) : with open ( customjs , 'w' ) as f : f . write ( '' ) request . addfinalizer ( clean_up_customjs ) notebook_port = pytest . config . option . notebook_port env = os . environ . copy ( ) env [ 'BOKEH_RESOURCES' ] = 'server' notebook_dir = join ( dirname ( __file__ ) , pardir , pardir ) cmd = [ 'jupyter' , 'notebook' ] argv = [ '--no-browser' , ( '--port=%s' % notebook_port ) , ( '--notebook-dir=%s' % notebook_dir ) ] jupter_notebook_url = ( 'http://localhost:%d' % notebook_port ) try : proc = subprocess . Popen ( ( cmd + argv ) , env = env , stdout = log_file , stderr = log_file ) except OSError : write ( ( 'Failed to run: %s' % ' ' . join ( ( cmd + argv ) ) ) ) sys . exit ( 1 ) else : def stop_jupyter_notebook ( ) : write ( 'Shutting down jupyter-notebook ...' ) proc . kill ( ) request . addfinalizer ( stop_jupyter_notebook ) def wait_until ( func , timeout = 5.0 , interval = 0.01 ) : start = time . time ( ) while True : if func ( ) : return True if ( ( time . time ( ) - start ) > timeout ) : return False time . sleep ( interval ) def wait_for_jupyter_notebook ( ) : def helper ( ) : if ( proc . returncode is not None ) : return True try : return requests . get ( jupter_notebook_url ) except ConnectionError : return False return wait_until ( helper ) if ( not wait_for_jupyter_notebook ( ) ) : write ( ( 'Timeout when running: %s' % ' ' . join ( ( cmd + argv ) ) ) ) sys . exit ( 1 ) if ( proc . returncode is not None ) : write ( ( 'Jupyter notebook exited with code ' + str ( proc . returncode ) ) ) sys . exit ( 1 ) return jupter_notebook_url
generate a jupyter notebook file cell-by-cell parameters script_blocks: list script execution cells .