Skip to content

Commit 6db93d3

Browse files
authored
Merge pull request #36 from dexplo/format_pep
Format code with black and flake8 to meet PEP
2 parents fad22df + cfb9018 commit 6db93d3

File tree

9 files changed

+634
-433
lines changed

9 files changed

+634
-433
lines changed

jupyter_to_medium/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from ._publish_to_medium import publish
2-
__version__ = '0.2.5'
2+
3+
__version__ = "0.2.5"
4+
35

46
def _jupyter_nbextension_paths():
5-
return [dict(
6-
section="notebook",
7-
src="nbextension",
8-
dest="jupyter_to_medium",
9-
require="jupyter_to_medium/index")]
7+
return [
8+
dict(
9+
section="notebook",
10+
src="nbextension",
11+
dest="jupyter_to_medium",
12+
require="jupyter_to_medium/index",
13+
)
14+
]
15+

jupyter_to_medium/_bundler.py

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,107 @@
11
from pathlib import Path
22
import json
3-
import urllib.parse
4-
5-
from tornado import gen
63

74
from ._publish_to_medium import publish
85

96
# https://testnb.readthedocs.io/en/latest/examples/Notebook/Distributing%20Jupyter%20Extensions%20as%20Python%20Packages.html#
10-
# as explained here this is required to tell Jupyter how we want the bundle to show
7+
# as explained here, this is required to tell Jupyter how wto show bundle
118

129

1310
def _jupyter_bundlerextension_paths():
14-
return [{
15-
# the name of the bundle
16-
"name": "jupyter_to_medium_bundler",
17-
# where the bundler.py file sits to bundle the code
18-
"module_name": "jupyter_to_medium._bundler",
19-
# what we want it to show as in the menu
20-
"label" : "Medium Post",
21-
# which menu we want it as (download vs deploy)
22-
"group" : "deploy",
23-
}]
11+
return [
12+
{
13+
# the name of the bundle
14+
"name": "jupyter_to_medium_bundler",
15+
# where the bundler.py file sits to bundle the code
16+
"module_name": "jupyter_to_medium._bundler",
17+
# what we want it to show as in the menu
18+
"label": "Medium Post",
19+
# which menu we want it as (download vs deploy)
20+
"group": "deploy",
21+
}
22+
]
2423

2524

2625
def upload(model, handler):
27-
arguments = ['title', 'integration_token', 'pub_name', 'tags',
28-
'publish_status', 'notify_followers', 'license', 'canonical_url',
29-
'chrome_path', 'save_markdown', 'table_conversion', 'gistify',
30-
'gist_threshold']
26+
arguments = [
27+
"title",
28+
"integration_token",
29+
"pub_name",
30+
"tags",
31+
"publish_status",
32+
"notify_followers",
33+
"license",
34+
"canonical_url",
35+
"chrome_path",
36+
"save_markdown",
37+
"table_conversion",
38+
"gistify",
39+
"gist_threshold",
40+
]
3141

3242
kwargs = {arg: handler.get_query_argument(arg, None) for arg in arguments}
33-
path = model['path']
34-
kwargs['filename'] = path
35-
kwargs['integration_token'] = kwargs['integration_token'].strip() or None
36-
kwargs['pub_name'] == kwargs['pub_name'].strip() or None
37-
kwargs['tags'] = [tag.strip() for tag in kwargs['tags'].split(',')[:5]]
38-
kwargs['notify_followers'] = kwargs['notify_followers'] == "True"
39-
kwargs['canonical_url'] = kwargs['canonical_url'].strip() or None
40-
kwargs['save_markdown'] = kwargs['save_markdown'] == "True"
41-
kwargs['gistify'] = kwargs['gistify'] == "True"
42-
if kwargs['gist_threshold'] == '':
43-
kwargs['gist_threshold'] = 5
43+
path = model["path"]
44+
kwargs["filename"] = path
45+
kwargs["integration_token"] = kwargs["integration_token"].strip() or None
46+
kwargs["pub_name"] == kwargs["pub_name"].strip() or None
47+
kwargs["tags"] = [tag.strip() for tag in kwargs["tags"].split(",")[:5]]
48+
kwargs["notify_followers"] = kwargs["notify_followers"] == "True"
49+
kwargs["canonical_url"] = kwargs["canonical_url"].strip() or None
50+
kwargs["save_markdown"] = kwargs["save_markdown"] == "True"
51+
kwargs["gistify"] = kwargs["gistify"] == "True"
52+
if kwargs["gist_threshold"] == "":
53+
kwargs["gist_threshold"] = 5
4454
else:
45-
kwargs['gist_threshold'] = int(kwargs['gist_threshold'])
55+
kwargs["gist_threshold"] = int(kwargs["gist_threshold"])
4656

