Skip to content

Commit

Permalink
Merge pull request #6451 from blink1073/jhub-integration
Browse files Browse the repository at this point in the history
JupyterHub integration
  • Loading branch information
blink1073 committed Jun 9, 2019
2 parents 8d36127 + a2f9d2d commit 3d710e0
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 64 deletions.
3 changes: 3 additions & 0 deletions dev_mode/package.json
Expand Up @@ -34,6 +34,7 @@
"@jupyterlab/fileeditor-extension": "^1.0.0-alpha.9",
"@jupyterlab/help-extension": "^1.0.0-alpha.9",
"@jupyterlab/htmlviewer-extension": "^1.0.0-alpha.10",
"@jupyterlab/hub-extension": "^1.0.0-alpha.8",
"@jupyterlab/imageviewer": "^1.0.0-alpha.9",
"@jupyterlab/imageviewer-extension": "^1.0.0-alpha.9",
"@jupyterlab/inspector-extension": "^1.0.0-alpha.9",
Expand Down Expand Up @@ -130,6 +131,7 @@
"@jupyterlab/fileeditor-extension": "",
"@jupyterlab/help-extension": "",
"@jupyterlab/htmlviewer-extension": "",
"@jupyterlab/hub-extension": "",
"@jupyterlab/imageviewer-extension": "",
"@jupyterlab/inspector-extension": "",
"@jupyterlab/launcher-extension": "",
Expand Down Expand Up @@ -225,6 +227,7 @@
"@jupyterlab/fileeditor-extension": "../packages/fileeditor-extension",
"@jupyterlab/help-extension": "../packages/help-extension",
"@jupyterlab/htmlviewer-extension": "../packages/htmlviewer-extension",
"@jupyterlab/hub-extension": "../packages/hub-extension",
"@jupyterlab/imageviewer-extension": "../packages/imageviewer-extension",
"@jupyterlab/inspector-extension": "../packages/inspector-extension",
"@jupyterlab/javascript-extension": "../packages/javascript-extension",
Expand Down
8 changes: 8 additions & 0 deletions jupyterlab/extension.py
Expand Up @@ -204,6 +204,14 @@ def load_jupyter_server_extension(nbapp):
# Must add before the root server handlers to avoid shadowing.
web_app.add_handlers('.*$', handlers)

# If running under JupyterHub, add more metadata.
if hasattr(nbapp, 'hub_prefix'):
page_config['hubPrefix'] = nbapp.hub_prefix
page_config['hubHost'] = nbapp.hub_host
page_config['hubUser'] = nbapp.user
api_token = os.getenv('JUPYTERHUB_API_TOKEN', '')
page_config['token'] = api_token

# Add the root handlers if we have not errored.
if not errored:
add_handlers(web_app, config)
34 changes: 18 additions & 16 deletions jupyterlab/labhubapp.py
@@ -1,4 +1,5 @@
import os
import warnings

from traitlets import default

Expand All @@ -11,29 +12,30 @@
raise ImportError('You must have jupyterhub installed for this to work.')
else:
class SingleUserLabApp(SingleUserNotebookApp, LabApp):

"""
A sublcass of JupyterHub's SingleUserNotebookApp which includes LabApp
as a mixin. This makes the LabApp configurables available to the spawned
jupyter server.
If you don't need to change any of the configurables from their default
values, then this class is not necessary, and you can deploy JupyterLab
by ensuring that its server extension is enabled and setting the
`Spawner.default_url` to '/lab'.
If you do need to configure JupyterLab, then use this application by
setting `Spawner.cmd = ['jupyter-labhub']`.
"""
@default("default_url")
def _default_url(self):
"""when using jupyter-labhub, jupyterlab is default ui"""
return "/lab"

def init_webapp(self, *args, **kwargs):
warnings.warn(
"SingleUserLabApp is deprecated, use SingleUserNotebookApp and set " + \
"c.Spawner.default_url = '/lab' in jupyterhub_config.py", DeprecationWarning
)
super().init_webapp(*args, **kwargs)
settings = self.web_app.settings
if 'page_config_data' not in settings:
settings['page_config_data'] = {}
settings['page_config_data']['hub_prefix'] = self.hub_prefix
settings['page_config_data']['hub_host'] = self.hub_host
settings['page_config_data']['hub_user'] = self.user
api_token = os.getenv('JUPYTERHUB_API_TOKEN')
if not api_token:
api_token = ''
if not self.token:
try:
self.token = api_token
except AttributeError:
self.log.error("Can't set self.token")
settings['page_config_data']['token'] = api_token


def main(argv=None):
Expand Down
28 changes: 8 additions & 20 deletions packages/application-extension/src/index.tsx
Expand Up @@ -2,12 +2,12 @@
// Distributed under the terms of the Modified BSD License.

import {
ConnectionLost,
IConnectionLost,
ILabShell,
ILabStatus,
ILayoutRestorer,
IRouter,
ConnectionLost,
JupyterFrontEnd,
JupyterFrontEndPlugin,
JupyterLab,
Expand Down Expand Up @@ -75,13 +75,14 @@ namespace CommandIDs {
*/
const main: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab/application-extension:main',
requires: [ICommandPalette, IConnectionLost, IRouter, IWindowResolver],
requires: [ICommandPalette, IRouter, IWindowResolver],
optional: [IConnectionLost],
activate: (
app: JupyterFrontEnd,
palette: ICommandPalette,
connectionLost: IConnectionLost,
router: IRouter,
resolver: IWindowResolver
resolver: IWindowResolver,
connectionLost: IConnectionLost | undefined
) => {
if (!(app instanceof JupyterLab)) {
throw new Error(`${main.id} must be activated in JupyterLab.`);
Expand Down Expand Up @@ -112,7 +113,8 @@ const main: JupyterFrontEndPlugin<void> = {
});

// If the connection to the server is lost, handle it with the
// connection lost token.
// connection lost handler.
connectionLost = connectionLost || ConnectionLost;
app.serviceManager.connectionFailure.connect(connectionLost);

const builder = app.serviceManager.builder;
Expand Down Expand Up @@ -746,19 +748,6 @@ const paths: JupyterFrontEndPlugin<JupyterFrontEnd.IPaths> = {
provides: JupyterFrontEnd.IPaths
};

/**
* The default JupyterLab connection lost provider. This may be overridden
* to provide custom behavior when a connection to the server is lost.
*/
const connectionlost: JupyterFrontEndPlugin<IConnectionLost> = {
id: '@jupyterlab/apputils-extension:connectionlost',
activate: (app: JupyterFrontEnd): IConnectionLost => {
return ConnectionLost;
},
autoStart: true,
provides: IConnectionLost
};

/**
* Export the plugins as default.
*/
Expand All @@ -773,8 +762,7 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
shell,
status,
info,
paths,
connectionlost
paths
];

export default plugins;
2 changes: 2 additions & 0 deletions packages/application/src/frontend.ts
Expand Up @@ -272,6 +272,8 @@ export namespace JupyterFrontEnd {
readonly themes: string;
readonly tree: string;
readonly workspaces: string;
readonly hubPrefix?: string;
readonly hubHost?: string;
};

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/application/src/lab.ts
Expand Up @@ -244,7 +244,9 @@ export namespace JupyterLab {
settings: PageConfig.getOption('settingsUrl'),
themes: PageConfig.getOption('themesUrl'),
tree: PageConfig.getOption('treeUrl'),
workspaces: PageConfig.getOption('workspacesUrl')
workspaces: PageConfig.getOption('workspacesUrl'),
hubHost: PageConfig.getOption('hubHost') || undefined,
hubPrefix: PageConfig.getOption('hubPrefix') || undefined
},
directories: {
appSettings: PageConfig.getOption('appSettingsDir'),
Expand Down
7 changes: 7 additions & 0 deletions packages/hub-extension/README.md
@@ -0,0 +1,7 @@
# @jupyterlab/hub-extension

JupyterLab](https://github.com/jupyterlab/jupyterlab) integration for
[JupyterHub](https://github.com/jupyterhub/jupyterhub).

This adds a "Hub" menu to JupyterLab that allows a user to log out of JupyterHub
or access their JupyterHub control panel.
48 changes: 48 additions & 0 deletions packages/hub-extension/package.json
@@ -0,0 +1,48 @@
{
"name": "@jupyterlab/hub-extension",
"version": "1.0.0-alpha.8",
"description": "JupyterLab integration for JupyterHub",
"homepage": "https://github.com/jupyterlab/jupyterlab",
"bugs": {
"url": "https://github.com/jupyterlab/jupyterlab/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/jupyterlab/jupyterlab.git"
},
"license": "BSD-3-Clause",
"author": "Project Jupyter",
"files": [
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
"schema/*.json",
"style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib/"
},
"scripts": {
"build": "tsc",
"clean": "rimraf lib",
"prepublishOnly": "npm run build",
"watch": "tsc -w --listEmittedFiles"
},
"dependencies": {
"@jupyterlab/application": "^1.0.0-alpha.9",
"@jupyterlab/apputils": "^1.0.0-alpha.9",
"@jupyterlab/coreutils": "^3.0.0-alpha.9",
"@jupyterlab/mainmenu": "^1.0.0-alpha.9",
"@jupyterlab/services": "^4.0.0-alpha.9"
},
"devDependencies": {
"rimraf": "~2.6.2",
"typescript": "~3.5.1"
},
"publishConfig": {
"access": "public"
},
"jupyterlab": {
"extension": true
}
}

0 comments on commit 3d710e0

Please sign in to comment.