Starting second Jupyter notebook where first left off

强颜欢笑 提交于 2020-03-25 16:05:47

问题


Context:

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!

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.

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.

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.

Question:

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?

My attempt:

It seems like the closest thing to what I want to do is to use

%run notebook_01.ipynb

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.

Edit: 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.


回答1:


If you run this from the command line :

jupyter nbconvert --to script first_notebook.iynb

It will create a python file from your first notebook called 'first_notebook.py'. After that you can import from that file into your second notebook with:

import first_notebook



回答2:


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.



来源:https://stackoverflow.com/questions/60662583/starting-second-jupyter-notebook-where-first-left-off

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!