How to pretty print dictionaries in iPython
<p>I'm currently using RethinkDB, which has a nice web UI with a Data Explorer which allows the user to print out the contents of the database like this:</p>

<p><a href="https://i.stack.imgur.com/bKKgU.png" rel="nofollow"><img src="https://i.stack.imgur.com/bKKgU.png" alt="enter image description here"></a></p>

<p>Note that each key-value pair starts on a new line, and the keys and values (mostly) have different colors. By contrast, if I print out the same using iPython, I get an almost illegible result:</p>

<p><a href="https://i.stack.imgur.com/4jLcS.png" rel="nofollow"><img src="https://i.stack.imgur.com/4jLcS.png" alt="enter image description here"></a></p>

<p>This is slightly ameliorated if I iterate over the cursor and <code>print</code> each item, like so:</p>

<p><a href="https://i.stack.imgur.com/VegvQ.png" rel="nofollow"><img src="https://i.stack.imgur.com/VegvQ.png" alt="enter image description here"></a></p>

<p>However, this requires more typing and still doesn't look as good as the RethinkDB web UI. Is there perhaps an iPython plugin that I can install to improve the appearance of the printed output?</p>

<p>(I had a look at <a href="https://docs.python.org/2/library/pprint.html" rel="nofollow">pprint</a>, but this seems to control only the positioning of the text and not its color).</p>

<p>You could use <a href="https://docs.python.org/3/library/json.html#json.dumps" rel="nofollow"><code>json.dumps()</code></a>:</p>

<pre><code>import json 

for row in r.db(....).run(conn):
    print(json.dumps(row, indent=4))
</code></pre>

<p>Although this does not display the keys in sorted order, as appears to be the case in the example, it might be sufficient for your needs. As pointed out by @coder, you <code>json.dumps()</code> can sort the keys by specifying the <code>sort_keys=True</code> parameter.</p>

<pre><code>for row in r.db(....).run(conn):
    print(json.dumps(row, indent=4, sort_keys=True))
</code></pre>

<p>It might also be possible to print the object directly (haven't tested this):</p>

<pre><code>print(json.dumps(r.db(....).run(conn), indent=4, sort_keys=True)
</code></pre>

<p>which might also print out the surrounding "list" object.</p>

<hr>

<p>To handle objects that do not support serialisation to JSON you can use a custom <a href="https://docs.python.org/3/library/json.html#json.JSONEncoder" rel="nofollow"><code>JSONEncoder</code></a>. Here is an example which handles <code>datetime.datetime</code> objects:</p>

<pre><code>from datetime import datetime

class DateTimeAwareJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            tz = obj.tzname()
            return obj.ctime() + (' {}'.format(tz) if tz else '')
        return super(DateTimeAwareJSONEncoder, self).default(obj)

for row in r.db(....).run(conn):
    print(json.dumps(row, indent=4, sort_keys=True, cls=DateTimeAwareJSONEncoder))
</code></pre>

<p>You can use <a href="https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior" rel="nofollow"><code>datetime.strftime()</code></a> to format the date time string as required.</p>

python rethinkdb