Skip to content

Commit

Permalink
wip hub integration
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Jun 2, 2019
1 parent 36f44d9 commit 8f58422
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 15 deletions.
8 changes: 8 additions & 0 deletions jupyterlab/extension.py
Original file line number Diff line number Diff line change
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'):
settings['page_config_data']['hub_prefix'] = nbapp.hub_prefix
settings['page_config_data']['hub_host'] = nbapp.hub_host
settings['page_config_data']['hub_user'] = nbapp.user
api_token = os.getenv('JUPYTERHUB_API_TOKEN', '')
settings['page_config_data']['token'] = api_token

# Add the root handlers if we have not errored.
if not errored:
add_handlers(web_app, config)
18 changes: 3 additions & 15 deletions jupyterlab/labhubapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,10 @@ def _default_url(self):
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
7 changes: 7 additions & 0 deletions packages/hub-extension/README.md
Original file line number Diff line number Diff line change
@@ -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.
38 changes: 38 additions & 0 deletions packages/hub-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"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"
},
"devDependencies": {
"rimraf": "~2.6.2",
"typescript": "~3.5.1"
},
"publishConfig": {
"access": "public"
}
}
89 changes: 89 additions & 0 deletions packages/hub-extension/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/

import { Menu } from '@phosphor/widgets';

import { ICommandPalette } from '@jupyterlab/apputils';

import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';

import { PageConfig, URLExt } from '@jupyterlab/coreutils';

import { IMainMenu } from '@jupyterlab/mainmenu';

/**
* The command IDs used by the plugin.
*/
export namespace CommandIDs {
export const controlPanel: string = 'hub:control-panel';

export const logout: string = 'hub:logout';
}

/**
* Activate the jupyterhub extension.
*/
function activateHubExtension(
app: JupyterFrontEnd,
palette: ICommandPalette,
mainMenu: IMainMenu
): void {
const hubHost = PageConfig.getOption('hub_host');
const hubPrefix = PageConfig.getOption('hub_prefix');
const baseUrl = PageConfig.getBaseUrl();

// Bail if not running on JupyterHub.
if (!hubPrefix) {
return;
}

console.log('hub-extension: Found configuration ', {
hubHost: hubHost,
hubPrefix: hubPrefix
});

const category = 'Hub';
const { commands } = app;

commands.addCommand(CommandIDs.controlPanel, {
label: 'Control Panel',
caption: 'Open the Hub control panel in a new browser tab',
execute: () => {
window.open(hubHost + URLExt.join(hubPrefix, 'home'), '_blank');
}
});

commands.addCommand(CommandIDs.logout, {
label: 'Logout',
caption: 'Log out of the Hub',
execute: () => {
window.location.href = hubHost + URLExt.join(baseUrl, 'logout');
}
});

// Add commands and menu itmes.
let menu = new Menu({ commands });
menu.title.label = category;
[CommandIDs.controlPanel, CommandIDs.logout].forEach(command => {
palette.addItem({ command, category });
menu.addItem({ command });
});
mainMenu.addMenu(menu, { rank: 100 });
}

/**
* Initialization data for the hub-extension.
*/
const hubExtension: JupyterFrontEndPlugin<void> = {
activate: activateHubExtension,
id: 'jupyter.extensions.hub-extension',
requires: [ICommandPalette, IMainMenu],
autoStart: true
};

export default hubExtension;
8 changes: 8 additions & 0 deletions packages/hub-extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfigbase",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"include": ["src/*"]
}
1 change: 1 addition & 0 deletions packages/metapackage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@jupyterlab/help-extension": "^1.0.0-alpha.8",
"@jupyterlab/htmlviewer": "^1.0.0-alpha.9",
"@jupyterlab/htmlviewer-extension": "^1.0.0-alpha.9",
"@jupyterlab/hub-extension": "^1.0.0-alpha.8",
"@jupyterlab/imageviewer": "^1.0.0-alpha.8",
"@jupyterlab/imageviewer-extension": "^1.0.0-alpha.8",
"@jupyterlab/inspector": "^1.0.0-alpha.8",
Expand Down
3 changes: 3 additions & 0 deletions packages/metapackage/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
{
"path": "../htmlviewer-extension"
},
{
"path": "../hub-extension"
},
{
"path": "../imageviewer"
},
Expand Down

0 comments on commit 8f58422

Please sign in to comment.