def install(user=False, symlink=False, quiet=False, enable=False): """Install the widget nbextension and optionally enable it. Parameters ---------- user: bool Install for current user instead of system-wide. symlink: bool Symlink instead of copy (for development). enable: bool Enable the extension after installing it. quiet: bool Suppress print statements about progress. """ if not quiet: print("Installing nbextension ...") staticdir = pjoin(dirname(abspath(__file__)), 'static') install_nbextension(staticdir, destination='widgets', user=user, symlink=symlink) if enable: if not quiet: print("Enabling the extension ...") cm = ConfigManager() cm.update('notebook', { "load_extensions": { "widgets/notebook/js/extension": True, } }) if not quiet: print("Done.")
def test_create_nbextensions_system(self): with TemporaryDirectory() as td: nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR = self.system_nbext = pjoin( td, u'nbextensions') install_nbextension(self.src, user=False) self.assert_installed(pjoin(basename(self.src), u'ƒile'), user=False)
def test_create_nbextensions_system(self): with TemporaryDirectory() as td: self.system_nbext = pjoin(td, u'nbextensions') with patch.object(nbextensions, 'SYSTEM_JUPYTER_PATH', [td]): install_nbextension(self.src, user=False) self.assert_installed(pjoin(basename(self.src), u'ƒile'), user=False)
def install(user=False, symlink=False, quiet=False, enable=False): """Install the widget nbextension and optionally enable it. Parameters ---------- user: bool Install for current user instead of system-wide. symlink: bool Symlink instead of copy (for development). enable: bool Enable the extension after installing it. quiet: bool Suppress print statements about progress. """ if not quiet: print("Installing nbextension ...") staticdir = pjoin(dirname(abspath(__file__)), 'static') install_nbextension(staticdir, destination='widgets', user=user, symlink=symlink) if enable: if not quiet: print("Enabling the extension ...") cm = ConfigManager() cm.update( 'notebook', {"load_extensions": { "widgets/notebook/js/extension": True, }}) if not quiet: print("Done.")
def test_create_nbextensions_user(self): with TemporaryDirectory() as td: self.ipdir = ipdir = pjoin(td, u'ipython') install_nbextension(self.src, user=True) self.assert_installed( pjoin(basename(self.src), u'ƒile'), user=True )
def test_create_nbextensions_system(self): with TemporaryDirectory() as td: nbextensions.SYSTEM_NBEXTENSIONS_INSTALL_DIR = self.system_nbext = pjoin(td, u'nbextensions') install_nbextension(self.src, user=False) self.assert_installed( pjoin(basename(self.src), u'ƒile'), user=False )
def test_quiet(self): stdout = StringIO() stderr = StringIO() with mock.patch.object(sys, 'stdout', stdout), \ mock.patch.object(sys, 'stderr', stderr): install_nbextension(self.src, verbose=0) self.assertEqual(stdout.getvalue(), '') self.assertEqual(stderr.getvalue(), '')
def test_install_zip(self): path = pjoin(self.src, "myjsext.zip") with zipfile.ZipFile(path, 'w') as f: f.writestr("a.js", b"b();") f.writestr("foo/a.js", b"foo();") install_nbextension(path) self.assert_installed("a.js") self.assert_installed(pjoin("foo", "a.js"))
def test_create_nbextensions_system(self): with TemporaryDirectory() as td: self.system_nbext = pjoin(td, u'nbextensions') with patch.object(nbextensions, 'SYSTEM_JUPYTER_PATH', [td]): install_nbextension(self.src, user=False) self.assert_installed( pjoin(basename(self.src), u'ƒile'), user=False )
def test_install_destination_bad(self): with TemporaryDirectory() as d: zf = u'ƒ.zip' zsrc = pjoin(d, zf) with zipfile.ZipFile(zsrc, 'w') as z: z.writestr("a.js", b"b();") with self.assertRaises(ValueError): install_nbextension(zsrc, destination='foo')
def test_check_nbextension(self): with TemporaryDirectory() as d: f = u'ƒ.js' src = pjoin(d, f) touch(src) install_nbextension(src, user=True) assert check_nbextension(f, user=True) assert check_nbextension([f], user=True) assert not check_nbextension([f, pjoin('dne', f)], user=True)
def test_install_symlink(self): with TemporaryDirectory() as d: f = u'ƒ.js' src = pjoin(d, f) touch(src) install_nbextension(src, symlink=True) dest = pjoin(self.system_nbext, f) assert os.path.islink(dest) link = os.readlink(dest) self.assertEqual(link, src)
def test_create_data_dir(self): """install_nbextension when data_dir doesn't exist""" with TemporaryDirectory() as td: self.data_dir = pjoin(td, u'jupyter_data') install_nbextension(self.src, user=True) self.assert_dir_exists(self.data_dir) for file in self.files: self.assert_installed( pjoin(basename(self.src), file), user=True, )
def test_create_data_dir(self): """install_nbextension when data_dir doesn't exist""" with TemporaryDirectory() as td: self.data_dir = pjoin(td, u'jupyter_data') install_nbextension(self.src, user=True) self.assert_dir_exists(self.data_dir) for file in self.files: self.assert_installed( pjoin(basename(self.src), file), user=True, )
def test_create_ipython_dir(self): """install_nbextension when ipython_dir doesn't exist""" with TemporaryDirectory() as td: self.ipdir = ipdir = pjoin(td, u'ipython') install_nbextension(self.src, user=True) self.assert_dir_exists(ipdir) for file in self.files: self.assert_installed( pjoin(basename(self.src), file), user=bool(ipdir) )
def test_install_symlink_bad(self): with self.assertRaises(ValueError): install_nbextension("http://example.com/foo.js", symlink=True) with TemporaryDirectory() as d: zf = u'ƒ.zip' zsrc = pjoin(d, zf) with zipfile.ZipFile(zsrc, 'w') as z: z.writestr("a.js", b"b();") with self.assertRaises(ValueError): install_nbextension(zsrc, symlink=True)
def install_nbextension(**kwargs): """Install the appropriate html and javascript into the IPython nbextension. Keyword arguments will be passed on to the IPython install_nbextension function. """ import os.path try: from jupyter_notebook import nbextensions except ImportError: # IPython 3.x from IPython.html import nbextensions pkgdir = os.path.dirname(__file__) kwargs['destination'] = 'pythreejs' nbextensions.install_nbextension(os.path.join(pkgdir, 'nbextension'), **kwargs)
def test_overwrite_dir(self): with TemporaryDirectory() as src: base = basename(src) fname = u'ƒ.js' touch(pjoin(src, fname)) install_nbextension(src) self.assert_installed(pjoin(base, fname)) os.remove(pjoin(src, fname)) fname2 = u'∂.js' touch(pjoin(src, fname2)) install_nbextension(src, overwrite=True) self.assert_installed(pjoin(base, fname2)) self.assert_not_installed(pjoin(base, fname))
def install_nbextension(**kwargs): """Install the appropriate html and javascript into the IPython nbextension. Keyword arguments will be passed on to the IPython install_nbextension function. """ import os.path try: from jupyter_notebook import nbextensions except ImportError: # IPython 3.x from IPython.html import nbextensions pkgdir = os.path.dirname(__file__) kwargs['destination'] = 'pythreejs' nbextensions.install_nbextension(os.path.join(pkgdir, 'nbextension'), **kwargs)
def test_overwrite_broken_symlink(self): with TemporaryDirectory() as d: f = u'ƒ.js' f2 = u'ƒ2.js' src = pjoin(d, f) src2 = pjoin(d, f2) touch(src) install_nbextension(src, symlink=True) os.rename(src, src2) install_nbextension(src2, symlink=True, overwrite=True, destination=f) dest = pjoin(self.system_nbext, f) assert os.path.islink(dest) link = os.readlink(dest) self.assertEqual(link, src2)
def test_skip_old_file(self): with TemporaryDirectory() as d: fname = u'ƒ.js' src = pjoin(d, fname) mtime = touch(src) install_nbextension(src) self.assert_installed(fname) dest = pjoin(self.system_nbext, fname) old_mtime = os.stat(dest).st_mtime mtime = touch(src, mtime - 100) install_nbextension(src) new_mtime = os.stat(dest).st_mtime self.assertEqual(new_mtime, old_mtime)
def test_install_tar(self): def _add_file(f, fname, buf): info = tarfile.TarInfo(fname) info.size = len(buf) f.addfile(info, BytesIO(buf)) for i,ext in enumerate((".tar.gz", ".tgz", ".tar.bz2")): path = pjoin(self.src, "myjsext" + ext) with tarfile.open(path, 'w') as f: _add_file(f, "b%i.js" % i, b"b();") _add_file(f, "foo/b%i.js" % i, b"foo();") install_nbextension(path) self.assert_installed("b%i.js" % i) self.assert_installed(pjoin("foo", "b%i.js" % i))
def test_overwrite_file(self): with TemporaryDirectory() as d: fname = u'ƒ.js' src = pjoin(d, fname) with open(src, 'w') as f: f.write('first') mtime = touch(src) dest = pjoin(self.system_nbext, fname) install_nbextension(src) with open(src, 'w') as f: f.write('overwrite') mtime = touch(src, mtime - 100) install_nbextension(src, overwrite=True) with open(dest) as f: self.assertEqual(f.read(), 'overwrite')
def test_update_file(self): with TemporaryDirectory() as d: fname = u'ƒ.js' src = pjoin(d, fname) with open(src, 'w') as f: f.write('first') mtime = touch(src) install_nbextension(src) self.assert_installed(fname) dest = pjoin(self.system_nbext, fname) old_mtime = os.stat(dest).st_mtime with open(src, 'w') as f: f.write('overwrite') touch(src, mtime + 10) install_nbextension(src) with open(dest) as f: self.assertEqual(f.read(), 'overwrite')
def install(profile='default', symlink=True, mixed=False, user=False, prefix=None, verbose=False, path=None): dname = os.path.dirname(__file__) # might want to check if already installed and overwrite if exist if symlink and verbose: log.info('Will try symlink nbextension') if mixed and verbose: log.info('Will install mixed content manager') if prefix and verbose: log.info("I'll install in prefix:", prefix) nbe.install_nbextension(os.path.join(dname,'gdrive'), symlink=symlink, user=user, prefix=prefix, nbextensions_dir=path) activate(profile, mixed=mixed)
def install(profile='default', symlink=True, mixed=False, user=False, prefix=None, verbose=False, path=None): dname = os.path.dirname(__file__) # might want to check if already installed and overwrite if exist if symlink and verbose: log.info('Will try symlink nbextension') if mixed and verbose: log.info('Will install mixed content manager') if prefix and verbose: log.info("I'll install in prefix:", prefix) nbe.install_nbextension(os.path.join(dname, 'gdrive'), symlink=symlink, user=user, prefix=prefix, nbextensions_dir=path) activate(profile, mixed=mixed)
def test_install_url(self): def fake_urlretrieve(url, dest): touch(dest) save_urlretrieve = nbextensions.urlretrieve nbextensions.urlretrieve = fake_urlretrieve try: install_nbextension("http://example.com/path/to/foo.js") self.assert_installed("foo.js") install_nbextension("https://example.com/path/to/another/bar.js") self.assert_installed("bar.js") install_nbextension("https://example.com/path/to/another/bar.js", destination = 'foobar.js') self.assert_installed("foobar.js") finally: nbextensions.urlretrieve = save_urlretrieve
def test_single_file(self): file = self.files[0] install_nbextension(pjoin(self.src, file)) self.assert_installed(file)
def test_destination_file(self): file = self.files[0] install_nbextension(pjoin(self.src, file), destination = u'ƒiledest') self.assert_installed(u'ƒiledest')
def test_destination_dir(self): d = u'∂ir' install_nbextension(pjoin(self.src, d), destination = u'ƒiledest2') self.assert_installed(pjoin(u'ƒiledest2', u'∂ir2', u'ƒile2'))
def test_install_nbextension(self): with self.assertRaises(TypeError): install_nbextension(glob.glob(pjoin(self.src, '*')))
def test_single_dir(self): d = u'∂ir' install_nbextension(pjoin(self.src, d)) self.assert_installed(self.files[-1])
#!/usr/bin/env python # Thanks @takluyver for your cite2c install.py. from os.path import dirname, abspath, join as pjoin from jupyter_notebook.nbextensions import install_nbextension from jupyter_notebook.services.config import ConfigManager import argparse parser = argparse.ArgumentParser(description="Installs the IPython widgets") parser.add_argument("-u", "--user", help="Install as current user", action="store_true") parser.add_argument("-s", "--symlink", help="Symlink instead of copying files", action="store_true") args = parser.parse_args() print("Installing nbextension ...") staticdir = pjoin(dirname(abspath(__file__)), 'static') install_nbextension(staticdir, destination='widgets', user=args.user, symlink=args.symlink) print("Enabling the extension ...") cm = ConfigManager() cm.update('notebook', {"load_extensions": {"widgets/notebook/js/extension": True}}) print("Done.")