1

I want to run another b.ipynb file from the a.ipynb file.

When I run a specific code in a.ipynb and run the b.ipynb file, the results are saved in the script when I run b.ipynb in Jupyer Notebook.

for example :

a.ipynb

run b.ipynb ..?

b.ipybn

print("5")

Then run the a.ipynb script and open b.ipynb.

I want to see in b.ipynb

print("5")
>> 5

What code should I write in the a.ipynb file?

primvice
  • 21
  • 3

2 Answers2

0

You can simply use the magic function %run

Usage:

%run [-n -i -e -G]
     [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
     ( -m mod | file ) [args]

Parameters after the filename are passed as command-line arguments to the program (put in sys.argv). Then, control returns to IPython’s prompt.

This is similar to running at a system prompt python file args, but with the advantage of giving you IPython’s tracebacks, and of loading all variables into your interactive namespace for further use (unless -p is used).

The file is executed in a namespace initially consisting only of name=='main' and sys.argv constructed as indicated. It thus sees its environment as if it were being run as a stand-alone program (except for sharing global objects such as previously imported modules). But after execution, the IPython interactive namespace gets updated with all variables defined in the program (except for name and sys.argv).

Rishit Dagli
  • 1,000
  • 8
  • 20
  • Oh, as you say, I could use "name == 'main' and sys.argv" to achieve the desired result. One problem, however, is that if I place run inside a function, it won't work. "run auto-run.ipynb"             ^ SyntaxError: invalid syntax – primvice Jan 29 '20 at 08:58
0

example of this will be something like import a function from another .ipynb file

You have to pip install ipynb first

first.ipynb

def print():
   print('Stack Overflow')

second.ipynb

from ipynb.fs.full.first import print
print()