0

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.

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
  • If you put `%%capture` as the first line, you should suppress the output. That cell magic suppresses better than `io.capture_output()` does, as [I've observed with seaborn output](https://stackoverflow.com/a/60269307/8508004), see in particular my Feb 19 at 21:57 comment. – Wayne Mar 13 '20 at 20:32
  • By the way normally `%%capture out` or a variation would be used to capture the output from a cell as discussed [here](https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-capture). In the case of just `%%capture`, we are using a trick to suppress the output as it will just discard the output since it is not assigned a variable ([source](https://stackoverflow.com/a/23692951/8508004)). – Wayne Mar 13 '20 at 20:44

2 Answers2

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
bashBedlam
  • 1,402
  • 1
  • 7
  • 11
  • Hello, I already tried that and it has the same effect as using the %run command. It has the negative side effect of not being suppressed by `io.capture_output()` – rocksNwaves Mar 12 '20 at 23:23
0

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.

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
  • 1
    You can replace those two lines with `%%capture` cell magic if you are just using the whole cell. Plus `%%capture` seems to suppress more output types than `io.capture_output()` does. I've seen the difference with seaborn. – Wayne Mar 13 '20 at 20:21