##// END OF EJS Templates
Add menu entries for getting converted views of a notebook
Thomas Kluyver -
Show More
@@ -28,6 +28,10 b' class NbconvertFileHandler(IPythonHandler):'
28
28
29 info = os.stat(os_path)
29 info = os.stat(os_path)
30 self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime))
30 self.set_header('Last-Modified', tz.utcfromtimestamp(info.st_mtime))
31 if self.get_argument('download', 'false').lower() == 'true':
32 filename = os.path.splitext(name)[0] + '.' + exporter.file_extension
33 self.set_header('Content-Disposition',
34 'attachment; filename="%s"' % filename)
31
35
32 output, resources = exporter.from_filename(os_path)
36 output, resources = exporter.from_filename(os_path)
33
37
@@ -16,16 +16,17 b' class NbconvertAPI(object):'
16 def __init__(self, base_url):
16 def __init__(self, base_url):
17 self.base_url = base_url
17 self.base_url = base_url
18
18
19 def _req(self, verb, path, body=None):
19 def _req(self, verb, path, body=None, params=None):
20 response = requests.request(verb,
20 response = requests.request(verb,
21 url_path_join(self.base_url, 'nbconvert', path),
21 url_path_join(self.base_url, 'nbconvert', path),
22 data=body,
22 data=body, params=params,
23 )
23 )
24 response.raise_for_status()
24 response.raise_for_status()
25 return response
25 return response
26
26
27 def from_file(self, format, path, name):
27 def from_file(self, format, path, name, download=False):
28 return self._req('GET', url_path_join(format, path, name))
28 return self._req('GET', url_path_join(format, path, name),
29 params={'download':download})
29
30
30 def from_post(self, format, nbmodel):
31 def from_post(self, format, nbmodel):
31 body = json.dumps(nbmodel)
32 body = json.dumps(nbmodel)
@@ -70,6 +71,12 b' class APITest(NotebookTestBase):'
70 with assert_http_error(404):
71 with assert_http_error(404):
71 self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb')
72 self.nbconvert_api.from_file('html', 'foo', 'thisdoesntexist.ipynb')
72
73
74 def test_from_file_download(self):
75 r = self.nbconvert_api.from_file('python', 'foo', 'testnb.ipynb', download=True)
76 content_disposition = r.headers['Content-Disposition']
77 assert 'attachment' in content_disposition
78 assert 'testnb.py' in content_disposition
79
73 def test_from_post(self):
80 def test_from_post(self):
74 nbmodel_url = url_path_join(self.base_url(), 'api/notebooks/foo/testnb.ipynb')
81 nbmodel_url = url_path_join(self.base_url(), 'api/notebooks/foo/testnb.ipynb')
75 nbmodel = requests.get(nbmodel_url).json()
82 nbmodel = requests.get(nbmodel_url).json()
@@ -69,6 +69,26 b' var IPython = (function (IPython) {'
69 );
69 );
70 };
70 };
71
71
72 MenuBar.prototype._nbconvert = function (format, download) {
73 download = download || false;
74 var notebook_name = IPython.notebook.get_notebook_name();
75 if (IPython.notebook.dirty) {
76 IPython.notebook.save_notebook({async : false});
77 }
78 var url = utils.url_path_join(
79 this.baseProjectUrl(),
80 'nbconvert',
81 format,
82 this.notebookPath(),
83 notebook_name + '.ipynb'
84 ) + "?download=" + download.toString();
85
86 if (download) {
87 window.location.assign(url);
88 } else {
89 window.open(url);
90 }
91 }
72
92
73 MenuBar.prototype.bind_events = function () {
93 MenuBar.prototype.bind_events = function () {
74 // File
94 // File
@@ -102,24 +122,17 b' var IPython = (function (IPython) {'
102 window.location.assign(url);
122 window.location.assign(url);
103 });
123 });
104
124
105 /* FIXME: download-as-py doesn't work right now
125 this.element.find('#print_preview').click(function () {
106 * We will need nbconvert hooked up to get this back
126 that._nbconvert('html', false);
107
127 });
128
108 this.element.find('#download_py').click(function () {
129 this.element.find('#download_py').click(function () {
109 var notebook_name = IPython.notebook.get_notebook_name();
130 that._nbconvert('python', true);
110 if (IPython.notebook.dirty) {
131 });
111 IPython.notebook.save_notebook({async : false});
132
112 }
133 this.element.find('#download_html').click(function () {
113 var url = utils.url_path_join(
134 that._nbconvert('html', true);
114 that.baseProjectUrl(),
115 'api/notebooks',
116 that.notebookPath(),
117 notebook_name + '.ipynb?format=py&download=True'
118 );
119 window.location.assign(url);
120 });
135 });
121
122 */
123
136
124 this.element.find('#rename_notebook').click(function () {
137 this.element.find('#rename_notebook').click(function () {
125 IPython.save_widget.rename_notebook();
138 IPython.save_widget.rename_notebook();
@@ -77,10 +77,12 b' class="notebook_app"'
77 </ul>
77 </ul>
78 </li>
78 </li>
79 <li class="divider"></li>
79 <li class="divider"></li>
80 <li id="print_preview"><a href="#">Print Preview</a></li>
80 <li class="dropdown-submenu"><a href="#">Download as</a>
81 <li class="dropdown-submenu"><a href="#">Download as</a>
81 <ul class="dropdown-menu">
82 <ul class="dropdown-menu">
82 <li id="download_ipynb"><a href="#">IPython Notebook (.ipynb)</a></li>
83 <li id="download_ipynb"><a href="#">IPython Notebook (.ipynb)</a></li>
83 <!-- <li id="download_py"><a href="#">Python (.py)</a></li> -->
84 <li id="download_py"><a href="#">Python (.py)</a></li>
85 <li id="download_html"><a href="#">HTML (.html)</a></li>
84 </ul>
86 </ul>
85 </li>
87 </li>
86 <li class="divider"></li>
88 <li class="divider"></li>
General Comments 0
You need to be logged in to leave comments. Login now