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><a href="http://stackoverflow.com/users/21945/mhawke">mhawke</a>'s answer works if one adds the keyword argument <code>time_format="raw"</code> to RethinkDB's <code>run()</code> command. (Otherwise, you get a <code>TypeError</code> because RethinkDB's object containing the time zone is not JSON serializable). The result looks like this:</p>

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

<p>which is much more legible. A slight drawback is that the <code>epoch_time</code> is more difficult to interpret than the original time format.</p>

python rethinkdb