potential implementation for #12

This commit is contained in:
Tim Paine 2018-05-14 18:06:00 -04:00
parent 4d09c983f4
commit f14dfab46d
5 changed files with 50 additions and 21 deletions

View file

@ -19,4 +19,5 @@ install the server extension, and add the following to `jupyter_notebook_config.
```python3
c.JupyterLabIFrame.iframes = ['list', 'of', 'sites']
c.JupyterLabIFrame.welcome = 'a site'
```

View file

@ -4,11 +4,12 @@ from notebook.utils import url_path_join
class IFrameHandler(IPythonHandler):
def initialize(self, iframes=None):
self.iframes = iframes
def initialize(self, welcome=None, sites=None):
self.sites = sites
self.welcome = welcome
def get(self):
self.finish(ujson.dumps(self.iframes))
self.finish(ujson.dumps({'welcome': self.welcome or '', 'sites': self.sites}))
def load_jupyter_server_extension(nb_server_app):
@ -19,11 +20,13 @@ def load_jupyter_server_extension(nb_server_app):
nb_server_app (NotebookWebApplication): handle to the Notebook webserver instance.
"""
web_app = nb_server_app.web_app
iframes = nb_server_app.config.get('JupyterLabIFrame', {}).get('iframes', [])
sites = nb_server_app.config.get('JupyterLabIFrame', {}).get('iframes', [])
welcome = nb_server_app.config.get('JupyterLabIFrame', {}).get('welcome', [])
host_pattern = '.*$'
base_url = web_app.settings['base_url']
print('Installing jupyterlab_iframe handler on path %s' % '/iframes')
print('Handinling iframes: %s' % iframes)
print('Handling iframes: %s' % sites)
web_app.add_handlers(host_pattern, [(url_path_join(base_url, '/iframes'), IFrameHandler, {'iframes': iframes})])
web_app.add_handlers(host_pattern, [(url_path_join(base_url, '/iframes'), IFrameHandler, {'welcome': welcome, 'sites': sites})])

View file

@ -1,6 +1,6 @@
{
"name": "jupyterlab_iframe",
"version": "0.1.2",
"version": "0.1.3",
"description": "A JupyterLab extension.",
"author": "Tim Paine",
"main": "lib/index.js",

View file

@ -9,11 +9,11 @@ with open(path.join(here, 'README.md'), encoding='utf-8') as f:
setup(
name='jupyterlab_iframe',
version='0.0.2',
version='0.0.3',
description='IFrame widgets for JupyterLab',
long_description=long_description,
url='https://github.com/timkpaine/jupyterlab_iframe',
download_url='https://github.com/timkpaine/jupyterlab_iframe/archive/v0.0.2.tar.gz',
download_url='https://github.com/timkpaine/jupyterlab_iframe/archive/v0.0.3.tar.gz',
author='Tim Paine',
author_email='t.paine154@gmail.com',
license='GPL',

View file

@ -67,6 +67,22 @@ class OpenIFrameWidget extends Widget {
}
}
function registerSite(app: JupyterLab, palette: ICommandPalette, site: string){
let command = 'iframe:open-' + site;
app.commands.addCommand(command, {
label: 'Open ' + site,
isEnabled: () => true,
execute: () => {
let widget = new IFrameWidget(site);
app.shell.addToMainArea(widget);
app.shell.activateById(widget.id);
}
});
palette.addItem({command: command, category: 'Sites'});
}
function activate(app: JupyterLab, docManager: IDocumentManager, palette: ICommandPalette, restorer: ILayoutRestorer) {
// Declare a widget variable
@ -117,21 +133,30 @@ function activate(app: JupyterLab, docManager: IDocumentManager, palette: IComma
xhr.onload = function (e:any) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let sites = JSON.parse(xhr.responseText);
let jsn = JSON.parse(xhr.responseText);
let welcome = jsn['welcome'];
let welcome_included = false;
let sites = jsn['sites'];
for(let site of sites){
console.log('adding quicklink for ' + site);
let command = 'iframe:open-' + site;
app.commands.addCommand(command, {
label: 'Open ' + site,
isEnabled: () => true,
execute: () => {
widget = new IFrameWidget(site);
app.shell.addToMainArea(widget);
app.shell.activateById(widget.id);
}
});
palette.addItem({command: command, category: 'Sites'});
if (site == welcome){
welcome_included = true;
}
registerSite(app, palette, site);
}
if (!welcome_included){
registerSite(app, palette, welcome);
}
if (welcome) {
app.restored.then(() => {
app.commands.execute('iframe:open-' + welcome);
});
}
} else {
console.error(xhr.statusText);
}