Commit e64aa9c6 authored by Jens Henrik Goebbert's avatar Jens Henrik Goebbert
Browse files

clear output

parent 9c2ba215
%% Cell type:markdown id: tags:
# Simple Animations Using clear_output
%% Cell type:markdown id: tags:
Sometimes you want to clear the output area in the middle of a calculation. This can be useful for doing simple animations. In terminals, there is the carriage-return (`'\r'`) for overwriting a single line, but the notebook frontend can clear the whole output area, not just a single line.
To clear output in the Notebook you can use the `clear_output()` function. If you are clearing the output every frame of an animation, calling `clear_output()` will create noticeable flickering. You can use `clear_output(wait=True)` to add the *clear_output* call to a queue. When data becomes available to replace the existing output, the *clear_output* will be called immediately before the new data is added. This avoids the flickering by not rendering the cleared output to the screen.
%% Cell type:markdown id: tags:
## Simple example
%% Cell type:markdown id: tags:
Here we show our progress iterating through a list:
%% Cell type:code id: tags:
``` python
import sys
import time
```
%% Cell type:code id: tags:
``` python
from IPython.display import display, clear_output
for i in range(10):
time.sleep(0.25)
clear_output(wait=True)
print(i)
sys.stdout.flush()
```
%% Output
9
%% Cell type:markdown id: tags:
## AsyncResult.wait_interactive
%% Cell type:markdown id: tags:
The AsyncResult object has a special `wait_interactive()` method, which prints its progress interactively,
so you can watch as your parallel computation completes.
**This example assumes you have an IPython cluster running, which you can start from the [cluster panel](/#clusters)**
%% Cell type:code id: tags:
``` python
#from IPython import parallel
#rc = parallel.Client()
#view = rc.load_balanced_view()
#
#amr = view.map_async(time.sleep, [0.5]*100)
#
#amr.wait_interactive()
```
%% Cell type:markdown id: tags:
## Matplotlib example
%% Cell type:markdown id: tags:
You can also use `clear_output()` to clear figures and plots.
%% Cell type:code id: tags:
``` python
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
```
%% Cell type:code id: tags:
``` python
from scipy.special import jn
x = np.linspace(0,5)
f, ax = plt.subplots()
ax.set_title("Bessel functions")
for n in range(1,10):
time.sleep(1)
ax.plot(x, jn(x,n))
clear_output(wait=True)
display(f)
# close the figure at the end, so we don't get a duplicate
# of the last plot
plt.close()
```
%% Output
%% Cell type:code id: tags:
``` python
```
......
%% Cell type:markdown id: tags:
# Simple interactive bacgkround jobs with IPython
We start by loading the `backgroundjobs` library and defining a few trivial functions to illustrate things with.
%% Cell type:code id: tags:
``` python
from IPython.lib import backgroundjobs as bg
import sys
import time
def sleepfunc(interval=2, *a, **kw):
args = dict(interval=interval,
args=a,
kwargs=kw)
time.sleep(interval)
return args
def diefunc(interval=2, *a, **kw):
time.sleep(interval)
raise Exception("Dead job with interval %s" % interval)
def printfunc(interval=1, reps=5):
for n in range(reps):
time.sleep(interval)
print('In the background... %i' % n)
sys.stdout.flush()
print('All done!')
sys.stdout.flush()
```
%% Cell type:markdown id: tags:
Now, we can create a job manager (called simply `jobs`) and use it to submit new jobs.
Run the cell below, it will show when the jobs start. Wait a few seconds until you see the 'all done' completion message:
%% Cell type:code id: tags:
``` python
jobs = bg.BackgroundJobManager()
# Start a few jobs, the first one will have ID # 0
jobs.new(sleepfunc, 4)
jobs.new(sleepfunc, kw={'reps':2})
jobs.new('printfunc(1,3)')
```
%% Output
Starting job # 0 in a separate thread.
Starting job # 2 in a separate thread.
Starting job # 3 in a separate thread.
<BackgroundJob #3: printfunc(1,3)>
In the background... 0
In the background... 1
In the background... 2
All done!
%% Cell type:markdown id: tags:
You can check the status of your jobs at any time:
%% Cell type:code id: tags:
``` python
jobs.status()
```
%% Output
Completed jobs:
0 : <function sleepfunc at 0x10521f2f0>
2 : <function sleepfunc at 0x10521f2f0>
3 : printfunc(1,3)
%% Cell type:markdown id: tags:
For any completed job, you can get its result easily:
%% Cell type:code id: tags:
``` python
jobs[0].result
```
%% Output
{'args': (), 'interval': 4, 'kwargs': {}}
%% Cell type:markdown id: tags:
## Errors and tracebacks
%% Cell type:markdown id: tags:
The jobs manager tries to help you with debugging:
%% Cell type:code id: tags:
``` python
# This makes a couple of jobs which will die. Let's keep a reference to
# them for easier traceback reporting later
diejob1 = jobs.new(diefunc, 1)
diejob2 = jobs.new(diefunc, 2)
```
%% Output
Starting job # 4 in a separate thread.
Starting job # 5 in a separate thread.
%% Cell type:markdown id: tags:
You can get the traceback of any dead job. Run the line
below again interactively until it prints a traceback (check the status
of the job):
%% Cell type:code id: tags:
``` python
print("Status of diejob1: %s" % diejob1.status)
diejob1.traceback() # jobs.traceback(4) would also work here, with the job number
```
%% Output
Status of diejob1: Dead (Exception), call jobs.traceback() for details
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
/Users/minrk/dev/ip/mine/IPython/lib/backgroundjobs.py in call(self)
 489 
 490 def call(self):
--> 491 return self.func(*self.args, **self.kwargs)

<ipython-input-1-169e49434ce0> in diefunc(interval, *a, **kw)
 13 def diefunc(interval=2, *a, **kw):
 14 time.sleep(interval)
---> 15 raise Exception("Dead job with interval %s" % interval)
 16 
 17 def printfunc(interval=1, reps=5):
Exception: Dead job with interval 1
%% Cell type:markdown id: tags:
This will print all tracebacks for all dead jobs:
%% Cell type:code id: tags:
``` python
jobs.traceback()
```
%% Output
Traceback for: <BackgroundJob #4: <function diefunc at 0x10521f7b8>>
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
/Users/minrk/dev/ip/mine/IPython/lib/backgroundjobs.py in call(self)
 489 
 490 def call(self):
--> 491 return self.func(*self.args, **self.kwargs)

<ipython-input-1-169e49434ce0> in diefunc(interval, *a, **kw)
 13 def diefunc(interval=2, *a, **kw):
 14 time.sleep(interval)
---> 15 raise Exception("Dead job with interval %s" % interval)
 16 
 17 def printfunc(interval=1, reps=5):
Exception: Dead job with interval 1
Traceback for: <BackgroundJob #5: <function diefunc at 0x10521f7b8>>
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
/Users/minrk/dev/ip/mine/IPython/lib/backgroundjobs.py in call(self)
 489 
 490 def call(self):
--> 491 return self.func(*self.args, **self.kwargs)

<ipython-input-1-169e49434ce0> in diefunc(interval, *a, **kw)
 13 def diefunc(interval=2, *a, **kw):
 14 time.sleep(interval)
---> 15 raise Exception("Dead job with interval %s" % interval)
 16 
 17 def printfunc(interval=1, reps=5):
Exception: Dead job with interval 2