[IPython-dev] How to display markdown text in DataFrame in Notebook

Patrick Surry patrick.surry at gmail.com
Sat May 30 20:40:36 EDT 2015


Another simple option is to use Pandas to_html() with the formatters option
to specify column-specific formatting and generate custom HTML, which could
easily start as markdown.

Here's an example where one column has raw markdown, and another where it
builds markdown => HTML on the fly.

Cheers,
Patrick


from markdown import markdown
from IPython.display import display, HTML

# Be careful that Pandas doesn't truncate your long HTML strings...
pd.set_option('display.max_colwidth', 256)

df = pd.DataFrame(
    [
        ['GOOG', 532, '[Google](http://www.google.com)'],
        ['IBM', 170, '[IBM](http://www.ibm.com)'],
        ['AAPL', 130, '[Apple](http://www.apple.com)']
    ],
    columns=['Symbol', 'Price', 'Company']
)
HTML(df.to_html(
    formatters=dict(
        Price='${:,.0f}'.format,
        Symbol=lambda s: markdown('[{sym:s}](
http://finance.yahoo.com/q?s={sym:s})'.format(sym=s)),
        Company=markdown
    ),
    escape=False,
    index=False
))


SymbolPriceCompany

GOOG <http://finance.yahoo.com/q?s=GOOG>
$532

Google <http://www.google.com/>

IBM <http://finance.yahoo.com/q?s=IBM>
$170

IBM <http://www.ibm.com/>

AAPL <http://finance.yahoo.com/q?s=AAPL>
$130

Apple <http://www.apple.com/>



> I have DataFrames with url fields.
>
> Can I easily tweak IPython Notebook so that it displays hyperlink
> markdowns? Meaning it renders clickable title?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150530/9c510a28/attachment.html>


More information about the IPython-dev mailing list