Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Human readable course titles for multi-class nbgrader on JupyterHub #1797

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions demos/demo_multiple_classes/global_nbgrader_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
c = get_config()
c.Exchange.path_includes_course = True
c.Authenticator.plugin_class = JupyterHubAuthPlugin
c.NbGrader.course_titles = {
'course101': 'Course 101',
'course123': 'Course 123'
}
12 changes: 11 additions & 1 deletion nbgrader/apps/baseapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from jupyter_core.application import JupyterApp
from textwrap import dedent
from tornado.log import LogFormatter
from traitlets import Unicode, List, Bool, Instance, default
from traitlets import Unicode, List, Bool, Instance, default, Dict
from traitlets.config.application import catch_config_error
from traitlets.config.loader import Config

Expand Down Expand Up @@ -66,6 +66,16 @@ class NbGrader(JupyterApp):

_log_formatter_cls = LogFormatter

course_titles = Dict(
{},
help=dedent(
"""
Dict mapping course IDs to human readable course titles. If there is
no title for a course, ID is shown.
"""
)
).tag(config=True)

@default("log_level")
def _log_level_default(self) -> int:
return logging.INFO
Expand Down
15 changes: 15 additions & 0 deletions nbgrader/coursedir.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ def _validate_course_id(self, proposal):
self.log.warning("course_id '%s' has trailing whitespace, stripping it away", proposal['value'])
return proposal['value'].strip()

course_title = Unicode(
'',
help=dedent(
"""
A human readable course name for display purposes.
"""
)
).tag(config=True)

@validate('course_title')
def _validate_course_title(self, proposal):
if proposal['value'].strip() != proposal['value']:
self.log.warning("course_title '%s' has trailing whitespace, stripping it away", proposal['value'])
return proposal['value'].strip()

student_id = Unicode(
"*",
help=dedent(
Expand Down
15 changes: 14 additions & 1 deletion nbgrader/server_extensions/assignment_list/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def load_config(self):

return app.config

def get_course_titles(self):
paths = jupyter_config_path()
paths.insert(0, os.getcwd())

app = NbGrader()
app.config_file_paths.append(paths)
app.load_config_file()

return app.course_titles

@contextlib.contextmanager
def get_assignment_dir_config(self):

Expand Down Expand Up @@ -186,9 +196,12 @@ def list_courses(self):
if not assignments["success"]:
return assignments

course_ids = list(set([x["course_id"] for x in assignments["value"]]))
titles_dict = self.get_course_titles()
course_ids.sort(key=lambda x: titles_dict.get(x, x))
retvalue = {
"success": True,
"value": sorted(list(set([x["course_id"] for x in assignments["value"]])))
"value": [{"course_id": x, "course_title": titles_dict.get(x, x)} for x in course_ids]
}

return retvalue
Expand Down
23 changes: 23 additions & 0 deletions nbgrader/server_extensions/course_list/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def load_config(self):
app.load_config_file()

return app.config

def get_course_titles(self):
paths = jupyter_config_path()
paths.insert(0, os.getcwd())

app = NbGrader()
app.config_file_paths.append(paths)
app.load_config_file()

return app.course_titles

@gen.coroutine
def check_for_local_formgrader(self, config):
Expand All @@ -77,8 +87,12 @@ def check_for_local_formgrader(self, config):
coursedir = CourseDirectory(config=config)

if status:
title = coursedir.course_title
if not title:
title = coursedir.course_id
raise gen.Return([{
'course_id': coursedir.course_id,
'course_title': title,
'url': base_url + '/formgrader',
'kind': 'local'
}])
Expand Down Expand Up @@ -111,8 +125,12 @@ def check_for_noauth_jupyterhub_formgraders(self, config):
self.log.error("Formgrader not available at URL: %s", url)
raise gen.Return([])

title = coursedir.course_title
if not title:
title = coursedir.course_id
courses = [{
'course_id': coursedir.course_id,
'course_title': title,
'url': url + "/lab?formgrader=true",
'kind': 'jupyterhub'
}]
Expand Down Expand Up @@ -149,13 +167,18 @@ def check_for_jupyterhub_formgraders(self, config):
raise gen.Return([])

courses = []
course_titles = self.get_course_titles()
for course in course_names:
if course not in services:
self.log.warning("Couldn't find formgrader for course '%s'", course)
continue
service = services[course]
title = course_titles.get(course)
if not title:
title = course
courses.append({
'course_id': course,
'course_title': title,
'url': self.get_base_url() + service['prefix'].rstrip('/') + "/lab?formgrader=true",
'kind': 'jupyterhub'
})
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions src/assignment_list/assignmentlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,10 @@ export class CourseList{
dropdown_selector: string;
refresh_selector: string;
assignment_list: AssignmentList;
current_course: string;
current_course: { [key: string]: string }
options = new Map();
base_url: string;
data : string[];
data : { [key: string]: string }[];
course_list_element : HTMLUListElement;
default_course_element: HTMLButtonElement;
dropdown_element: HTMLButtonElement;
Expand Down Expand Up @@ -673,7 +673,7 @@ private handle_load_list(data: { success: any; value: any; }): void {
}
};

private load_list_success(data: string[]): void {
private load_list_success(data: { [key: string]: string }[]): void {
this.data = data;
this.disable_list()
this.clear_list();
Expand All @@ -698,28 +698,28 @@ private load_list_success(data: string[]): void {
}
};

private change_course(course: string): void {
private change_course(course: { [key: string]: string }): void {
this.disable_list();
if (this.current_course !== undefined) {
this.default_course_element.innerText = course;
this.default_course_element.innerText = course['course_title'];
}
this.current_course = course;
this.default_course_element.innerText = this.current_course;
this.default_course_element.innerText = this.current_course['course_title'];
var success = ()=>{this.load_assignment_list_success()};
this.assignment_list.load_list(course, success);
this.assignment_list.load_list(course['course_id'], success);
};

private load_assignment_list_success(): void {
if (this.data) {
var that = this;
var set_course = function (course: string) {
var set_course = function (course: { [key: string]: string }) {
return function () { that.change_course(course); };
}

for (var i=0; i<this.data.length; i++) {
var a = document.createElement('a');
a.href = '#';
a.innerText = this.data[i];
a.innerText = this.data[i]['course_title'];
var element = document.createElement('li');
element.append(a);
element.onclick = set_course(this.data[i]);
Expand Down
2 changes: 1 addition & 1 deletion src/course_list/courselist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function createElementFromCourse(data: any, app: JupyterFrontEnd, isNotebook:boo

var anchor = document.createElement('a') as HTMLAnchorElement;
anchor.href = '#';
anchor.innerText = data['course_id'];
anchor.innerText = data['course_title'];
if (data['kind'] == 'local') {
anchor.href = '#';
anchor.onclick = function() {
Expand Down
Loading