4757
# add these options in the future to html form
4858
# kwargs['chrome_path'] = kwargs['chrome_path'].strip() or None
49-
59+
5060
try:
5161
data = publish(**kwargs)
5262
except Exception as e:
5363
import traceback
64+
5465
error_name = type(e).__name__
55-
error = f'{error_name}: {str(e)}'
66+
error = f"{error_name}: {str(e)}"
5667
tb = traceback.format_exc()
57-
msg = error + f'\n\n{tb}'
68+
msg = error + f"\n\n{tb}"
5869
print(msg)
59-
msg = msg.replace('\n', '<br>')
60-
data = {'app_status': 'fail',
61-
'error_data': msg}
70+
msg = msg.replace("\n", "<br>")
71+
data = {"app_status": "fail", "error_data": msg}
6272
else:
63-
if 'data' in data:
64-
data = data['data']
65-
data['app_status'] = "success"
73+
if "data" in data:
74+
data = data["data"]
75+
data["app_status"] = "success"
6676
else:
67-
data = {'app_status': 'fail',
68-
'error_data': 'Error: \n' + str(data)}
77+
data = {
78+
"app_status": "fail",
79+
"error_data": "Error: \n" + str(data),
80+
}
6981

7082
return data
7183

84+
7285
def read_html(name):
7386
mod_path = Path(__file__).parent
74-
html_path = mod_path / 'static' / f'{name}.html'
87+
html_path = mod_path / "static" / f"{name}.html"
7588
return open(html_path).read()
7689

7790

7891
def get_html_form(xsrf_input, title):
79-
html = read_html('form')
92+
html = read_html("form")
8093
return html.format(xsrf_input=xsrf_input, title=title)
8194

8295

8396
def get_html_success(data):
84-
html = read_html('success')
97+
html = read_html("success")
8598
return html.format(**data)
8699

87100

88101
def get_html_fail(data):
89-
error_data = data['error_data']
102+
error_data = data["error_data"]
90103
error_message = json.dumps(error_data)
91-
html = read_html('fail')
104+
html = read_html("fail")
92105
return html.format(error_message=error_message)
93106

94107

@@ -105,34 +118,33 @@ def bundle(handler, model):
105118
Notebook model from the configured ContentManager
106119
"""
107120
# check on the app_status in the form.html tag
108-
app_status = handler.get_query_argument('app_status', None)
109-
121+
app_status = handler.get_query_argument("app_status", None)
122+
110123
# if we've never initialised the form e.g. when clicked
111124
if app_status is None:
112125
# create the input element to be included in our form
113126
xsrf_input = handler.xsrf_form_html()
114127
# get the base notebook filename to populate the form
115-
notebook_filename = Path(model['name']).stem
128+
notebook_filename = Path(model["name"]).stem
116129
# create the html we will push to the user (the input form)
117130
html = get_html_form(xsrf_input, notebook_filename)
118131
# write this html to the tornado handler so user will see
119132
handler.write(html)
120133
# else if we've already initialised the form
121-
elif app_status == 'waiting':
134+
elif app_status == "waiting":
122135
# read the html if we've filled in more of the user fields
123136
# this is required so we continuously serve the correct html
124-
html = read_html('waiting')
137+
html = read_html("waiting")
125138
# write updated html
126139
handler.write(html)
127140
handler.flush()
128141

129142
data = upload(model, handler)
130-
if data['app_status'] == 'success':
143+
if data["app_status"] == "success":
131144
html = get_html_success(data)
132145
else:
133146
html = get_html_fail(data)
134-
147+
135148
handler.write(html)
136149

137150
handler.finish()
138-

0 commit comments

Comments
 (0)