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
Loading items

Target

Select target project
  • jupyter4jsc/j4j_notebooks
  • kreuzer1/j4j_notebooks
  • goebbert1/j4j_notebooks
3 results
Select Git revision
Loading items
Show changes
Commits on Source (163)
Showing
with 129 additions and 15662 deletions
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
\ No newline at end of file
%% 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
# Code of the IPython Cookbook, Second Edition (2018)
[![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/ipython-books/cookbook-2nd-code/master)
This repository contains the Jupyter notebooks of the 100+ recipes of [*IPython Interactive Computing and Visualization Cookbook, Second Edition (2018)*](https://www.packtpub.com/big-data-and-business-intelligence/ipython-interactive-computing-and-visualization-cookbook-second-e), by [Cyrille Rossant](http://cyrille.rossant.net), *Packt Publishing*.
This repository is read-only: the source files are on the [cookbook-2nd](https://github.com/ipython-books/cookbook-2nd) repository.
## Running the code in the cloud
With [Binder](https://mybinder.org/), you can run most of the Jupyter notebooks directly from your web browser without installing anything. Just click on the `launch binder` button above. A temporary Jupyter Notebook server with all dependencies will be automatically launched in the cloud. It is not persistent: all your changes will be lost after some time.
## Running the code on your computer
1. [**Install** git](https://git-scm.com/downloads).
2. [**Download and install** Anaconda](https://www.anaconda.com/download/): choose the **Python 3.6, 64-bit** version for your operating system (macOS, Linux, or Windows).
3. **Open** a terminal (`cmd` on Windows).
4. **Clone** the repository:
```bash
$ git clone https://github.com/ipython-books/cookbook-2nd-code.git
$ cd cookbook-2nd-code
```
5. **Create** the `cookbook` [conda environment](https://conda.io/docs/user-guide/tasks/manage-environments.html#creating-an-environment-from-an-environment-yml-file):
```bash
conda env create -f environment.yml
```
6. **Activate** the environment:
* On macOS and Linux:
```bash
source activate cookbook
```
* On Windows:
```bash
activate cookbook
```
7. **Launch** the [Jupyter Notebook](http://jupyter.org/install.html):
```bash
$ jupyter notebook
```