从第二个Jupyter笔记本开始启动 [英] Starting second Jupyter notebook where first left off

查看:70
本文介绍了从第二个Jupyter笔记本开始启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

我开始使用Jupyter Lab自学一些新的库.我知道严格禁止在SO上显示情感,并且可以对其进行编辑,但是WOW,Jupyter笔记本很酷!

I started teaching myself a few new libraries using Jupyter Lab. I know showing emotion on SO is strictly forbidden and this will get edited, but WOW, Jupyter notebooks are cool!

无论如何,我在研究代码示例时会在markdown中做笔记.它使我有了在学习时编写自己的小教科书的想法.

Anyway, I'm taking notes in markdown as I work through code examples. It gave me the idea of writing my own little textbook as I learn.

例如,在笔记本1中,我谈论(自学)线性回归.它会记录词汇,显示一些数学公式,然后通过一些代码示例进行工作.结束部分.

For example, in notebook 1, I talk about (teach myself) linear regression. It take notes on vocabulary, show some mathy formulas then work through some code examples. End section.

在笔记本2中,我开始了有关不同指标的对话,以展示回归模型的有效性.然后,我想执行一些代码来计算这些指标...但是回归模型的所有代码都在最后一个笔记本中,我无法访问它.

In notebook 2, I start the conversation about different metrics to show how effective the regression model was. Then I want to execute some code to calculate those metrics... but all the code for the regression model is in the last notebook and I can't access it.

问题:

有没有办法将这两个笔记本链接在一起,这样我就不必从第一个笔记本重新编写代码了?

Is there a way to link these two notebooks together so that I don't have to re-write the code from the first one?

我的尝试:

似乎最想做的就是使用

%run notebook_01.ipynb

但是,这将引发错误.请注意,它似乎是在搜索要运行的.py文件:

However, this throws an error. Note that it appears to search for a .py file to run:

ERROR:root:File 'linear_regression01.ipynb.py' not found.

我发现一些问题/答案似乎对其他用户有效,但不适用于我.

I have found some questions/answers where this appears to work for other users, but it is not for me.

我得到了神奇的命令%run可以工作,但是它运行并且将整个第一个笔记本打印到第二个笔记本中.我很好地知道如何做到这一点,它确实达到了无需重新编码的目标,但是它绝对重新打印了我不想要的所有内容.

I got the magic command %run to work, however it runs AND prints the entire first notebook into the second. I'ts good to know how to do this and it does achieve the goal of not having to re-code, but it re-prints absolutely everything, which I do not want.

推荐答案

好吧,我通过抑制输出找到了答案:

Ok, I found the answer by way of suppressing outputs:

只需将其放在第二个笔记本的顶部:

Just put this at the top of your second notebook:

from IPython.utils import io
with io.capture_output() as captured:
    %run your_linked_notebook.ipynb 

这将导致您要链接的笔记本运行,从而允许您使用其中的任何数据,而不必查看其所有输出和可视化效果.

This will cause the notebook you want to link to run, allowing you to use any of the data from it, but without having to see all of the outputs and visualizations from it.

如果您要处理大量数据或链接许多执行昂贵计算的笔记本,那么这可能不是一个好方法,但对于大多数人而言,这样做可能会成功.

This is probably not a great way to go if you are working with a lot of data or linking a lot of notebooks that perform expensive computations, but will likely do the trick for most people.

如果有不涉及运行链接的笔记本的答案,我将很高兴看到它.

If there is an answer that does not involve running the notebook linked, I'd be excited to see it.

这篇关于从第二个Jupyter笔记本开始启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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