PY-14740 Convert an .ipynb file to and from a standard .py file

added action convert *.py file to ipnb to the tools menu
This commit is contained in:
Ekaterina Tuzova
2017-05-11 21:05:35 +03:00
parent a510a8168a
commit 6b29525825
3 changed files with 106 additions and 0 deletions
+32
View File
@@ -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)
@@ -49,6 +49,11 @@
<action class="org.jetbrains.plugins.ipnb.editor.actions.IpnbConvertToPythonAction" id="IpnbConvertToPythonAction"
text="Convert to Python Script"/>
<action class="org.jetbrains.plugins.ipnb.editor.actions.IpnbConvertFromPythonAction" id="IpnbConvertFromPythonAction"
text="Convert to Jupyter Notebook">
<add-to-group group-id="ToolsMenu" anchor="last"/>
</action>
<action class="org.jetbrains.plugins.ipnb.editor.actions.IpnbMoveCellDownAction" id="IpnbMoveCellDownAction" text="Move Cell Down">
<keyboard-shortcut keymap="$default" first-keystroke="ctrl shift DOWN"/>
</action>
@@ -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());
}
}