Skip to content

Instantly share code, notes, and snippets.

takluyver /
Last active Jun 20, 2020
Run 2to3 on IPython notebooks
#!/usr/bin/env python3
To run: python3 notebook-or-directory
# Authors: Thomas Kluyver, Fernando Perez
# See:
import argparse
import pathlib
from nbformat import read, write
takluyver /
Created Jul 21, 2016
Make a notebook from a script
"""Create a notebook containing code from a script.
Run as: python
import sys
import nbformat
from nbformat.v4 import new_notebook, new_code_cell
nb = new_notebook()
takluyver /
Created Jan 25, 2016
Cell magic for 'await' expressions in IPython
# (c) Thomas Kluyver, 2016
# Use it under the MIT license
# This is fairly experimental. Use at your own risk.
import ast
from ast import Call, Attribute, Name, Load
import asyncio as asyncio_mod
#import astpp
from IPython.utils.text import indent
takluyver /
Created Sep 6, 2014
Flatten notebooks for git diff

Copy to somewhere on $PATH. Then, in the root of a git repository, run these commands:

echo "*.ipynb diff=ipynb" >> .gitattributes 
git config diff.ipynb.textconv

When you change a notebook and run git diff, you'll see the diff of flattened, simplified notebooks, rather than the full JSON. This does lose some information (metadata, non-text output), but it makes it easier to see simple changes in the notebook.

This doesn't help with merging conflicting changes in notebooks. For that, see

Riverbank GPL Exception version 1.1
Additional rights granted beyond the GPL (the "Exception").
As a special exception to the terms and conditions of version 2.0 of the GPL,
Riverbank hereby grants you the rights described below, provided you agree to
the terms and conditions in this Exception, including its obligations and
restrictions on use.
takluyver /
Created Aug 27, 2019
Karabo data cache format experimentation
import json
import h5py
import numpy as np
def read_json(path):
with open(path) as f:
loaded_data = json.load(f)
res = {}
for info in loaded_data:
takluyver / VDS issue.ipynb
Created Apr 9, 2019
HDF5 virtual dataset access issue
View VDS issue.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
takluyver / example
Created May 13, 2016
Debug on interrupt
View example
$ python3
> /home/takluyver/scratch/>None
-> pdb.set_trace()
(Pdb) p a
class TableCell(object):
bg_colour = None
def __init__(self, value, header=False, bg_colour=None, text_colour=None):
self.value = value
self.header = header
self.bg_colour = bg_colour
self.text_colour = text_colour
def _make_css(self):
You can’t perform that action at this time.