瀏覽代碼

[notebook] Move jupyter export into notebook view model

Johan Ahlen 10 年之前
父節點
當前提交
1d72300

+ 39 - 2
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -750,7 +750,7 @@
     };
 
     self.addSession = function (session) {
-      var toRemove = []
+      var toRemove = [];
       $.each(self.sessions(), function (index, s) {
         if (s.type() == session.type()) {
           toRemove.push(s);
@@ -869,7 +869,7 @@
         if (snippet.id() == id) idx = cnt;
       });
       self.snippets(self.snippets().move(self.snippets().length - 1, idx));
-    }
+    };
 
     self.getContext = function() {
      return {
@@ -942,6 +942,43 @@
         }
       });
     };
+
+    self.exportJupyterNotebook = function () {
+      function addCell(type, code) {
+        var cell = {
+          cell_type: type,
+          source: [
+            code
+          ],
+          metadata: {
+            collapsed: false
+          }
+        };
+        if (type == "code") {
+          cell.outputs = [];
+          cell.execution_count = 0;
+        }
+        return cell;
+      }
+
+      var jupyterNotebook = {
+        nbformat: 4,
+        nbformat_minor: 0,
+        cells: [],
+        metadata: {}
+      };
+
+      self.snippets().forEach(function (snippet) {
+        if (snippet.type() == "pyspark") {
+          jupyterNotebook.cells.push(addCell("code", snippet.statement_raw()));
+        }
+        if (snippet.type() == "markdown") {
+          jupyterNotebook.cells.push(addCell("markdown",snippet.statement_raw()));
+        }
+      });
+
+      download(JSON.stringify(jupyterNotebook), self.name() + ".ipynb", "text/plain");
+    }
   };
 
 

+ 2 - 51
desktop/libs/notebook/src/notebook/templates/editor_components.mako

@@ -169,7 +169,7 @@ ${ require.config() }
           </a>
         </li>
         <li>
-          <a class="pointer" data-bind="click: exportJupyterNotebook">
+          <a class="pointer" data-bind="click: function() { $root.selectedNotebook().exportJupyterNotebook() }">
             <i class="fa fa-fw fa-file-code-o"></i> ${ _('Export to Jupyter') }
           </a>
         </li>
@@ -1443,7 +1443,7 @@ ${ require.config() }
     "ko.hue-bindings"
   ], function (ko, charts, EditorViewModel) {
 
-      var VIEW_MODEL_OPTIONS = $.extend(${ options_json | n,unicode }, {
+    var VIEW_MODEL_OPTIONS = $.extend(${ options_json | n,unicode }, {
       user: '${ user.username }',
       assistAvailable: '${ autocomplete_base_url | n,unicode }' !== '',
       snippetViewSettings: {
@@ -1528,55 +1528,6 @@ ${ require.config() }
 
     var viewModel;
 
-    var exportJupyterNotebook = function () {
-      function addCell(type, code) {
-        var cell = {
-          cell_type: type,
-          source: [
-            code
-          ],
-          metadata: {
-            collapsed: false
-          }
-        }
-        if (type == "code") {
-          cell.outputs = [];
-          cell.execution_count = 0;
-        }
-        return cell;
-      }
-
-      function addCodeCell(code) {
-        return addCell("code", code);
-      }
-
-      function addMarkdownCell(code) {
-        return addCell("markdown", code);
-      }
-
-      var currentNotebook = viewModel.notebooks()[0];
-      var jupyterNotebook = {
-        nbformat: 4,
-        nbformat_minor: 0,
-        cells: [],
-        metadata: {}
-      }
-
-      currentNotebook.snippets().forEach(function (snippet) {
-        if (snippet.type() == "pyspark") {
-          jupyterNotebook.cells.push(addCodeCell(snippet.statement_raw()));
-        }
-        if (snippet.type() == "markdown") {
-          jupyterNotebook.cells.push(addMarkdownCell(snippet.statement_raw()));
-        }
-      });
-
-
-      download(JSON.stringify(jupyterNotebook), currentNotebook.name() + ".ipynb", "text/plain");
-    }
-
-    window.exportJupyterNotebook = exportJupyterNotebook;
-
     var importExternalNotebook = function (notebook) {
       var currentNotebook = viewModel.notebooks()[0];
       currentNotebook.name(notebook.name);