{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*New in version 0.17.1*\n",
    "\n",
    "<p style=\"color: red\">*Provisional: This is a new feature and still under development. We'll be adding features and possibly making breaking changes in future releases. We'd love to hear your [feedback](https://github.com/pydata/pandas/issues).*<p style=\"color: red\">\n",
    "\n",
    "This document is written as a Jupyter Notebook, and can be viewed or downloaded [here](http://nbviewer.ipython.org/github/pydata/pandas/blob/master/doc/source/html-styling.ipynb).\n",
    "\n",
    "You can apply **conditional formatting**, the visual styling of a DataFrame\n",
    "depending on the data within, by using the ``DataFrame.style`` property.\n",
    "This is a property that returns a ``pandas.Styler`` object, which has\n",
    "useful methods for formatting and displaying DataFrames.\n",
    "\n",
    "The styling is accomplished using CSS.\n",
    "You write \"style functions\" that take scalars, `DataFrame`s or `Series`, and return *like-indexed* DataFrames or Series with CSS `\"attribute: value\"` pairs for the values.\n",
    "These functions can be incrementally passed to the `Styler` which collects the styles before rendering.\n",
    "\n",
    "### Contents\n",
    "\n",
    "- [Building Styles](#Building-Styles)\n",
    "- [Finer Control: Slicing](#Finer-Control:-Slicing)\n",
    "- [Builtin Styles](#Builtin-Styles)\n",
    "- [Other options](#Other-options)\n",
    "- [Sharing Styles](#Sharing-Styles)\n",
    "- [Limitations](#Limitations)\n",
    "- [Terms](#Terms)\n",
    "- [Extensibility](#Extensibility)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Building Styles\n",
    "\n",
    "Pass your style functions into one of the following methods:\n",
    "\n",
    "- `Styler.applymap`: elementwise\n",
    "- `Styler.apply`: column-/row-/table-wise\n",
    "\n",
    "Both of those methods take a function (and some other keyword arguments) and applies your function to the DataFrame in a certain way.\n",
    "`Styler.applymap` works through the DataFrame elementwise.\n",
    "`Styler.apply` passes each column or row into your DataFrame one-at-a-time or the entire table at once, depending on the `axis` keyword argument.\n",
    "For columnwise use `axis=0`, rowwise use `axis=1`, and for the entire table at once use `axis=None`.\n",
    "\n",
    "For `Styler.applymap` your function should take a scalar and return a single string with the CSS attribute-value pair.\n",
    "\n",
    "For `Styler.apply` your function should take a Series or DataFrame (depending on the axis parameter), and return a Series or DataFrame with an identical shape where each value is a string with a CSS attribute-value pair.\n",
    "\n",
    "Let's see some examples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(24)\n",
    "df = pd.DataFrame({'A': np.linspace(1, 10, 10)})\n",
    "df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],\n",
    "               axis=1)\n",
    "df.iloc[0, 2] = np.nan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's a boring example of rendering a DataFrame, without any (visible) styles:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Note*: The `DataFrame.style` attribute is a propetry that returns a `Styler` object. `Styler` has a `_repr_html_` method defined on it so they are rendered automatically. If you want the actual HTML back for further processing or for writing to file call the `.render()` method which returns a string.\n",
    "\n",
    "The above output looks very similar to the standard DataFrame HTML representation. But we've done some work behind the scenes to attach CSS classes to each cell. We can view these by calling the `.render` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.highlight_null().render().split('\\n')[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `row0_col2` is the identifier for that particular cell. We've also prepended each row/column identifier with a UUID unique to each DataFrame so that the style from one doesn't collide with the styling from another within the same notebook or page (you can set the `uuid` if you'd like to tie together the styling of two DataFrames).\n",
    "\n",
    "When writing style functions, you take care of producing the CSS attribute / value pairs you want. Pandas matches those up with the CSS classes that identify each cell."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's write a simple style function that will color negative numbers red and positive numbers black."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def color_negative_red(val):\n",
    "    \"\"\"\n",
    "    Takes a scalar and returns a string with\n",
    "    the css property `'color: red'` for negative\n",
    "    strings, black otherwise.\n",
    "    \"\"\"\n",
    "    color = 'red' if val < 0 else 'black'\n",
    "    return 'color: %s' % color"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case, the cell's style depends only on it's own value.\n",
    "That means we should use the `Styler.applymap` method which works elementwise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "s = df.style.applymap(color_negative_red)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice the similarity with the standard `df.applymap`, which operates on DataFrames elementwise. We want you to be able to resuse your existing knowledge of how to interact with DataFrames.\n",
    "\n",
    "Notice also that our function returned a string containing the CSS attribute and value, separated by a colon just like in a `<style>` tag. This will be a common theme.\n",
    "\n",
    "Finally, the input shapes matched. `Styler.applymap` calls the function on each scalar input, and the function returns a scalar output."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now suppose you wanted to highlight the maximum value in each column.\n",
    "We can't use `.applymap` anymore since that operated elementwise.\n",
    "Instead, we'll turn to `.apply` which operates columnwise (or rowwise using the `axis` keyword). Later on we'll see that something like `highlight_max` is already defined on `Styler` so you wouldn't need to write this yourself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def highlight_max(s):\n",
    "    '''\n",
    "    highlight the maximum in a Series yellow.\n",
    "    '''\n",
    "    is_max = s == s.max()\n",
    "    return ['background-color: yellow' if v else '' for v in is_max]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.apply(highlight_max)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case the input is a `Series`, one column at a time.\n",
    "Notice that the output shape of `highlight_max` matches the input shape, an array with `len(s)` items."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We encourage you to use method chains to build up a style piecewise, before finally rending at the end of the chain."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.\\\n",
    "    applymap(color_negative_red).\\\n",
    "    apply(highlight_max)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Above we used `Styler.apply` to pass in each column one at a time.\n",
    "\n",
    "<p style=\"background-color: #DEDEBE\">*Debugging Tip*: If you're having trouble writing your style function, try just passing it into <code style=\"background-color: #DEDEBE\">DataFrame.apply</code>. Internally, <code style=\"background-color: #DEDEBE\">Styler.apply</code> uses <code style=\"background-color: #DEDEBE\">DataFrame.apply</code> so the result should be the same.</p>\n",
    "\n",
    "What if you wanted to highlight just the maximum value in the entire table?\n",
    "Use `.apply(function, axis=None)` to indicate that your function wants the entire table, not one column or row at a time. Let's try that next.\n",
    "\n",
    "We'll rewrite our `highlight-max` to handle either Series (from `.apply(axis=0 or 1)`) or DataFrames (from `.apply(axis=None)`). We'll also allow the color to be adjustable, to demonstrate that `.apply`, and `.applymap` pass along keyword arguments."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def highlight_max(data, color='yellow'):\n",
    "    '''\n",
    "    highlight the maximum in a Series or DataFrame\n",
    "    '''\n",
    "    attr = 'background-color: {}'.format(color)\n",
    "    if data.ndim == 1:  # Series from .apply(axis=0) or axis=1\n",
    "        is_max = data == data.max()\n",
    "        return [attr if v else '' for v in is_max]\n",
    "    else:  # from .apply(axis=None)\n",
    "        is_max = data == data.max().max()\n",
    "        return pd.DataFrame(np.where(is_max, attr, ''),\n",
    "                            index=data.index, columns=data.columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When using ``Styler.apply(func, axis=None)``, the function must return a DataFrame with the same index and column labels."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.apply(highlight_max, color='darkorange', axis=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Building Styles Summary\n",
    "\n",
    "Style functions should return strings with one or more CSS `attribute: value` delimited by semicolons. Use\n",
    "\n",
    "- `Styler.applymap(func)` for elementwise styles\n",
    "- `Styler.apply(func, axis=0)` for columnwise styles\n",
    "- `Styler.apply(func, axis=1)` for rowwise styles\n",
    "- `Styler.apply(func, axis=None)` for tablewise styles\n",
    "\n",
    "And crucially the input and output shapes of `func` must match. If `x` is the input then ``func(x).shape == x.shape``."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finer Control: Slicing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Both `Styler.apply`, and `Styler.applymap` accept a `subset` keyword.\n",
    "This allows you to apply styles to specific rows or columns, without having to code that logic into your `style` function.\n",
    "\n",
    "The value passed to `subset` behaves simlar to slicing a DataFrame.\n",
    "\n",
    "- A scalar is treated as a column label\n",
    "- A list (or series or numpy array)\n",
    "- A tuple is treated as `(row_indexer, column_indexer)`\n",
    "\n",
    "Consider using `pd.IndexSlice` to construct the tuple for the last one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.apply(highlight_max, subset=['B', 'C', 'D'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For row and column slicing, any valid indexer to `.loc` will work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.applymap(color_negative_red,\n",
    "                  subset=pd.IndexSlice[2:5, ['B', 'D']])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Only label-based slicing is supported right now, not positional.\n",
    "\n",
    "If your style function uses a `subset` or `axis` keyword argument, consider wrapping your function in a `functools.partial`, partialing out that keyword.\n",
    "\n",
    "```python\n",
    "my_func2 = functools.partial(my_func, subset=42)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finer Control: Display Values\n",
    "\n",
    "We distinguish the *display* value from the *actual* value in `Styler`.\n",
    "To control the display value, the text is printed in each cell, use `Styler.format`. Cells can be formatted according to a [format spec string](https://docs.python.org/3/library/string.html#format-specification-mini-language) or a callable that takes a single value and returns a string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.format(\"{:.2%}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use a dictionary to format specific columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.format({'B': \"{:0<4.0f}\", 'D': '{:+.2f}'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or pass in a callable (or dictionary of callables) for more flexible handling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.format({\"B\": lambda x: \"±{:.2f}\".format(abs(x))})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Builtin Styles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we expect certain styling functions to be common enough that we've included a few \"built-in\" to the `Styler`, so you don't have to write them yourself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.highlight_null(null_color='red')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can create \"heatmaps\" with the `background_gradient` method. These require matplotlib, and we'll use [Seaborn](http://stanford.edu/~mwaskom/software/seaborn/) to get a nice colormap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "cm = sns.light_palette(\"green\", as_cmap=True)\n",
    "\n",
    "s = df.style.background_gradient(cmap=cm)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Styler.background_gradient` takes the keyword arguments `low` and `high`. Roughly speaking these extend the range of your data by `low` and `high` percent so that when we convert the colors, the colormap's entire range isn't used. This is useful so that you can actually read the text still."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Uses the full color range\n",
    "df.loc[:4].style.background_gradient(cmap='viridis')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Compreess the color range\n",
    "(df.loc[:4]\n",
    "    .style\n",
    "    .background_gradient(cmap='viridis', low=.5, high=0)\n",
    "    .highlight_null('red'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can include \"bar charts\" in your DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.bar(subset=['A', 'B'], color='#d65f5f')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There's also `.highlight_min` and `.highlight_max`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.highlight_max(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.highlight_min(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use `Styler.set_properties` when the style doesn't actually depend on the values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.set_properties(**{'background-color': 'black',\n",
    "                           'color': 'lawngreen',\n",
    "                           'border-color': 'white'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sharing Styles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Say you have a lovely style built up for a DataFrame, and now you want to apply the same style to a second DataFrame. Export the style with `df1.style.export`, and import it on the second DataFrame with `df1.style.set`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df2 = -df\n",
    "style1 = df.style.applymap(color_negative_red)\n",
    "style1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "style2 = df2.style\n",
    "style2.use(style1.export())\n",
    "style2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that you're able share the styles even though they're data aware. The styles are re-evaluated on the new DataFrame they've been `use`d upon."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Other options\n",
    "\n",
    "You've seen a few methods for data-driven styling.\n",
    "`Styler` also provides a few other options for styles that don't depend on the data.\n",
    "\n",
    "- precision\n",
    "- captions\n",
    "- table-wide styles\n",
    "\n",
    "Each of these can be specified in two ways:\n",
    "\n",
    "- A keyword argument to `pandas.core.Styler`\n",
    "- A call to one of the `.set_` methods, e.g. `.set_caption`\n",
    "\n",
    "The best method to use depends on the context. Use the `Styler` constructor when building many styled DataFrames that should all share the same properties. For interactive use, the`.set_` methods are more convenient."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Precision"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can control the precision of floats using pandas' regular `display.precision` option."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with pd.option_context('display.precision', 2):\n",
    "    html = (df.style\n",
    "              .applymap(color_negative_red)\n",
    "              .apply(highlight_max))\n",
    "html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or through a `set_precision` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style\\\n",
    "  .applymap(color_negative_red)\\\n",
    "  .apply(highlight_max)\\\n",
    "  .set_precision(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting the precision only affects the printed number; the full-precision values are always passed to your style functions. You can always use `df.round(2).style` if you'd prefer to round from the start."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Captions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Regular table captions can be added in a few ways."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.style.set_caption('Colormaps, with a caption.')\\\n",
    "    .background_gradient(cmap=cm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Table Styles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next option you have are \"table styles\".\n",
    "These are styles that apply to the table as a whole, but don't look at the data.\n",
    "Certain sytlings, including pseudo-selectors like `:hover` can only be used this way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from IPython.display import HTML\n",
    "\n",
    "def hover(hover_color=\"#ffff99\"):\n",
    "    return dict(selector=\"tr:hover\",\n",
    "                props=[(\"background-color\", \"%s\" % hover_color)])\n",
    "\n",
    "styles = [\n",
    "    hover(),\n",
    "    dict(selector=\"th\", props=[(\"font-size\", \"150%\"),\n",
    "                               (\"text-align\", \"center\")]),\n",
    "    dict(selector=\"caption\", props=[(\"caption-side\", \"bottom\")])\n",
    "]\n",
    "html = (df.style.set_table_styles(styles)\n",
    "          .set_caption(\"Hover to highlight.\"))\n",
    "html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`table_styles` should be a list of dictionaries.\n",
    "Each dictionary should have the `selector` and `props` keys.\n",
    "The value for `selector` should be a valid CSS selector.\n",
    "Recall that all the styles are already attached to an `id`, unique to\n",
    "each `Styler`. This selector is in addition to that `id`.\n",
    "The value for `props` should be a list of tuples of `('attribute', 'value')`.\n",
    "\n",
    "`table_styles` are extremely flexible, but not as fun to type out by hand.\n",
    "We hope to collect some useful ones either in pandas, or preferable in a new package that [builds on top](#Extensibility) the tools here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CSS Classes\n",
    "\n",
    "Certain CSS classes are attached to cells.\n",
    "\n",
    "- Index and Column names include `index_name` and `level<k>` where `k` is its level in a MultiIndex\n",
    "- Index label cells include\n",
    "  + `row_heading`\n",
    "  + `row<n>` where `n` is the numeric position of the row\n",
    "  + `level<k>` where `k` is the level in a MultiIndex\n",
    "- Column label cells include\n",
    "  + `col_heading`\n",
    "  + `col<n>` where `n` is the numeric position of the column\n",
    "  + `level<k>` where `k` is the level in a MultiIndex\n",
    "- Blank cells include `blank`\n",
    "- Data cells include `data`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Limitations\n",
    "\n",
    "- DataFrame only `(use Series.to_frame().style)`\n",
    "- The index and columns must be unique\n",
    "- No large repr, and performance isn't great; this is intended for summary DataFrames\n",
    "- You can only style the *values*, not the index or columns\n",
    "- You can only apply styles, you can't insert new HTML entities\n",
    "\n",
    "Some of these will be addressed in the future.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Terms\n",
    "\n",
    "- Style function: a function that's passed into `Styler.apply` or `Styler.applymap` and returns values like `'css attribute: value'`\n",
    "- Builtin style functions: style functions that are methods on `Styler`\n",
    "- table style: a dictionary with the two keys `selector` and `props`. `selector` is the CSS selector that `props` will apply to. `props` is a list of `(attribute, value)` tuples. A list of table styles passed into `Styler`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fun stuff\n",
    "\n",
    "Here are a few interesting examples.\n",
    "\n",
    "`Styler` interacts pretty well with widgets. If you're viewing this online instead of running the notebook yourself, you're missing out on interactively adjusting the color palette."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from IPython.html import widgets\n",
    "@widgets.interact\n",
    "def f(h_neg=(0, 359, 1), h_pos=(0, 359), s=(0., 99.9), l=(0., 99.9)):\n",
    "    return df.style.background_gradient(\n",
    "        cmap=sns.palettes.diverging_palette(h_neg=h_neg, h_pos=h_pos, s=s, l=l,\n",
    "                                            as_cmap=True)\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def magnify():\n",
    "    return [dict(selector=\"th\",\n",
    "                 props=[(\"font-size\", \"4pt\")]),\n",
    "            dict(selector=\"td\",\n",
    "                 props=[('padding', \"0em 0em\")]),\n",
    "            dict(selector=\"th:hover\",\n",
    "                 props=[(\"font-size\", \"12pt\")]),\n",
    "            dict(selector=\"tr:hover td:hover\",\n",
    "                 props=[('max-width', '200px'),\n",
    "                        ('font-size', '12pt')])\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "np.random.seed(25)\n",
    "cmap = cmap=sns.diverging_palette(5, 250, as_cmap=True)\n",
    "df = pd.DataFrame(np.random.randn(20, 25)).cumsum()\n",
    "\n",
    "df.style.background_gradient(cmap, axis=1)\\\n",
    "    .set_properties(**{'max-width': '80px', 'font-size': '1pt'})\\\n",
    "    .set_caption(\"Hover to magify\")\\\n",
    "    .set_precision(2)\\\n",
    "    .set_table_styles(magnify())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Extensibility\n",
    "\n",
    "The core of pandas is, and will remain, its \"high-performance, easy-to-use data structures\".\n",
    "With that in mind, we hope that `DataFrame.style` accomplishes two goals\n",
    "\n",
    "- Provide an API that is pleasing to use interactively and is \"good enough\" for many tasks\n",
    "- Provide the foundations for dedicated libraries to build on\n",
    "\n",
    "If you build a great library on top of this, let us know and we'll [link](http://pandas.pydata.org/pandas-docs/stable/ecosystem.html) to it.\n",
    "\n",
    "## Subclassing\n",
    "\n",
    "This section contains a bit of information about the implementation of `Styler`.\n",
    "Since the feature is so new all of this is subject to change, even more so than the end-use API.\n",
    "\n",
    "As users apply styles (via `.apply`, `.applymap` or one of the builtins), we don't actually calculate anything.\n",
    "Instead, we append functions and arguments to a list `self._todo`.\n",
    "When asked (typically in `.render` we'll walk through the list and execute each function (this is in `self._compute()`.\n",
    "These functions update an internal `defaultdict(list)`, `self.ctx` which maps DataFrame row / column positions to CSS attribute, value pairs.\n",
    "\n",
    "We take the extra step through `self._todo` so that we can export styles and set them on other `Styler`s.\n",
    "\n",
    "Rendering uses [Jinja](http://jinja.pocoo.org/) templates.\n",
    "The `.translate` method takes `self.ctx` and builds another dictionary ready to be passed into `Styler.template.render`, the Jinja template.\n",
    "\n",
    "\n",
    "## Alternate templates\n",
    "\n",
    "We've used [Jinja](http://jinja.pocoo.org/) templates to build up the HTML.\n",
    "The template is stored as a class variable ``Styler.template.``. Subclasses can override that.\n",
    "\n",
    "```python\n",
    "class CustomStyle(Styler):\n",
    "    template = Template(\"\"\"...\"\"\")\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
