{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python API for Table Display\n", "\n", "In addition to APIs for creating and formatting BeakerX's interactive table widget, the Python runtime configures pandas to display tables with the interactive widget instead of static HTML." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from beakerx import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pd.read_csv('../resources/data/interest-rates.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "table = TableDisplay(pd.read_csv('../resources/data/interest-rates.csv'))\n", "table.setAlignmentProviderForColumn('m3', TableDisplayAlignmentProvider.CENTER_ALIGNMENT)\n", "table.setRendererForColumn(\"y10\", TableDisplayCellRenderer.getDataBarsRenderer(False))\n", "table.setRendererForType(ColumnType.Double, TableDisplayCellRenderer.getDataBarsRenderer(True))\n", "table" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('../resources/data/interest-rates.csv')\n", "df['time'] = df['time'].str.slice(0,19).astype('datetime64[ns]')\n", "table = TableDisplay(df)\n", "table.setStringFormatForTimes(TimeUnit.DAYS)\n", "table.setStringFormatForType(ColumnType.Double, TableDisplayStringFormat.getDecimalFormat(4,6))\n", "table.setStringFormatForColumn(\"m3\", TableDisplayStringFormat.getDecimalFormat(0, 0))\n", "\n", "table" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "table = TableDisplay(pd.read_csv('../resources/data/interest-rates.csv'))\n", "table\n", "#freeze a column\n", "table.setColumnFrozen(\"y1\", True)\n", "#freeze a column to the right\n", "table.setColumnFrozenRight(\"y10\", True)\n", "#hide a column\n", "table.setColumnVisible(\"y30\", False)\n", "\n", "table.setColumnOrder([\"m3\", \"y1\", \"y5\", \"time\", \"y2\"])\n", "\n", "table" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "table = TableDisplay(pd.read_csv('../resources/data/interest-rates.csv'))\n", "table.addCellHighlighter(TableDisplayCellHighlighter.getHeatmapHighlighter(\"m3\", TableDisplayCellHighlighter.FULL_ROW))\n", "\n", "table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display mode: Pandas default" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "beakerx.pandas_display_default()\n", "pd.read_csv('../resources/data/interest-rates.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display mode: TableDisplay Widget" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "beakerx.pandas_display_table()\n", "pd.read_csv('../resources/data/interest-rates.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Recognized Formats" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "TableDisplay([{'y1':4, 'm3':2, 'z2':1}, {'m3':4, 'z2':2}])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "TableDisplay({\"x\" : 1, \"y\" : 2})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mapList4 = [\n", " {\"a\":1, \"b\":2, \"c\":3},\n", " {\"a\":4, \"b\":5, \"c\":6},\n", " {\"a\":7, \"b\":8, \"c\":5}\n", "]\n", "display = TableDisplay(mapList4)\n", "\n", "#set what happens on a double click\n", "display.setDoubleClickAction(lambda row, column, tabledisplay: tabledisplay.values[row].__setitem__(column, sum(tabledisplay.values[row])))\n", "\n", "#add a context menu item\n", "display.addContextMenuItem(\"negate\", lambda row, column, tabledisplay: tabledisplay.values[row].__setitem__(column, -1 * tabledisplay.values[row][column]))\n", "\n", "display" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mapList4 = [\n", " {\"a\":1, \"b\":2, \"c\":3},\n", " {\"a\":4, \"b\":5, \"c\":6},\n", " {\"a\":7, \"b\":8, \"c\":5}\n", "]\n", "display = TableDisplay(mapList4)\n", "\n", "#set what happens on a double click\n", "display.setDoubleClickAction(\"runDoubleClick\")\n", "\n", "display" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "runDoubleClick" ] }, "outputs": [], "source": [ "print(\"runDoubleClick fired\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set index to DataFrame" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('../resources/data/interest-rates.csv')\n", "df.set_index(['m3'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "df = pd.read_csv('../resources/data/interest-rates.csv')\n", "df.index = df['time']\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Update cell" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataToUpdate = [\n", " {'a':1, 'b':2, 'c':3},\n", " {'a':4, 'b':5, 'c':6},\n", " {'a':7, 'b':8, 'c':9}\n", "]\n", "tableToUpdate = TableDisplay(dataToUpdate)\n", "\n", "tableToUpdate" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tableToUpdate.values[0][0] = 99\n", "tableToUpdate.sendModel()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tableToUpdate.updateCell(2,\"c\",121)\n", "tableToUpdate.sendModel()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## HTML format\n", "\n", "HTML format allows markup and styling of the cell's content. Interactive JavaScript is not supported however." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "table = TableDisplay({'x': 'italic red',\n", " 'y': 'bold blue',\n", " 'z': 'strings without markup work fine too'})\n", "table.setStringFormatForColumn(\"Value\", TableDisplayStringFormat.getHTMLFormat())\n", "table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Auto linking of URLs\n", "\n", "The normal string format automatically detects URLs and links them. An underline appears when the mouse hovers over such a string, and when you click it opens in a new window." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "TableDisplay({'Two Sigma': 'http://twosigma.com', 'BeakerX': 'http://BeakerX'})" ] } ], "metadata": { "celltoolbar": "Tags", "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }