Update the session api in preparation for file and console sessions

pull/1359/head
Steven Silvester 10 years ago
parent db7fa4ab85
commit 76d08aca99

@ -349,7 +349,7 @@ paths:
schema:
$ref: '#/definitions/Session'
patch:
summary: "This can be used to rename the notebook, or move it to a new directory."
summary: "This can be used to rename the session."
tags:
- sessions
parameters:
@ -395,29 +395,14 @@ paths:
items:
$ref: '#/definitions/Session'
post:
summary: "Create a new session, or return an existing session if a session for the notebook path already exists"
summary: "Create a new session, or return an existing session if a session of the same name already exists"
tags:
- sessions
parameters:
- name: session
in: body
schema:
type: object
properties:
notebook:
type: object
required:
- path
properties:
path:
type: string
description: path to notebook file
kernel:
type: object
properties:
name:
type: string
description: "Kernel spec name, defaults to default kernel spec"
$ref: '#definitions/Session'
responses:
201:
description: Session created or returned
@ -429,7 +414,7 @@ paths:
type: string
format: url
501:
description: Kernel not available
description: Session not available
schema:
type: object
description: error message
@ -654,12 +639,19 @@ definitions:
id:
type: string
format: uuid
notebook:
type: object
properties:
path:
type: string
description: path to notebook
name:
type: string
description: the name of file, notebook, or console
directory:
type: string
description: the directory of the session
type:
type: string;
description: session type
enum:
- notebook
- file
- console
kernel:
$ref: '#/definitions/Kernel'
Contents:

@ -7,6 +7,7 @@ Preliminary documentation at https://github.com/ipython/ipython/wiki/IPEP-16%3A-
# Distributed under the terms of the Modified BSD License.
import json
import os
from tornado import gen, web
@ -32,16 +33,24 @@ class SessionRootHandler(APIHandler):
@gen.coroutine
def post(self):
# Creates a new session
#(unless a session already exists for the named nb)
#(unless a session already exists for the named session)
sm = self.session_manager
model = self.get_json_body()
if model is None:
raise web.HTTPError(400, "No JSON data provided")
try:
path = model['notebook']['path']
name = model['name']
except KeyError:
raise web.HTTPError(400, "Missing field in JSON data: notebook.path")
raise web.HTTPError(400, "Missing field in JSON data: name")
try:
directory = model['directory']
except KeyError:
raise web.HTTPError(400, "Missing field in JSON data: directory")
model_type = model.get('type', 'notebook')
kernel = model.get('kernel', {})
kernel_name = kernel.get('name') or None
@ -52,6 +61,7 @@ class SessionRootHandler(APIHandler):
kernel_name = None
# Check to see if session exists
path = os.path.join(directory, name)
exists = yield gen.maybe_future(sm.session_exists(path=path))
if exists:
model = yield gen.maybe_future(sm.get_session(path=path))
@ -59,7 +69,8 @@ class SessionRootHandler(APIHandler):
try:
model = yield gen.maybe_future(
sm.create_session(path=path, kernel_name=kernel_name,
kernel_id=kernel_id))
kernel_id=kernel_id,
type=model_type))
except NoSuchKernel:
msg = ("The '%s' kernel is not available. Please pick another "
"suitable kernel instead, or install that kernel." % kernel_name)
@ -105,10 +116,10 @@ class SessionHandler(APIHandler):
before = yield gen.maybe_future(sm.get_session(session_id=session_id))
changes = {}
if 'notebook' in model:
notebook = model['notebook']
if notebook.get('path') is not None:
changes['path'] = notebook['path']
if 'name' in model:
changes['name'] = model['name']
if 'type' in model:
changes['type'] = model['type']
if 'kernel' in model:
# Kernel id takes precedence over name.
if model['kernel'].get('id') is not None:

@ -3,6 +3,7 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import uuid
try:
@ -26,7 +27,7 @@ class SessionManager(LoggingConfigurable):
# Session database initialized below
_cursor = None
_connection = None
_columns = {'session_id', 'path', 'kernel_id'}
_columns = {'session_id', 'path', 'type', 'kernel_id'}
@property
def cursor(self):
@ -34,7 +35,7 @@ class SessionManager(LoggingConfigurable):
if self._cursor is None:
self._cursor = self.connection.cursor()
self._cursor.execute("""CREATE TABLE session
(session_id, path, kernel_id)""")
(session_id, path, type, kernel_id)""")
return self._cursor
@property
@ -56,7 +57,7 @@ class SessionManager(LoggingConfigurable):
self.close()
def session_exists(self, path):
"""Check to see if the session for a given notebook exists"""
"""Check to see if the session of a given name exists"""
self.cursor.execute("SELECT * FROM session WHERE path=?", (path,))
reply = self.cursor.fetchone()
if reply is None:
@ -69,22 +70,22 @@ class SessionManager(LoggingConfigurable):
return unicode_type(uuid.uuid4())
@gen.coroutine
def create_session(self, path=None, kernel_name=None, kernel_id=None):
def create_session(self, path=None, type=None, kernel_name=None, kernel_id=None):
"""Creates a session and returns its model"""
session_id = self.new_session_id()
if kernel_id is not None and kernel_id in self.kernel_manager:
pass
else:
kernel_id = yield self.start_kernel_for_session(session_id, path,
kernel_name)
type, kernel_name)
result = yield gen.maybe_future(
self.save_session(session_id, path=path, kernel_id=kernel_id)
self.save_session(session_id, path=path, type=type, kernel_id=kernel_id)
)
# py2-compat
raise gen.Return(result)
@gen.coroutine
def start_kernel_for_session(self, session_id, path, kernel_name):
def start_kernel_for_session(self, session_id, path, type, kernel_name):
"""Start a new kernel for a given session."""
# allow contents manager to specify kernels cwd
kernel_path = self.contents_manager.get_kernel_path(path=path)
@ -94,7 +95,7 @@ class SessionManager(LoggingConfigurable):
# py2-compat
raise gen.Return(kernel_id)
def save_session(self, session_id, path=None, kernel_id=None):
def save_session(self, session_id, path=None, type=None, kernel_id=None):
"""Saves the items for the session with the given session_id
Given a session_id (and any other of the arguments), this method
@ -115,8 +116,8 @@ class SessionManager(LoggingConfigurable):
model : dict
a dictionary of the session model
"""
self.cursor.execute("INSERT INTO session VALUES (?,?,?)",
(session_id, path, kernel_id)
self.cursor.execute("INSERT INTO session VALUES (?,?,?,?)",
(session_id, path, type, kernel_id)
)
return self.get_session(session_id=session_id)
@ -206,9 +207,9 @@ class SessionManager(LoggingConfigurable):
model = {
'id': row['session_id'],
'notebook': {
'path': row['path']
},
'name': os.path.basename(row['path']),
'directory': os.path.dirname(row['path']),
'type': row['type'],
'kernel': self.kernel_manager.kernel_model(row['kernel_id'])
}
return model

@ -240,8 +240,12 @@ define([
* @returns {Object} - the data model
*/
Session.prototype._get_model = function () {
var path = this.notebook_model.path;
var parts = path.split('/');
return {
notebook: this.notebook_model,
name: parts[parts.length -1],
directory: parts.slice(0, parts.length - 1).join('/'),
type: 'notebook',
kernel: this.kernel_model
};
};
@ -261,7 +265,8 @@ define([
this.session_url = utils.url_path_join(this.session_service_url, this.id);
}
if (data && data.notebook) {
this.notebook_model.path = data.notebook.path;
var path = data.path + '/' + data.name;
this.notebook_model.path = path;
}
if (data && data.kernel) {
this.kernel_model.name = data.kernel.name;

Loading…
Cancel
Save