Package: clonenotebooks
This was built starting fromDaniel Allan's notebook clone extensionas a basis. It currently
requires branchstep7ofmy fork of NBViewerin order to run.Installation InstructionsThe easiest and quickest way to get a version of this up and running
to test out would be to use the Docker image and setup instructions included in theDockersubfolder of this repository.Otherwise install branchstep7ofmy fork of NBViewerusing the same installation
instructions as for the Jupyter master branch, in particular the setup
instructions for using NBViewer as a JupyterHub service. Then download this repository,
and in the folder run (the dot is important, it means "present working
directory"):pip install .if you don't want to make changes to the source code, otherwisepip install -e .This installs the corresponding Python package, but we still need to
tell Jupyter to use the appropriate sub-module as a notebook server
extension. To do this, run:jupyter serverextension enable clonenotebooks.cloners --sys-prefixMake sure you havenbviewerinstalled as a Python package, via Pip
or Conda or whatever else, on order for this to work.Then copy thetemplatesfolder to your preferred location, and add
to the command for NBViewer in yourjupyterhub_config.pyfile--template-path=/your/preferred/locationThen make sure to include annbviewer_config.pyfile (this is a major why
for now usingmy fork of NBVieweris
necessary) with the lines:c.NBViewer.handler_settings    = {'clone_notebooks' : True}

c.NBViewer.local_handler       = "clonenotebooks.renderers.LocalRenderingHandler"
c.NBViewer.url_handler         = "clonenotebooks.renderers.URLRenderingHandler"
c.NBViewer.github_blob_handler = "clonenotebooks.renderers.GitHubBlobRenderingHandler"
c.NBViewer.github_tree_handler = "clonenotebooks.renderers.GitHubTreeRenderingHandler"
c.NBViewer.gist_handler        = "clonenotebooks.renderers.GistRenderingHandler"
c.NBViewer.user_gists_handler  = "clonenotebooks.renderers.UserGistsRenderingHandler"By default notebooks are cloned into the root directory as determined by the value ofc.Spawner.notebook_dirinjupyterhub_config.py. For cloning notebooks into another directory, the linec.NBViewer.handler_settingsinnbviewer_config.pyneeds to be modified as follows:c.NBViewer.handler_settings    = {'clone_notebooks' : True, 'clone_to_directory' : <desired directory here>}One can include{username}as a stand-in for the JupyterHub user's name, analogous to the settings forc.Spawner.notebook_dirandc.Spawner.default_urlinjupyterhub_config.py. For example the following is valid:c.NBViewer.handler_settings    = {'clone_notebooks' : True, 'clone_to_directory' : '/users/{username[0]}/{username}'}will cause notebooks to be cloned into/jupyter/users/f/foofor userfooand/jupyter/users/b/barfor userbarif the value ofc.Spawner.notebook_diris'/jupyter', and will cause notebooks to be cloned into/users/f/foofor userfooand/users/b/barfor userbarif the value ofc.Spawner.notebook_diris'/'. In particular, the destination where notebooks is cloned willalwaysbe relative to the contents manager's root directory (which will usually equal the value ofc.Spawner.notebook_dir).An example copy ofnbviewer_config.pyis also included in this repository, in theDockersubfolder. Ideally this
should have everything configured, but admittedly these setup instructions are more
vague than they could be and might not have suggested an important step.I recommend comparing with the Dockerfiles in theDockersubfolder of this repositoryfor an example setup
if any difficulties arise. Please give any and all feedback about any ways in
which the documentation could be improved, since it will be much
appreciated.Here is a link to the issues pagefor requests for improved documentation and/or general feedback.Kernelspec CloningFor notebooks from almost any source (local, Gist, URL),clonenotebookschecks for a "local" kernelspec (kernel.json) file located in the same directory as the notebook being cloned, with the assumption that this kernelspec can be used at the clone destination to load the environment needed to run the environment. If it finds one, the kernelspec is installed in addition to the notebook being cloned. The name given to the kernelspec (i.e. the name of the corresponding directory in<environment_path>/share/jupyter/kernels) is by default the name of the enclosing directory. ("Kernel name" as used here should not be confused with thedisplay_nameattribute of thekernel.json, which is what is visible to the end-user and does not need to be unique.) (In the case of notebooks from URLs or Gist, "enclosing directory" refers to the "base name" of the URL "path" excluding the filename, e.g.testinhttps://example.com/test/notebook.ipynb.) If a kernelspec with the same name is already found, the previous one is overwritten. In particular, if you update the kernelspec (kernel.json) file in the directory and then clone another notebook from that directory, the updated kernelspec will replace the previous one.The behavior is somewhat different for notebooks sourced fromGitHub. First,clonenotebookschecks for a "global" kernelspec located at the repository's root directory. Second, it checks for a "local" kernelspec in the notebook's directory as in the cases above. If a "local" kernelspec is found, regardless of whether a "global" kernelspec is also found, the "local" kernelspec will be installed, and under the name<repo_name>-<branch_name>-<enclosing_directory>(so as to avoid name conflicts with any "global" kernelspecs). If a "global" kernelspec is found, but no "local" kernelspec is found, then the "global" kernelspec is installed under the name<repo_name>-<branch_name>. (For notebooks located in the repository root, anykernel.jsonlocated also in the repository root will be treated as a "global" kernelspec, and thus installed under<repo_name>-<branch_name>, even though in this case thekernel.jsonis technically also a "local" kernelspec.)"Global" kernelspecs could probably be implemented for other notebook sources if there is demand for the feature and if sensible defaults can be chosen for the locations to check for the "global" kernelspec.FeaturesThis extension can clone notebooks served from:URLGitHub tree viewGitHub individual file viewindividual GitHub GistsGist's from a user's pagelocal files from a directoryindividual local filesAttributionsExample notebooks included in multiple-container Dockerfile demo are from theJupyter gallery of interesting notebooks. Credit for them belongs to their respective authors (Filipa Rodrigues,Jason Chin,Shashi Gowda).