diff --git a/python/helpers/py2ipnb_converter.py b/python/helpers/py2ipnb_converter.py new file mode 100644 index 000000000000..5d538379cede --- /dev/null +++ b/python/helpers/py2ipnb_converter.py @@ -0,0 +1,32 @@ +import sys +import nbformat +from nbformat.v4 import new_code_cell, new_notebook + +import codecs + +name = sys.argv[-1][:-3] +source = name + ".py" +target = name + ".ipynb" + + +def parse_python(file): + with open(file, "r") as file_descriptor: + lines = [] + for line in file_descriptor: + line = line.strip() + if line.startswith('# In[') and line.endswith(']:') and lines: + yield "".join(lines) + lines = [] + continue + lines.append(line) + if lines: + yield "".join(lines) + +cells = [] +for cell_content in parse_python(source): + cells.append(new_code_cell(source=cell_content)) + +nb0 = new_notebook(cells=cells, metadata={'language': 'python',}) + +with codecs.open(target, encoding='utf-8', mode='w') as f: + nbformat.write(nb0, f, 4) diff --git a/python/ipnb/resources/META-INF/ipython-notebook.xml b/python/ipnb/resources/META-INF/ipython-notebook.xml index 36f48a4a803f..1b96878b62f2 100644 --- a/python/ipnb/resources/META-INF/ipython-notebook.xml +++ b/python/ipnb/resources/META-INF/ipython-notebook.xml @@ -49,6 +49,11 @@ + + + + diff --git a/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbConvertFromPythonAction.java b/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbConvertFromPythonAction.java new file mode 100644 index 000000000000..db58f56ddc7a --- /dev/null +++ b/python/ipnb/src/org/jetbrains/plugins/ipnb/editor/actions/IpnbConvertFromPythonAction.java @@ -0,0 +1,69 @@ +package org.jetbrains.plugins.ipnb.editor.actions; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.openapi.roots.ProjectFileIndex; +import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; +import com.jetbrains.python.PythonHelpersLocator; +import com.jetbrains.python.psi.PyFile; +import com.jetbrains.python.sdk.PySdkUtil; +import com.jetbrains.python.sdk.PythonSdkType; +import icons.PythonIcons; +import org.jetbrains.annotations.NotNull; + +public class IpnbConvertFromPythonAction extends AnAction { + public IpnbConvertFromPythonAction() { + super(PythonIcons.Python.IpythonNotebook); + } + + @Override + public void actionPerformed(@NotNull AnActionEvent event) { + final DataContext context = event.getDataContext(); + final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(context); + final Project project = CommonDataKeys.PROJECT.getData(context); + convertFromPythonScript(virtualFile, project); + } + + @Override + public void update(AnActionEvent e) { + e.getPresentation().setEnabled(true); + e.getPresentation().setVisible(true); + + final DataContext context = e.getDataContext(); + final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(context); + if (!(psiFile instanceof PyFile)) { + e.getPresentation().setEnabled(false); + e.getPresentation().setVisible(false); + } + final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(context); + final Project project = CommonDataKeys.PROJECT.getData(context); + if (virtualFile == null || project == null) { + e.getPresentation().setEnabled(false); + } + } + + public static void convertFromPythonScript(VirtualFile virtualFile, Project project) { + Module module = ProjectFileIndex.SERVICE.getInstance(project).getModuleForFile(virtualFile); + if (module == null) return; + + final Sdk sdk = PythonSdkType.findPythonSdk(module); + if (sdk == null) { + return; + } + final String homePath = sdk.getHomePath(); + if (homePath == null) { + return; + } + + PySdkUtil.getProcessOutput(virtualFile.getParent().getPath(), new String[]{homePath, + PythonHelpersLocator.getHelperPath("py2ipnb_converter.py"), virtualFile.getPath()}); + VfsUtil.markDirtyAndRefresh(true, false, true, virtualFile.getParent()); + } +}