Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • documentation
  • integration
  • master
  • pre_update
4 results

Target

Select target project
  • jupyter4jsc/j4j_notebooks
  • kreuzer1/j4j_notebooks
  • goebbert1/j4j_notebooks
3 results
Select Git revision
  • integration
  • master
2 results
Show changes

Commits on Source 142

42 additional commits have been omitted to prevent performance issues.
Showing
with 0 additions and 16824 deletions
%% 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:
# Capturing Output With <tt>%%capture</tt>
%% Cell type:markdown id: tags:
IPython has a [cell magic](Cell Magics.ipynb), `%%capture`, which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.
%% Cell type:code id: tags:
``` python
from __future__ import print_function
import sys
```
%% Cell type:markdown id: tags:
By default, `%%capture` discards these streams. This is a simple way to suppress unwanted output.
%% Cell type:code id: tags:
``` python
%%capture
print('hi, stdout')
print('hi, stderr', file=sys.stderr)
```
%% Cell type:markdown id: tags:
If you specify a name, then stdout/stderr will be stored in an object in your namespace.
%% Cell type:code id: tags:
``` python
%%capture captured
print('hi, stdout')
print('hi, stderr', file=sys.stderr)
```
%% Cell type:code id: tags:
``` python
captured
```
%% Output
<IPython.utils.capture.CapturedIO at 0x1076c9310>
%% Cell type:markdown id: tags:
Calling the object writes the output to stdout/stderr as appropriate.
%% Cell type:code id: tags:
``` python
captured()
```
%% Output
hi, stdout
hi, stderr
%% Cell type:code id: tags:
``` python
captured.stdout
```
%% Output
'hi, stdout\n'
%% Cell type:code id: tags:
``` python
captured.stderr
```
%% Output
'hi, stderr\n'
%% Cell type:markdown id: tags:
`%%capture` grabs all output types, not just stdout/stderr, so you can do plots and use IPython's display system inside `%%capture`
%% Cell type:code id: tags:
``` python
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
```
%% Cell type:code id: tags:
``` python
%%capture wontshutup
print("setting up X")
x = np.linspace(0,5,1000)
print("step 2: constructing y-data")
y = np.sin(x)
print("step 3: display info about y")
plt.plot(x,y)
print("okay, I'm done now")
```
%% Cell type:code id: tags:
``` python
wontshutup()
```
%% Output
setting up X
step 2: constructing y-data
step 3: display info about y
okay, I'm done now
%% Cell type:markdown id: tags:
And you can selectively disable capturing stdout, stderr or rich display, by passing `--no-stdout`, `--no-stderr` and `--no-display`
%% Cell type:code id: tags:
``` python
%%capture cap --no-stderr
print('hi, stdout')
print("hello, stderr", file=sys.stderr)
```
%% Output
hello, stderr
%% Cell type:code id: tags:
``` python
cap.stdout
```
%% Output
'hi, stdout\n'
%% Cell type:code id: tags:
``` python
cap.stderr
```
%% Output
''
%% Cell type:code id: tags:
``` python
cap.outputs
```
%% Output
[]
%% Cell type:markdown id: tags:
# Using `raw_input` and `%debug` in the Notebook
%% Cell type:markdown id: tags:
The Notebook has added support for `raw_input` and `%debug`, as of 1.0.
%% Cell type:code id: tags:
``` python
# Python 3 compat
import sys
if sys.version_info[0] >= 3:
raw_input = input
```
%% Cell type:code id: tags:
``` python
name = 'John' # raw_input("What is your name? ")
name
```
%% Output
What is your name? bla
'bla'
%% Cell type:code id: tags:
``` python
def div(x, y):
return x/y
# div(1,0)
```
%% Output
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-4-a5097cc0c0c5> in <module>()
2 return x/y
3
----> 4 div(1,0)
<ipython-input-4-a5097cc0c0c5> in div(x, y)
1 def div(x, y):
----> 2 return x/y
3
4 div(1,0)
ZeroDivisionError: division by zero
%% Cell type:code id: tags:
``` python
%debug
```
%% Output
> <ipython-input-4-a5097cc0c0c5>(2)div()
 1 def div(x, y):
----> 2  return x/y
 3 

ipdb> x
1
ipdb> y
0
ipdb> bt
<ipython-input-4-a5097cc0c0c5>(4)<module>()
 1 def div(x, y):
 2  return x/y
 3 
----> 4 div(1,0)

> <ipython-input-4-a5097cc0c0c5>(2)div()
 1 def div(x, y):
----> 2  return x/y
 3 
 4 div(1,0)

ipdb> exit