Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: topocm
Views: 392
1
import sys
2
import os
3
from hashlib import md5
4
5
module_dir = os.path.dirname(__file__)
6
sys.path.extend(module_dir)
7
8
from IPython import display
9
import feedparser
10
11
__all__ = ['MoocVideo', 'PreprintReference', 'MoocDiscussion',
12
'MoocCheckboxesAssessment', 'MoocMultipleChoiceAssessment',
13
'MoocPeerAssessment', 'MoocSelfAssessment']
14
15
class MoocComponent(object):
16
def __repr__(self):
17
return '{0}(**{1})'.format(self.__class__.__name__, repr(self.param))
18
19
20
class MoocVideo(MoocComponent, display.YouTubeVideo):
21
def __init__(self, id, src_location=None, res='720', display_name="",
22
download_track='true', download_video='true',
23
show_captions='true', **kwargs):
24
"""A video component of an EdX mooc embeddable in IPython notebook."""
25
tmp = locals()
26
del tmp['kwargs'], tmp['self'], tmp['__class__']
27
del tmp['id'], tmp['src_location'], tmp['res']
28
kwargs.update(tmp)
29
kwargs['youtube_id_1_0'] = id
30
kwargs['youtube'] ="1.00:" + id
31
32
# Add source if provided
33
loc = ("http://delftxdownloads.tudelft.nl/"
34
"TOPOCMx-QuantumKnots/TOPOCMx-{0}-video.{1}.mp4")
35
if src_location is not None:
36
kwargs['source'] = loc.format(src_location, res)
37
38
self.param = kwargs
39
super(MoocVideo, self).__init__(id, rel=0, cc_load_policy=1)
40
41
42
class PreprintReference(object):
43
def __init__(self, index, description="", show_abstract=True):
44
"""Formatted basic information from arxiv preprint."""
45
46
arxiv_query = "http://export.arxiv.org/api/query?id_list="
47
self.index = index
48
self.show_abstract = show_abstract
49
self.description = description
50
self.data = feedparser.parse(arxiv_query + index)
51
52
def _repr_html_(self):
53
data = self.data
54
ind = self.index
55
title = data['entries'][0]['title']
56
s = '<h3 class="title mathjax">' + title + '</h3>'
57
58
s += '<p><a href=http://arxiv.org/abs/%s>http://arxiv.org/abs/%s</a><br>' % (ind, ind)
59
60
s += '<div class="authors">'
61
s += ", ".join(author.name for author in data['entries'][0]['authors'])
62
s += '</div></p>'
63
64
if self.show_abstract:
65
s += '<p><blockquote class="abstract mathjax">'
66
s += data['entries'][0]['summary']
67
s += '</blockquote></p>'
68
69
if self.description:
70
s += '<p><b>Hint:</b> %s </p>' % self.description
71
72
return s
73
74
75
class MoocPeerAssessment(MoocComponent):
76
def __init__(self, must_grade=5, must_be_graded_by=3, due=9, review_due=16,
77
url_name=None, **kwargs):
78
79
self.placeholder = ('<p><b> Read one of the above papers and see how it is\n'
80
'related to the current topic.</b></p>\n'
81
'<p><b>In the live version of the course, you would '
82
'need to write a summary which is then assessed by '
83
'your peers.</b></p>')
84
85
tmp = locals()
86
del tmp['kwargs'], tmp['self']
87
kwargs.update(tmp)
88
89
with open(module_dir + '/xmls/openassessment_peer.xml', 'r') as content_file:
90
openassessment_peer = content_file.read()
91
92
kwargs['openassessment_peer'] = openassessment_peer
93
94
self.param = kwargs
95
96
def _repr_html_(self):
97
return self.placeholder
98
99
100
class MoocSelfAssessment(MoocComponent):
101
def __init__(self, due=9, review_due=16, url_name=None, **kwargs):
102
103
tmp = locals()
104
del tmp['kwargs'], tmp['self']
105
kwargs.update(tmp)
106
107
self.placeholder = ('<p><b> MoocSelfAssessment description</b></p>\n'
108
'<p><b>In the live version of the course, you would '
109
'need to share your solution and grade yourself.'
110
'</b></p>')
111
112
with open(module_dir + '/xmls/openassessment_self.xml', 'r') as content_file:
113
openassessment_self = content_file.read()
114
115
kwargs['openassessment_self'] = openassessment_self
116
117
self.param = kwargs
118
119
def _repr_html_(self):
120
return self.placeholder
121
122
123
class MoocCheckboxesAssessment(MoocComponent):
124
def __init__(self, question, answers, correct_answers, max_attempts=2,
125
display_name="Question", **kwargs):
126
"""
127
MoocCheckboxesAssessment component
128
129
input types
130
-----------
131
132
question : string
133
answers : list of strings
134
correct_answers : list of int
135
136
"""
137
tmp = locals()
138
del tmp['kwargs'], tmp['self']
139
kwargs.update(tmp)
140
141
self.param = kwargs
142
143
def _get_html_repr(self):
144
param = self.param
145
s = '<h4>%s</h4>' % param['question']
146
s += '<form><input type="checkbox"> '
147
s += '<br><input type="checkbox"> '.join(param['answers'])
148
s += '</form>'
149
150
answer = 'The correct answer{0}:<br>'.format('s are' if
151
len(param['correct_answers']) > 1
152
else ' is')
153
tmp = []
154
for i in param['correct_answers']:
155
tmp.append(param['answers'][i])
156
answer += ', '.join(t for t in tmp)
157
answer += '.'
158
try:
159
answer += '<br><i>' + param['explanation'] + '</i>'
160
except KeyError:
161
pass
162
163
s += """
164
<button title="Click to show/hide content" type="button"
165
onclick="if(document.getElementById('{0}')
166
.style.display=='none') {{document.getElementById('{0}')
167
.style.display=''}}else{{document.getElementById('{0}')
168
.style.display='none'}}">Show answer</button>
169
170
<div id="{0}" style="display:none">
171
{1} <br>
172
</div> """
173
return s.format(md5(repr(self).encode('utf-8')).hexdigest(), answer)
174
175
def _repr_html_(self):
176
return self._get_html_repr()
177
178
179
class MoocMultipleChoiceAssessment(MoocComponent):
180
def __init__(self, question, answers, correct_answer, max_attempts=2,
181
display_name="Question", **kwargs):
182
"""
183
MoocMultipleChoiceAssessment component
184
185
Parameters:
186
-----------
187
188
question : string
189
answers : list of strings
190
correct_answers : int
191
192
"""
193
tmp = locals()
194
del tmp['kwargs'], tmp['self']
195
kwargs.update(tmp)
196
197
self.param = kwargs
198
199
def _get_html_repr(self):
200
param = self.param
201
s = '<h4>%s</h4>' % param['question']
202
s+= '<form>'
203
for ans in param['answers']:
204
s += '<input type="radio" name="answer" value="%s">' % ans + ans + '<br>'
205
s+= '</form>'
206
207
answer = 'The correct answer is: <br>'
208
answer += param['answers'][param['correct_answer']]
209
try:
210
answer += '<br><i>' + param['explanation'] + '</i>'
211
except KeyError:
212
pass
213
214
s += """
215
<button title="Click to show/hide content" type="button"
216
onclick="if(document.getElementById('{0}')
217
.style.display=='none') {{document.getElementById('{0}')
218
.style.display=''}}else{{document.getElementById('{0}')
219
.style.display='none'}}">Show answer</button>
220
221
<div id="{0}" style="display:none">{1}</div>"""
222
return s.format(md5(repr(self).encode('utf-8')).hexdigest(), answer)
223
224
def _repr_html_(self):
225
return self._get_html_repr()
226
227
228
class MoocDiscussion(MoocComponent):
229
def __init__(self, discussion_category, discussion_target, display_name=None,
230
discussion_id=None, **kwargs):
231
232
tmp = locals()
233
del tmp['kwargs'], tmp['self']
234
kwargs.update(tmp)
235
236
if display_name is None:
237
kwargs['display_name'] = discussion_target
238
239
if discussion_id is None:
240
kwargs['discussion_id'] = md5(discussion_category.encode('utf-8') + discussion_target.encode('utf-8')).hexdigest()
241
242
self.param = kwargs
243
244
def _repr_html_(self):
245
return "<p><b>Discussion</b> entitled '{0}' is available in the online version of the course.</p>".format(self.param['display_name'])
246
247