简体   繁体   English

如何从打开的Jupyter笔记本中以编程方式打开现有的Jupyter笔记本

[英]How can I open an existing Jupyter notebook programmatically from inside an open Jupyter notebook

My scenario is this... 我的情况是这样的...

From within a Jupyter Notebook (NB) I want to open another NB, so I locate my current working directory by typing: 我想从Jupyter笔记本(NB)中打开另一个NB,因此我可以通过键入以下内容找到当前的工作目录:

import os
os.getcwd()

So I get a listing: 所以我得到一个清单:

Volume in drive C is OSDisk Volume Serial Number is 8AD4-7C2E 驱动器C中的卷为OSDisk卷序列号为8AD4-7C2E

Directory of C:\\Users\\rlysak01\\Desktop\\02-PyCoreBootcamp\\03-Object-and-Data Structure Basics\\Core_Language_Explanations C:\\ Users \\ rlysak01 \\ Desktop \\ 02-PyCoreBootcamp \\ 03-Object-and-Data Structure Basics \\ Core_Language_Explanations目录

03/04/2019 05:32 PM testfolder 02/23/2019 03:22 PM 66,763 Untitled.ipynb 4 File(s) 117,932 bytes 4 Dir(s) 1,962,647,552 bytes free 03/04/2019 05:32 PM testfolder 02/02/23下午03:22 66,763 Untitled.ipynb 4个文件117,932字节4个目录1,962,647,552字节可用

Now I want to open the NB named "untitled.ipynb" to see what's in it. 现在,我想打开名为“ untitled.ipynb”的NB来查看其中的内容。

A Google search only finds ways to launch a new server and open a specified NB from within that new server process. Google搜索只能找到启动新服务器并从该新服务器进程中打开指定的NB的方法。

Is there a way to open that notebook without starting a new Jupyter server on my local machine? 有没有一种方法可以打开该笔记本,而无需在本地计算机上启动新的Jupyter服务器?

When I try 当我尝试

nb_to_open = os.getcwd() + '\\untitled.ipynb'
open(nb_to_open)

I get the following response, but the notebook does not open: 我收到以下响应,但是笔记本无法打开:

<_io.TextIOWrapper name='C:\\Users\\rlysak01\\Desktop\\02-PyCoreBootcamp\\untitled.ipynb' mode='r' encoding='cp1252'>

What I really want is to put that file information into the current Notebook server process and open it in a new browser window. 我真正想要的是将文件信息放入当前的Notebook服务器进程中,然后在新的浏览器窗口中打开它。

I figured out that I can MANUALLY copy the URL from the current notebook's browser address field and add the NB filename to the URL, but this involves a tedious manual step. 我发现我可以从当前笔记本的浏览器地址字段中手动复制URL并将NB文件名添加到URL,但这需要一个繁琐的手动步骤。

If the current URL is this: http://localhost:8888/notebooks/Desktop/02-PyCoreBootcamp/ 如果当前URL是这样的: http:// localhost:8888 / notebooks / Desktop / 02-PyCoreBootcamp /

I can manually hack it to be this: 我可以手动修改为:

http://localhost:8888/notebooks/Desktop/02-PyCoreBootcamp/untitled.ipynb http:// localhost:8888 / notebooks / Desktop / 02-PyCoreBootcamp / untitled.ipynb

Then I convert the Jupyter code cell to a Markdown cell, execute the Markdown cell and click on the http hyperlink in the markdown cell. 然后,我将Jupyter代码单元格转换为Markdown单元格,执行Markdown单元格并单击Markdown单元格中的http超链接。

But this is a manual hack that I want to do complete via Python in the Jupyter NB cell. 但这是我想通过Jupyter NB单元中的Python完成的手动黑客。

If I can find the URL programmatically, then I could add my filename onto the end of the URL. 如果可以通过编程找到URL,则可以将文件名添加到URL的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM