Sample Notebook to see how the %run iPython magic works

Note: If this will work as expected, this could be a good way to proceed with heavy executions to run in notebooks (on likely "remote" machines).

TARGET_SCRIPT = 'script2run'  # script2run.py file will be executed

In [ ]:
%run script2run

Checking Namespace

See whether the namespace of the iPython notebook has been populated with variables declared in the script


In [ ]:
print(dummy_global.name)

In [ ]:
print(dummy_main.name)

Now test Heavyweight computations

Now try to execute a script whose execution takes some time, in order to see if the control comes back to the notebook after the %run magic


In [ ]:
%run -t script2run --mode=heavy

In [ ]:
print(dummy_main.name)

Testing the iPythonPexpect extension


In [ ]:
import os

In [ ]:
install_ext = False
if not os.path.exists('/Users/valerio/.ipython/extensions/ipythonPexpect.py'):
    print('ipythonPexpect must be installed')
else:
    print('Extension already installed')

In [ ]:
%install_ext https://cdcvs.fnal.gov/redmine/projects/ipython_ext/repository/revisions/master/raw/ipythonPexpect.py

In [ ]:
%load_ext ipythonPexpect

In [ ]:
%pexpect_spawn_python

In [ ]:
N = 10**7

In [ ]:
s = sum([i**3 for i in range(N)])

In [ ]:
%pexpect_close

Python multiprocessing: Pool.apply_async


In [ ]:
import multiprocessing as mp

In [ ]:
print(mp.cpu_count())

In [ ]:
from script2run import main_heavy

In [ ]:
pool = mp.Pool(processes=mp.cpu_count())

In [ ]:
r = pool.apply_async(main_heavy)

In [ ]:
output = r.get()
print(output)

Trying get the name of current notebook


In [ ]:
from ipykernel import get_connection_info
import json

info = json.loads(get_connection_info())
kernel_id = info['key']

info

In [ ]:
kernel_id

In [ ]:
import os
os.getcwd()

In [ ]:
from notebook.notebookapp import list_running_servers

for server in list_running_servers():
    print(server)

In [ ]:
import urllib
import json
response = urllib.request.urlopen('http://127.0.0.1:8188/api/sessions')
encoding = response.read().decode('utf-8')
sessions = json.loads(encoding)
for sess in sessions:
    print(sess)

In [ ]:
from ipykernel.kernelapp import IPKernelApp

In [ ]:
kernel_app = IPKernelApp.instance()

In [ ]:
kernel_app.connection_file

In [ ]:
kernel_app.connection_file

In [ ]:
ipy_kernel = kernel_app.kernel
ipy_kernel.execution_count

In [ ]:
ipy_kernel.shell.reset()

Testing Notebook APIs


In [ ]:
from nbformat import current_nbformat

current_nbformat

In [ ]:
from nbformat import v4 as nbf
from nbformat import read as nbf_read

In [ ]:
from nbformat import NO_CONVERT

In [ ]:
from nbformat import current_nbformat

In [ ]:
current_nbformat

In [ ]:
from nbformat import versions

In [ ]:
versions

In [ ]:


In [ ]:
nb = nbf_read('iPython Run Magic Test.ipynb', as_version=4)

In [ ]:
code_cells = list(filter(lambda c: c['cell_type'] == 'code', nb.cells))
code_cells

In [ ]:
new_output_cell = nbf.new_output('stream', text='Output Text!')
new_code_cell = nbf.new_code_cell(source="print('Output Text!)")

In [ ]:
last_cell = code_cells[-1]

In [ ]:
last_cell

In [ ]:
last_cell.source = "print('Output Text!)"
last_cell.outputs.append(new_output_cell)
last_cell.metadata['collapsed'] = False

In [ ]:
from notebook.notebookapp import NotebookApp

In [ ]:
import notebook

In [ ]:
notebook.services.kernels.kernelmanager.getcwd()

In [ ]: