无法将 .py 文件导入 ipython 笔记本 [英] Cannot import .py file to ipython notebook

查看:18
本文介绍了无法将 .py 文件导入 ipython 笔记本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前为我无法让它工作"的问题道歉:我应该如何将 .py 文件加载到 ipython notebook 中?我想将 python 代码转换为笔记本(首先是简单的脚本,然后是包含 nbconvert 指令的脚本 嵌入为注释-- 请参阅链接文件的底部.)

With apologies in advance for the "I can't get it to work" question: How should I load a .py file into ipython notebook? I want to convert python code to notebooks (first simple scripts and later scripts that include nbconvert directives embedded as comments-- see bottom of the linked file.)

也许我做错了,但也许我的设置有问题.当我将 .py 文件拖到 Notebook 的文件列表时,我收到消息

Perhaps I'm doing it wrong, but perhaps there's something wrong with my set-up. When I drag a .py file to the Notebook's file list, I get the message

文件类型无效:上传的笔记本必须是 .ipynb 文件.

Invalid file type: Uploaded notebooks must be .ipynb files.

我什至尝试将扩展名更改为 .ipynb(保持 python 脚本不变);很合理,我得到了一个错误:

I even tried changing the extension to .ipynb (keeping the python script unmodified); reasonably enough, I got an error:

加载笔记本时出错:请求错误

Error loading notebook: Bad request

知道出了什么问题吗?

系统信息:我使用的是 OS X(10.8,Mountain Lion),使用 Firefox 28.0 和 Anaconda 1.9.2 (x86_64),它提供 python 2.7.6 和 ipython 2.0.Anaconda 不在默认路径上;我将它添加到 bash 会话中,然后从中启动带有 ipython notebook 的笔记本,并且我能够在浏览器中正常打开和编辑 .ipynb 文件.

System information: I'm on OS X (10.8, Mountain Lion), using Firefox 28.0 and Anaconda 1.9.2 (x86_64), which supplies python 2.7.6 and ipython 2.0. Anaconda is not on the default PATH; I add it in a bash session from which I then launch notebook with ipython notebook, and I'm able to open and edit .ipynb files normally in the browser.

但我确实有一些奇怪的行为:当从笔记本导出作为 .py 文件时,我没有记录控制注释 这里 但格式更简单,没有版本号:

But I do get some curious behavior: When exporting from notebook as a .py file, I don't get the control comments documented here but a simpler format, without version number:

# coding: utf-8

# In[ ]:

print "This is a slide"

## Top-level title 

### Second-level heading 

#### Third-level heading

# This is some `markdown` text. 
# 
# And some more here.

知道这里发生了什么吗?

Any idea what's going on here?

同样的格式由ipython nbconvert生成.但是,如果我使用 ipython notebook --script(每次保存时将笔记本导出为 python 脚本)启动笔记本服务器,结果将包含 nbconvert 指令我们需要转换回笔记本!

The same format is generated by ipython nbconvert. However, if I start the notebook server with ipython notebook --script (which exports the notebook as a python script every time it is saved), the result contains the nbconvert directives we need to convert back to a notebook!

推荐答案

我不确定为什么 notebook 本身不支持此功能,但我得出的结论是:无法通过命令完成行或笔记本 GUI.

I'm not sure why notebook doesn't support this natively, but I've concluded that the answer is: It can't be done from the command line or notebook GUI.

<markdowncell>这样的控制注释只能通过python访问notebook的API来解释,如我的相关问题的答案.

Control comments like <markdowncell> can only be interpreted by accessing notebook's API through python, as shown by @CliffordVienna in this answer to my related question.

import IPython.nbformat.current as nbf
nb = nbf.read(open('test.py', 'r'), 'py')
nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')

上述方法不适用于 Notebook API 的当前版本 (v4),因此我添加了 这个自我回答来展示它是如何完成的.

The above method does not work with the current version (v4) of the Notebook API, so I have added this self-answer to show how it's done.

这篇关于无法将 .py 文件导入 ipython 笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