1
1
from pathlib import Path
2
2
import json
3
- import urllib .parse
4
-
5
- from tornado import gen
6
3
7
4
from ._publish_to_medium import publish
8
5
9
6
# 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
11
8
12
9
13
10
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
+ ]
24
23
25
24
26
25
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
+ ]
31
41
32
42
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
44
54
else :
45
- kwargs [' gist_threshold' ] = int (kwargs [' gist_threshold' ])
55
+ kwargs [" gist_threshold" ] = int (kwargs [" gist_threshold" ])
46
56
47
57
# add these options in the future to html form
48
58
# kwargs['chrome_path'] = kwargs['chrome_path'].strip() or None
49
-
59
+
50
60
try :
51
61
data = publish (** kwargs )
52
62
except Exception as e :
53
63
import traceback
64
+
54
65
error_name = type (e ).__name__
55
- error = f' { error_name } : { str (e )} '
66
+ error = f" { error_name } : { str (e )} "
56
67
tb = traceback .format_exc ()
57
- msg = error + f' \n \n { tb } '
68
+ msg = error + f" \n \n { tb } "
58
69
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 }
62
72
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"
66
76
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
+ }
69
81
70
82
return data
71
83
84
+
72
85
def read_html (name ):
73
86
mod_path = Path (__file__ ).parent
74
- html_path = mod_path / ' static' / f' { name } .html'
87
+ html_path = mod_path / " static" / f" { name } .html"
75
88
return open (html_path ).read ()
76
89
77
90
78
91
def get_html_form (xsrf_input , title ):
79
- html = read_html (' form' )
92
+ html = read_html (" form" )
80
93
return html .format (xsrf_input = xsrf_input , title = title )
81
94
82
95
83
96
def get_html_success (data ):
84
- html = read_html (' success' )
97
+ html = read_html (" success" )
85
98
return html .format (** data )
86
99
87
100
88
101
def get_html_fail (data ):
89
- error_data = data [' error_data' ]
102
+ error_data = data [" error_data" ]
90
103
error_message = json .dumps (error_data )
91
- html = read_html (' fail' )
104
+ html = read_html (" fail" )
92
105
return html .format (error_message = error_message )
93
106
94
107
@@ -105,34 +118,33 @@ def bundle(handler, model):
105
118
Notebook model from the configured ContentManager
106
119
"""
107
120
# 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
+
110
123
# if we've never initialised the form e.g. when clicked
111
124
if app_status is None :
112
125
# create the input element to be included in our form
113
126
xsrf_input = handler .xsrf_form_html ()
114
127
# get the base notebook filename to populate the form
115
- notebook_filename = Path (model [' name' ]).stem
128
+ notebook_filename = Path (model [" name" ]).stem
116
129
# create the html we will push to the user (the input form)
117
130
html = get_html_form (xsrf_input , notebook_filename )
118
131
# write this html to the tornado handler so user will see
119
132
handler .write (html )
120
133
# else if we've already initialised the form
121
- elif app_status == ' waiting' :
134
+ elif app_status == " waiting" :
122
135
# read the html if we've filled in more of the user fields
123
136
# this is required so we continuously serve the correct html
124
- html = read_html (' waiting' )
137
+ html = read_html (" waiting" )
125
138
# write updated html
126
139
handler .write (html )
127
140
handler .flush ()
128
141
129
142
data = upload (model , handler )
130
- if data [' app_status' ] == ' success' :
143
+ if data [" app_status" ] == " success" :
131
144
html = get_html_success (data )
132
145
else :
133
146
html = get_html_fail (data )
134
-
147
+
135
148
handler .write (html )
136
149
137
150
handler .finish ()
138
-
0 commit comments