| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Load Pandas DataFrame" |
| 8 |
] |
| 9 |
}, |
| 10 |
{ |
| 11 |
"cell_type": "code", |
| 12 |
"execution_count": null, |
| 13 |
"metadata": {}, |
| 14 |
"outputs": [], |
| 15 |
"source": [ |
| 16 |
"import json\n", |
| 17 |
"\n", |
| 18 |
"import numpy as np\n", |
| 19 |
"import pandas as pd\n", |
| 20 |
"from bqplot import DateScale, ColorScale\n", |
| 21 |
"from py2vega.functions.type_coercing import toDate\n", |
| 22 |
"from py2vega.functions.date_time import datetime\n", |
| 23 |
"\n", |
| 24 |
"from ipydatagrid import Expr, DataGrid, TextRenderer, BarRenderer\n", |
| 25 |
"\n", |
| 26 |
"n = 10000\n", |
| 27 |
"\n", |
| 28 |
"df = pd.DataFrame(\n", |
| 29 |
" {\n", |
| 30 |
" \"Value 1\": np.random.randn(n),\n", |
| 31 |
" \"Value 2\": np.random.randn(n),\n", |
| 32 |
" \"Dates\": pd.date_range(end=pd.Timestamp(\"today\"), periods=n),\n", |
| 33 |
" }\n", |
| 34 |
")\n", |
| 35 |
"\n", |
| 36 |
"text_renderer = TextRenderer(\n", |
| 37 |
" text_color=\"black\", background_color=ColorScale(min=-5, max=5)\n", |
| 38 |
")\n", |
| 39 |
"\n", |
| 40 |
"\n", |
| 41 |
"def bar_color(cell):\n", |
| 42 |
" date = toDate(cell.value)\n", |
| 43 |
" return \"green\" if date > datetime(\"2000\") else \"red\"\n", |
| 44 |
"\n", |
| 45 |
"\n", |
| 46 |
"renderers = {\n", |
| 47 |
" \"Value 1\": text_renderer,\n", |
| 48 |
" \"Value 2\": text_renderer,\n", |
| 49 |
" \"Dates\": BarRenderer(\n", |
| 50 |
" bar_value=DateScale(),\n", |
| 51 |
" bar_color=Expr(bar_color),\n", |
| 52 |
" format=\"%Y/%m/%d\",\n", |
| 53 |
" format_type=\"time\",\n", |
| 54 |
" ),\n", |
| 55 |
"}\n", |
| 56 |
"\n", |
| 57 |
"grid = DataGrid(df, base_row_size=30, base_column_size=300, renderers=renderers)\n", |
| 58 |
"grid.transform([{\"type\": \"sort\", \"column\": \"Dates\", \"desc\": True}])\n", |
| 59 |
"grid" |
| 60 |
] |
| 61 |
}, |
| 62 |
{ |
| 63 |
"cell_type": "code", |
| 64 |
"execution_count": null, |
| 65 |
"metadata": {}, |
| 66 |
"outputs": [], |
| 67 |
"source": [] |
| 68 |
} |
| 69 |
], |
| 70 |
"metadata": { |
| 71 |
"kernelspec": { |
| 72 |
"display_name": "Python 3 (ipykernel)", |
| 73 |
"language": "python", |
| 74 |
"name": "python3" |
| 75 |
}, |
| 76 |
"language_info": { |
| 77 |
"codemirror_mode": { |
| 78 |
"name": "ipython", |
| 79 |
"version": 3 |
| 80 |
}, |
| 81 |
"file_extension": ".py", |
| 82 |
"mimetype": "text/x-python", |
| 83 |
"name": "python", |
| 84 |
"nbconvert_exporter": "python", |
| 85 |
"pygments_lexer": "ipython3", |
| 86 |
"version": "3.12.3" |
| 87 |
} |
| 88 |
}, |
| 89 |
"nbformat": 4, |
| 90 |
"nbformat_minor": 4 |
| 91 |
} |