{ "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": [ "table = TableDisplay(pd.read_csv('../resources/data/interest-rates.csv'))\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": {}, "outputs": [], "source": [ "df = pd.read_csv('../resources/data/interest-rates.csv')\n", "df.index = df['time']\n", "df" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }