Skip to content

Commit 338ec4d

Browse files
brichetshreve
andauthored
Backport PR 1902 - Eliminate compatibility issues in supporting notebook and jupyterlab (#1912)
Co-authored-by: Violet Shreve <[email protected]>
1 parent e0b2e65 commit 338ec4d

File tree

8 files changed

+61
-41
lines changed

8 files changed

+61
-41
lines changed

nbgrader/docs/source/conf.py

+4
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@
288288
# If true, do not generate a @detailmenu in the "Top" node's menu.
289289
#texinfo_no_detailmenu = False
290290

291+
linkcheck_ignore = [
292+
# Assume GitHub contributor search links for this repo are always valid
293+
r"https://github.com/search\?q=repo%3Ajupyter%2Fnbgrader\+involves%3A.*type=Issues",
294+
]
291295

292296
# Example configuration for intersphinx: refer to the Python standard library.
293297
intersphinx_mapping = {'python': ('http://docs.python.org/', None)}

nbgrader/server_extensions/formgrader/base.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from tornado import web
66
from jupyter_server.base.handlers import JupyterHandler
7+
from jupyter_server.utils import url_path_join, url_is_absolute
78
from ...api import Gradebook
89
from ...apps.api import NbGraderAPI
910

@@ -41,7 +42,10 @@ def gradebook(self):
4142

4243
@property
4344
def mathjax_url(self):
44-
return self.settings['mathjax_url']
45+
url = self.settings.get("mathjax_url", "")
46+
if not url or url_is_absolute(url):
47+
return url
48+
return url_path_join(self.base_url or "/", url)
4549

4650
@property
4751
def exporter(self):

nbgrader/server_extensions/formgrader/handlers.py

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ def get(self, submission_id):
145145
'base_url': self.base_url,
146146
'student': student_id,
147147
'last_name': submission.student.last_name,
148-
'last_name': submission.student.last_name,
149148
'first_name': submission.student.first_name,
150149
'notebook_path': self.url_prefix + '/' + relative_path
151150
}

nbgrader/server_extensions/formgrader/static/js/jupyterlab_utils.js

-10
This file was deleted.

nbgrader/server_extensions/formgrader/static/js/manage_assignments.js

+3-20
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,14 @@ var AssignmentUI = Backbone.View.extend({
9696

9797
render: function () {
9898
this.clear();
99-
var this_assignment = this;
99+
100100
// assignment name
101101
var name = this.model.get("name")
102102
this.$name.attr("data-order", name);
103103

104-
// Append link with a listener to send message to iframe parent.
105-
// NOTE: the formgrade UI is embedded in an iframe.
106104
this.$name.append($("<a/>")
107105
.text(name)
108-
.attr("href", "#")
109-
.click(function() {
110-
window.parent.postMessage(
111-
jlab_go_to_path(url_prefix + "/" + this_assignment.model.get("source_path")),
112-
'*'
113-
);
114-
})
106+
.map(linkTo("directory", url_prefix + "/" + this.model.get("source_path")))
115107
);
116108

117109
// duedate
@@ -157,16 +149,8 @@ var AssignmentUI = Backbone.View.extend({
157149
// preview student version
158150
var release_path = this.model.get("release_path");
159151
if (release_path) {
160-
// Append link with a listener to send message to iframe parent.
161-
// NOTE: the formgrade UI is embedded in an iframe.
162152
this.$preview.append($("<a/>")
163-
.attr("href", "#")
164-
.click(function() {
165-
window.parent.postMessage(
166-
jlab_go_to_path(url_prefix + "/" + release_path),
167-
'*'
168-
);
169-
})
153+
.map(linkTo("directory", url_prefix + "/" + release_path))
170154
.append($("<span/>")
171155
.addClass("glyphicon glyphicon-search")
172156
.attr("aria-hidden", "true")
@@ -620,7 +604,6 @@ var loadAssignments = function () {
620604
var models = undefined;
621605
var views = [];
622606

623-
624607
$(window).on('load', function () {
625608
loadAssignments();
626609
});

nbgrader/server_extensions/formgrader/static/js/utils.js

+41
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,44 @@ var insertDataTable = function (tbl) {
8787
}]
8888
});
8989
};
90+
91+
var linkTo = function (type, path) {
92+
/*
93+
* Connect a link in the appropriate manner for the context.
94+
* - If we're in the outermost frame, assume notebook and use an href.
95+
* - If we're in an iframe, assume lab and send a message.
96+
*/
97+
if (window === window.top) {
98+
var prefix = {
99+
notebook: "/notebooks/",
100+
file: "/edit/",
101+
directory: "/tree/"
102+
}[type];
103+
104+
return (_, el) => {
105+
return $(el)
106+
.attr("href", prefix + path)
107+
.attr("target", "_blank")[0];
108+
};
109+
} else {
110+
var command = {
111+
notebook: "docmanager:open",
112+
file: "docmanager:open",
113+
directory: "filebrowser:go-to-path",
114+
}[type];
115+
116+
return (_, el) => {
117+
return $(el)
118+
.attr("href", "#")
119+
.click(() => {
120+
window.parent.postMessage(
121+
JSON.stringify({
122+
"command": command,
123+
"arguments": {"path": path}
124+
}),
125+
"*"
126+
);
127+
})[0];
128+
}
129+
}
130+
}

nbgrader/server_extensions/formgrader/templates/base.tpl

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<script src="{{ base_url }}/formgrader/static/components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
1111
<script src="{{ base_url }}/formgrader/static/js/backbone_xsrf.js"></script>
1212
<script src="{{ base_url }}/formgrader/static/js/utils.js"></script>
13-
<script src="{{ base_url }}/formgrader/static/js/jupyterlab_utils.js"></script>
1413

1514
<link rel="stylesheet" href="{{ base_url }}/formgrader/static/components/bootstrap/css/bootstrap.min.css" />
1615
<link rel="stylesheet" href="{{ base_url }}/formgrader/static/components/datatables.net-bs/css/dataTables.bootstrap.min.css">

nbgrader/server_extensions/formgrader/templates/formgrade/formgrade_macros.html.j2

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var base_url = "{{ resources.base_url }}/formgrader";
2020
<script src="{{ resources.base_url }}/formgrader/static/js/formgrade_keyboardmanager.js"></script>
2121
<script src="{{ resources.base_url }}/formgrader/static/js/formgrade_models.js"></script>
2222
<script src="{{ resources.base_url }}/formgrader/static/js/formgrade.js"></script>
23+
<script src="{{ resources.base_url }}/formgrader/static/js/utils.js"></script>
2324
<script type="text/javascript">
2425
function toggle_name(on) {
2526
$(".name-shown").tooltip('hide');
@@ -33,12 +34,11 @@ function toggle_name(on) {
3334
}
3435
}
3536
36-
function jlab_open_notebook(notebook_path) {
37-
return JSON.stringify({
38-
"command": "docmanager:open",
39-
"arguments": {"path": notebook_path}
37+
$(document).ready(function() {
38+
$("[data-link]").each(function() {
39+
$(this).map(linkTo("notebook", $(this).data("link"))).removeAttr("data-link");
4040
});
41-
}
41+
});
4242
</script>
4343

4444
<link rel="stylesheet" href="{{ resources.base_url }}/formgrader/static/components/bootstrap/css/bootstrap.min.css" />
@@ -64,12 +64,12 @@ function jlab_open_notebook(notebook_path) {
6464
<li><a href="{{ resources.base_url }}/formgrader/gradebook/{{ resources.assignment_id }}/{{ resources.notebook_id }}">{{ resources.notebook_id }}</a></li>
6565
<li class="active live-notebook">
6666
<a class="name-hidden" data-toggle="tooltip" data-placement="bottom" title="Open live notebook"
67-
onclick='window.parent.postMessage(jlab_open_notebook("{{ resources.notebook_path }}"), "*");'
67+
data-link="{{ resources.notebook_path }}"
6868
>
6969
Submission #{{ resources.index + 1 }}
7070
</a>
7171
<a class="name-shown" data-toggle="tooltip" data-placement="bottom" title="Open live notebook"
72-
onclick='window.parent.postMessage(jlab_open_notebook("{{ resources.notebook_path }}"), "*"); '
72+
data-link="{{ resources.notebook_path }}"
7373
>
7474
{%- if resources.last_name and resources.first_name -%}
7575
{{ resources.last_name }}, {{ resources.first_name }}
@@ -99,4 +99,4 @@ function jlab_open_notebook(notebook_path) {
9999
$('span.glyphicon.name-hidden').tooltip({title: "Show student name", placement: "bottom", trigger: "hover"});
100100
$('span.glyphicon.name-shown').tooltip({title: "Hide student name", placement: "bottom", trigger: "hover"});
101101
</script>
102-
{% endmacro %}
102+
{% endmacro %}

0 commit comments

Comments
 (0)