skip to navigation
skip to content

daff 1.1.6

Diff and patch tables

Latest Version: 1.3.7

[![Build Status](](
[![NPM version](](
[![Gem Version](](
[![PyPI version](](

daff: data diff

This is a library for comparing tables, producing a summary of their
differences, and using such a summary as a patch file. It is
optimized for comparing tables that share a common origin, in other
words multiple versions of the "same" table.

For a live demo, see:

Download the code for your preferred language here:

For certain languages you can use the command-line:
npm install daff -g # node/javascript
pip3 install daff # python3
gem install daff # ruby

Or use the library to view csv diffs on github via a chrome extension:

The diff format used by `daff` is specified here:

This library is a stripped down version of the coopy toolbox (see To compare tables from different origins,
or with automatically generated IDs, or other complications, check out
the coopy toolbox.

The program

You can run `daff`/``/`daff.rb` as a utility program:
$ daff
daff can produce and apply tabular diffs.
Call as:
daff [--output OUTPUT.csv] a.csv b.csv
daff [--output OUTPUT.csv] parent.csv a.csv b.csv
daff [--output OUTPUT.jsonbook] a.jsonbook b.jsonbook
daff patch [--output OUTPUT.csv] source.csv patch.csv
daff trim [--output OUTPUT.csv] source.csv
daff render [--output OUTPUT.html] diff.csv

If you need more control, here is the full list of flags:
daff diff [--output OUTPUT.csv] [--context NUM] [--all] [--act ACT] a.csv b.csv
--context NUM: show NUM rows of context
--all: do not prune unchanged rows
--act ACT: show only a certain kind of change (update, insert, delete)

daff render [--output OUTPUT.html] [--css CSS.css] [--fragment] [--plain] diff.csv
--css CSS.css: generate a suitable css file to go with the html
--fragment: generate just a html fragment rather than a page
--plain: do not use fancy utf8 characters to make arrows prettier

Using with git

Run `daff git csv` to see how to use daff to improve `git`'s handling
of csv files.

$ daff git csv
You can use daff to improve git's handling of csv files, by using it as a
diff driver (for showing what has changed) and as a merge driver (for merging
changes between multiple versions). Here is how.

Create and add a file called .gitattributes in the root directory of your
repository, containing:

*.csv diff=daff-diff
*.csv merge=daff-merge

Create a file called .gitconfig in your home directory (or alternatively
open .git/config for a particular repository) and add:

[merge "daff-merge"]
name = daff tabular merge
driver = daff merge --output %A %O %A %B

[diff "daff-diff"]
command = daff diff --git

Make sure you can run daff from the command-line as just "daff" - if not,
replace "daff" in the driver and command lines above with the correct way
to call it.

The library

You can use `daff` as a library from any supported language. We take
here the example of Javascript. To use `daff` on a webpage,
first include `daff.js`:
<script src="daff.js"></script>
Or if using node outside the browser:
var daff = require('daff');

For concreteness, assume we have two versions of a table,
`data1` and `data2`:
var data1 = [
var data2 = [

To make those tables accessible to the library, we wrap them
in `daff.TableView`:
var table1 = new daff.TableView(data1);
var table2 = new daff.TableView(data2);

We can now compute the alignment between the rows and columns
in the two tables:
var alignment = daff.compareTables(table1,table2).align();

To produce a diff from the alignment, we first need a table
for the output:
var data_diff = [];
var table_diff = new daff.TableView(data_diff);

Using default options for the diff:
var flags = new daff.CompareFlags();
var highlighter = new daff.TableDiff(alignment,flags);

The diff is now in `data_diff` in highlighter format, see
specification here:

[ [ '!', '', '+++', '' ],
[ '@@', 'Country', 'Code', 'Capital' ],
[ '+', 'Ireland', 'ie', 'Dublin' ],
[ '+', 'France', 'fr', 'Paris' ],
[ '->', 'Spain', 'es', 'Barcelona->Madrid' ],
[ '+++', 'Germany', 'de', 'Berlin' ] ]

For visualization, you may want to convert this to a HTML table
with appropriate classes on cells so you can color-code inserts,
deletes, updates, etc. You can do this with:
var diff2html = new daff.DiffRender();
var table_diff_html = diff2html.html();

For 3-way differences (that is, comparing two tables given knowledge
of a common ancestor) use `daff.compareTables3` (give ancestor
table as the first argument).

Here is how to apply that difference as a patch:
var patcher = new daff.HighlightPatch(table1,table_diff);
// table1 should now equal table2

For other languages, you should find sample code in
the packages on the [Releases]( page.

Supported languages

The `daff` library is written in [Haxe]( which
can be translated reasonably well into at least the following languages:

* Javascript
* Python
* Java
* C#
* C++
* (via a hack, just for `daff`) Ruby

Some translations are done for you on the
[Releases]( page.
To make another translation,
follow the
[Haxe getting started tutorial]( for the
language you care about, then do one of:

make js
make php
make py
make java
make cs
make cpp

[@Floppy]( has made a lovingly-hand-written [native Ruby port]( that covers core functionality. I've made a brutally-machine-converted port that is a full translation but less idiomatic.

For each language, the `daff` library expects to be handed an interface to tables you create, rather than creating them
itself. This is to avoid inefficient copies from one format to another. You'll find a `SimpleTable` class you can use if
you find this awkward.

Reading material

* : a specification of the diff format we use.
* : using this library with github.
* : using this library with gitlab.
* : a summary of where the library came from.
* : a post about storing small data in git/github.
* : counterpoint - a post discussing tracked-changes rather than diffs.

## License

daff is distributed under the MIT License.  
File Type Py Version Uploaded on Size
daff-1.1.6.tar.gz (md5) Source 2014-07-11 86KB
  • Downloads (All Versions):
  • 106 downloads in the last day
  • 544 downloads in the last week
  • 2986 downloads in the last month