Skip to content

Commit 46e1447

Browse files
committed
Remove superfluous docstrings and change them to comments which are more intelligently positioned in the code.
1 parent 48b1c49 commit 46e1447

File tree

4 files changed

+78
-48
lines changed

4 files changed

+78
-48
lines changed

nbviewer/providers/base.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class BaseHandler(web.RequestHandler):
5757
"""Base Handler class with common utilities"""
5858

5959
def initialize(self, format=None, format_prefix="", **handler_settings):
60+
# format: str, optional
61+
# Rendering format (e.g. script, slides, html)
6062
self.format = format or self.default_format
6163
self.format_prefix = format_prefix
6264
self.http_client = httpclient.AsyncHTTPClient()
@@ -631,20 +633,34 @@ def filter_formats(self, nb, raw):
631633
app_log.info("failed to test %s: %s", self.request.uri, name)
632634

633635
# empty methods to be implemented by subclasses to make GET requests more modular
634-
def format_notebook_request(self, **kwargs):
636+
def get_notebook_data(self, **kwargs):
637+
"""
638+
Pass as kwargs variables needed to define those variables which will be necessary for
639+
the provider to find the notebook. (E.g. path for LocalHandler, user and repo for GitHub.)
640+
Return variables the provider needs to find and load the notebook. Then run custom logic
641+
in GET or pass the output of get_notebook_data immediately to deliver_notebook.
642+
643+
First part of any provider's GET method.
644+
645+
Custom logic, if applicable, is middle part of any provider's GET method, and usually
646+
is implemented or overwritten in subclasses, while get_notebook_data and deliver_notebook
647+
will often remain unchanged from the parent class (e.g. for a custom GitHub provider).
648+
"""
635649
pass
636650

637-
def load_notebook(self, **kwargs):
651+
def deliver_notebook(self, **kwargs):
652+
"""
653+
Pass as kwargs the return values of get_notebook_data to this method. Get the JSON data
654+
from the provider to render the notebook. Finish with a call to self.finish_notebook.
655+
656+
Last part of any provider's GET method.
657+
"""
638658
pass
639659

640660
# Wrappers to facilitate custom rendering in subclasses without having to rewrite entire GET methods
641661
# This would seem to mostly involve creating different template namespaces to enable custom logic in
642662
# extended templates, but there might be other possibilities
643663
def render_notebook_template(self, body, nb, download_url, json_notebook, **namespace):
644-
"""
645-
format: str, optional
646-
Rendering format (e.g., script, slides, html)
647-
"""
648664
return self.render_template(
649665
"formats/%s.html" % self.format,
650666
body=body,

nbviewer/providers/gist/handlers.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424
from .. import _load_handler_from_location
2525

2626
class GistClientMixin(GithubClientMixin):
27-
"""
28-
provider_label: str
29-
Text to to apply to the navbar icon linking to the provider
30-
provider_icon: str
31-
CSS classname to apply to the navbar icon linking to the provider
32-
executor_label: str, optional
33-
Text to apply to the navbar icon linking to the execution service
34-
executor_icon: str, optional
35-
CSS classname to apply to the navbar icon linking to the execution service
36-
"""
27+
28+
# PROVIDER_CTX is a dictionary whose entries are passed as keyword arguments
29+
# to the render_template method of the GistHandler. The following describe
30+
# the information contained in each of these keyword arguments:
31+
# provider_label: str
32+
# Text to to apply to the navbar icon linking to the provider
33+
# provider_icon: str
34+
# CSS classname to apply to the navbar icon linking to the provider
35+
# executor_label: str, optional
36+
# Text to apply to the navbar icon linking to the execution service
37+
# executor_icon: str, optional
38+
# CSS classname to apply to the navbar icon linking to the execution service
3739
PROVIDER_CTX = {
3840
'provider_label': 'Gist',
3941
'provider_icon': 'github-square',
@@ -63,7 +65,7 @@ def render_usergists_template(self, entries, user, provider_url, prev_url,
6365
"""
6466
provider_url: str
6567
URL to the notebook document upstream at the provider (e.g., GitHub)
66-
executor_url: str, optional
68+
executor_url: str, optional (kwarg passed into `namespace`)
6769
URL to execute the notebook document (e.g., Binder)
6870
"""
6971
return self.render_template("usergists.html", entries=entries, user=user,
@@ -139,10 +141,7 @@ def parse_gist(self, user, gist_id, filename=''):
139141
@gen.coroutine
140142
def tree_get(self, user, gist_id, gist, files):
141143
"""
142-
provider_url:
143-
URL to the notebook document upstream at the provider (e.g., GitHub)
144-
executor_url: str, optional
145-
URL to execute the notebook document (e.g., Binder)
144+
user, gist_id, gist, and files are (most) of the values returned by parse_gist
146145
"""
147146
entries = []
148147
ipynbs = []
@@ -175,6 +174,10 @@ def tree_get(self, user, gist_id, gist, files):
175174
gist_id=gist_id
176175
) if self.binder_base_url else None
177176

177+
# provider_url:
178+
# URL to the notebook document upstream at the provider (e.g., GitHub)
179+
# executor_url: str, optional
180+
# URL to execute the notebook document (e.g., Binder)
178181
html = self.render_template(
179182
'treelist.html',
180183
entries=entries,
@@ -197,8 +200,12 @@ def file_get(self, user, gist_id, filename, gist, many_files_gist, file):
197200

198201
yield self.deliver_notebook(user, gist_id, filename, gist, file, content)
199202

203+
# Only called by file_get
200204
@gen.coroutine
201205
def get_notebook_data(self, gist_id, filename, many_files_gist, file):
206+
"""
207+
gist_id, filename, many_files_gist, file are all passed to file_get
208+
"""
202209
if (file['type'] or '').startswith('image/'):
203210
app_log.debug("Fetching raw image (%s) %s/%s: %s", file['type'], gist_id, filename, file['raw_url'])
204211
response = yield self.fetch(file['raw_url'])
@@ -220,11 +227,13 @@ def get_notebook_data(self, gist_id, filename, many_files_gist, file):
220227
else:
221228
return content
222229

230+
# Only called by file_get
223231
@gen.coroutine
224232
def deliver_notebook(self, user, gist_id, filename, gist, file, content):
225233
"""
226-
provider_url: str, optional
227-
URL to the notebook document upstream at the provider (e.g., GitHub)
234+
user, gist_id, filename, gist, file, are the same values as those
235+
passed into file_get, whereas content is returned from
236+
get_notebook_data using user, gist_id, filename, gist, and file.
228237
"""
229238
# Enable a binder navbar icon if a binder base URL is configured
230239
executor_url = self.BINDER_PATH_TMPL.format(
@@ -234,6 +243,8 @@ def deliver_notebook(self, user, gist_id, filename, gist, file, content):
234243
path=quote(filename)
235244
) if self.binder_base_url else None
236245

246+
# provider_url: str, optional
247+
# URL to the notebook document upstream at the provider (e.g., GitHub)
237248
yield self.finish_notebook(
238249
content,
239250
file['raw_url'],
@@ -246,7 +257,10 @@ def deliver_notebook(self, user, gist_id, filename, gist, file, content):
246257
@cached
247258
@gen.coroutine
248259
def get(self, user, gist_id, filename=''):
249-
260+
"""
261+
Encompasses both the case of a single file gist, handled by
262+
`file_get`, as well as a many-file gist, handled by `tree_get`.
263+
"""
250264
user, gist_id, gist, files, many_files_gist = yield self.parse_gist(user, gist_id, filename)
251265

252266
if many_files_gist and not filename:

nbviewer/providers/github/handlers.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,18 @@ def _github_url():
3939
return os.environ.get('GITHUB_URL') if os.environ.get('GITHUB_URL', '') else "https://github.com/"
4040

4141
class GithubClientMixin(object):
42-
"""
43-
provider_label: str
44-
Text to to apply to the navbar icon linking to the provider
45-
provider_icon: str
46-
CSS classname to apply to the navbar icon linking to the provider
47-
executor_label: str, optional
48-
Text to apply to the navbar icon linking to the execution service
49-
executor_icon: str, optional
50-
CSS classname to apply to the navbar icon linking to the execution service
51-
"""
42+
43+
# PROVIDER_CTX is a dictionary whose entries are passed as keyword arguments
44+
# to the render_template method of the GistHandler. The following describe
45+
# the information contained in each of these keyword arguments:
46+
# provider_label: str
47+
# Text to to apply to the navbar icon linking to the provider
48+
# provider_icon: str
49+
# CSS classname to apply to the navbar icon linking to the provider
50+
# executor_label: str, optional
51+
# Text to apply to the navbar icon linking to the execution service
52+
# executor_icon: str, optional
53+
# CSS classname to apply to the navbar icon linking to the execution service
5254
PROVIDER_CTX = {
5355
'provider_label': 'GitHub',
5456
'provider_icon': 'github',
@@ -301,14 +303,6 @@ def get_notebook_data(self, user, repo, ref, path):
301303

302304
@gen.coroutine
303305
def deliver_notebook(self, user, repo, ref, path, raw_url, blob_url, tree_entry):
304-
"""
305-
provider_url:
306-
URL to the notebook document upstream at the provider (e.g., GitHub)
307-
breadcrumbs: list of dict
308-
Breadcrumb 'name' and 'url' to render as links at the top of the notebook page
309-
executor_url: str, optional
310-
URL to execute the notebook document (e.g., Binder)
311-
"""
312306
# fetch file data from the blobs API
313307
with self.catch_client_error():
314308
response = yield self.github_client.fetch(tree_entry['url'])
@@ -351,6 +345,13 @@ def deliver_notebook(self, user, repo, ref, path, raw_url, blob_url, tree_entry)
351345
except Exception as e:
352346
app_log.error("Failed to decode notebook: %s", raw_url, exc_info=True)
353347
raise web.HTTPError(400)
348+
# Explanation of some kwargs passed into `finish_notebook`:
349+
# provider_url:
350+
# URL to the notebook document upstream at the provider (e.g., GitHub)
351+
# breadcrumbs: list of dict
352+
# Breadcrumb 'name' and 'url' to render as links at the top of the notebook page
353+
# executor_url: str, optional
354+
# URL to execute the notebook document (e.g., Binder)
354355
yield self.finish_notebook(nbjson, raw_url,
355356
provider_url=blob_url,
356357
executor_url=executor_url,

nbviewer/providers/local/handlers.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,6 @@ def get_notebook_data(self, path):
158158

159159
@gen.coroutine
160160
def deliver_notebook(self, fullpath, path):
161-
"""
162-
breadcrumbs: list of dict
163-
Breadcrumb 'name' and 'url' to render as links at the top of the notebook page
164-
title: str
165-
Title to use as the HTML page title (i.e., text on the browser tab)
166-
"""
167161
try:
168162
with io.open(fullpath, encoding='utf-8') as f:
169163
nbdata = f.read()
@@ -174,6 +168,11 @@ def deliver_notebook(self, fullpath, path):
174168
raise web.HTTPError(404)
175169
raise ex
176170

171+
# Explanation of some kwargs passed into `finish_notebook`:
172+
# breadcrumbs: list of dict
173+
# Breadcrumb 'name' and 'url' to render as links at the top of the notebook page
174+
# title: str
175+
# Title to use as the HTML page title (i.e., text on the browser tab)
177176
yield self.finish_notebook(nbdata,
178177
download_url='?download',
179178
msg="file from localfile: %s" % path,

0 commit comments

Comments
 (0)