{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ipysheet: Interactive spreadsheets for the Jupyter Notebook\n", "\n", "## https://github.com/QuantStack/ipysheet/\n", "\n", "\n", "ipysheet is a jupyter interactive widget library which provides a spreadsheet widget to the Jupyter notebook.\n", "\n", "- MIT Licensed\n", "\n", "**Installation:**\n", "\n", "```bash\n", "conda install -c conda-forge ipysheet\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting started\n", "\n", "ipysheet provides ways to create a sheet with interactive cells and cell-ranges:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipysheet import sheet, cell\n", "\n", "s = sheet(rows=3, columns=4)\n", "cell1 = cell(0, 0, 'Hello')\n", "cell2 = cell(2, 0, 'World')\n", "cell_value = cell(2,2, 42.)\n", "s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cell1.value = 'Hi'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a spreadsheet from a Pandas dataframe/NumPy arrays\n", "\n", "You can easily load your data from dataframes or arrays using the `from_dataframe` and `from_array` helper functions. It returns a spreadsheet containing cell-ranges with your data.\n", "\n", "You can also use the `to_dataframe` and `to_array` to export your data back to Pandas/NumPy datastructures once your done interacting with it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from ipysheet import from_dataframe\n", "\n", "number_columns = 26\n", "\n", "dates = pd.date_range('20130101', periods=6)\n", "df = pd.DataFrame(np.random.randn(6, number_columns), index=dates, columns=list(chr(ord('A') + i) for i in range(number_columns)))\n", "\n", "s = from_dataframe(df)\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Link a spreadsheet to a bqplot\n", "\n", "The same way you can `link` two widget values together, nothing stops you from linking an entire cell-range to an interactive bplot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from traitlets import link\n", "from ipywidgets import HBox\n", "import bqplot.pyplot as plt\n", "from ipysheet import sheet, cell, column\n", "\n", "size = 18\n", "scale = 100.\n", "np.random.seed(0)\n", "x_data = np.arange(size)\n", "y_data = np.cumsum(np.random.randn(size) * scale)\n", "\n", "fig = plt.figure()\n", "axes_options = {'x': {'label': 'Date', 'tick_format': '%m/%d'},\n", " 'y': {'label': 'Price', 'tick_format': '0.0f'}}\n", "\n", "scatt = plt.scatter(x_data, y_data, colors=['red'], stroke='black')\n", "fig.layout.width = '70%'\n", "\n", "s = sheet(rows=size, columns=2)\n", "x_column = column(0, x_data)\n", "y_column = column(1, y_data)\n", "\n", "link((scatt, 'x'), (x_column, 'value'))\n", "link((scatt, 'y'), (y_column, 'value'))\n", "\n", "HBox((fig, s))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculation decorator\n", "\n", "The `calculation` decorator allows you to define a cell as being the result of an operation depending on other cells. It will listen for changes on the cell dependencies, and update it dynamically." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ipywidgets as widgets\n", "from ipysheet import calculation\n", "\n", "s = sheet(rows=3, columns=2, column_headers=False, row_headers=False)\n", "cell_a = cell(0, 1, 1, label_left='a')\n", "cell_b = cell(1, 1, 2, label_left='b')\n", "cell_sum = cell(2, 1, 3, label_left='sum', read_only=True)\n", "\n", "# create a slider linked to cell a\n", "slider = widgets.FloatSlider(min=-10, max=10, description='a')\n", "widgets.jslink((cell_a, 'value'), (slider, 'value'))\n", "\n", "@calculation(inputs=[cell_a, cell_b], output=cell_sum)\n", "def calculate(a, b):\n", " return a + b\n", "\n", "widgets.VBox([s, slider])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom renderers\n", "\n", "Custom renderers allows you to change the CSS of the cells (e.g. text color, background color) depending on the cell value. Whenever a custom renderer is created, it is then available for any cell, and you can specify which renderer a cell is using when creating it.\n", "\n", "Here we create a `renderer_negative` function which will color the cells in orange if values are negative." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipysheet import renderer\n", "\n", "def renderer_negative(value):\n", " return {\n", " 'backgroundColor': 'orange' if value < 0 else ''\n", " }\n", "renderer(code=renderer_negative, name='negative_transpiled');" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "from ipysheet import cell_range\n", "\n", "s = sheet(rows=3, columns=4)\n", "data = [[random.randint(-10, 10) for j in range(4)] for j in range(3)]\n", "cell_range(data, renderer='negative_transpiled')\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Embed interactive widgets in cells" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import FloatSlider\n", "from ipysheet import column\n", "\n", "s = sheet()\n", "\n", "column1 = column(0, [FloatSlider() for _ in range(5)])\n", "column2 = column(1, [1.] * 5)\n", "\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and export to a dataframe:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipysheet import to_dataframe\n", "\n", "to_dataframe(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "widgets-tutorial", "language": "python", "name": "widgets-tutorial" }, "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }