Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.

Commit 12a2ef0

Browse files
committed
Simplify starting rsession
- No AJAX needed. Visiting /rsessionproxy now starts rsession & redirects you to appropriate URL - Don't require a POST for starting rsession - a GET is good enough. If it's already started, we redirect user to it. If not, we start it. - Remove DELETE verb handler, since it is never used
1 parent 4224ce3 commit 12a2ef0

File tree

2 files changed

+6
-96
lines changed

2 files changed

+6
-96
lines changed

nbrsessionproxy/handlers.py

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -63,46 +63,6 @@ def initialize(self, state):
6363
def rsession_uri(self):
6464
return '{}proxy/{}/'.format(self.base_url, self.port)
6565

66-
def gen_response(self, proc):
67-
response = {
68-
'pid': proc.pid,
69-
'url':self.rsession_uri(),
70-
}
71-
return response
72-
73-
def get_client_id(self):
74-
'''Returns either None or the value of 'active-client-id' from
75-
~/.rstudio/session-persistent-state.'''
76-
77-
client_id = None
78-
79-
# Assume the contents manager is local
80-
#root_dir = self.settings['contents_manager'].root_dir
81-
#root_dir = self.config.FileContentsManager.root_dir
82-
root_dir = os.getcwd()
83-
84-
self.log.debug('client_id: root_dir: {}'.format(root_dir))
85-
path = os.path.join(root_dir, '.rstudio', 'session-persistent-state')
86-
if not os.path.exists(path):
87-
self.log.debug('client_id: No such file: {}'.format(path))
88-
return client_id
89-
90-
try:
91-
buf = open(path).read()
92-
except Exception as e:
93-
self.log.debug("client_id: could not read {}: {}".format(path, e))
94-
return client_id
95-
96-
self.log.debug("client_id: read {} bytes".format(len(buf)))
97-
config_key = 'active-client-id'
98-
for line in buf.split():
99-
if line.startswith(config_key + '='):
100-
# remove the key, '=', and leading and trailing quotes
101-
client_id = line[len(config_key)+1+1:-1]
102-
self.log.debug('client_id: read: {}'.format(client_id))
103-
break
104-
105-
return client_id
10666
def is_running(self):
10767
'''Check if our proxied process is still running.'''
10868

@@ -132,15 +92,13 @@ def is_running(self):
13292

13393

13494
@web.authenticated
135-
def post(self):
95+
def get(self):
13696
'''Start a new rsession.'''
13797

13898
if self.is_running():
13999
proc = self.state['proc']
140100
self.log.info('Resuming process on port {}'.format(self.port))
141-
response = self.gen_response(proc)
142-
self.finish(json.dumps(response))
143-
return
101+
return self.redirect(self.rsession_uri())
144102

145103
self.log.debug('No existing process')
146104

@@ -185,29 +143,7 @@ def post(self):
185143
# Store our process
186144
self.state['proc'] = proc
187145

188-
response = self.gen_response(proc)
189-
190-
client_id = self.get_client_id()
191-
self.log.debug('post: client_id: {}'.format(client_id))
192-
self.finish(json.dumps(response))
193-
194-
@web.authenticated
195-
def get(self):
196-
if self.is_running():
197-
proc = self.state['proc']
198-
self.log.info('Process exists on port {}'.format(self.port))
199-
response = self.gen_response(proc)
200-
self.finish(json.dumps(response))
201-
return
202-
self.finish(json.dumps({}))
203-
204-
@web.authenticated
205-
def delete(self):
206-
if 'proc' not in self.state:
207-
raise web.HTTPError(reason='no rsession running', status_code=500)
208-
proc = self.state['proc']
209-
proc.kill()
210-
self.finish()
146+
return self.redirect(self.rsession_uri())
211147

212148
def setup_handlers(web_app):
213149
host_pattern = '.*$'

nbrsessionproxy/static/tree.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,8 @@ define(function(require) {
33
var Jupyter = require('base/js/namespace');
44
var utils = require('base/js/utils');
55

6-
var ajax = utils.ajax || $.ajax;
7-
86
var base_url = utils.get_body_data('baseUrl');
97

10-
function open_rsession(w) {
11-
/* the url we POST to to start rsession */
12-
var rsp_url = base_url + 'rsessionproxy';
13-
14-
/* prepare ajax */
15-
var settings = {
16-
type: "POST",
17-
data: {},
18-
dataType: "json",
19-
success: function(data) {
20-
if (!("url" in data)) {
21-
/* FIXME: visit some template */
22-
return;
23-
}
24-
w.location = data['url'];
25-
},
26-
error : utils.log_ajax_error,
27-
}
28-
29-
ajax(rsp_url, settings);
30-
}
318

329
function load() {
3310
console.log("nbrsessionproxy loading");
@@ -53,12 +30,9 @@ define(function(require) {
5330
var rsession_link = $('<a>')
5431
.attr('role', 'menuitem')
5532
.attr('tabindex', '-1')
56-
.attr('href', '#')
57-
.text('RStudio Session')
58-
.on('click', function() {
59-
var w = window.open(undefined, Jupyter._target);
60-
open_rsession(w);
61-
});
33+
.attr('href', base_url + 'rsessionproxy')
34+
.attr('target', '_blank')
35+
.text('RStudio Session');
6236

6337
/* add the link to the item and
6438
* the item to the menu */

0 commit comments

Comments
 (0)