| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Trees in Paris" |
| 8 |
] |
| 9 |
}, |
| 10 |
{ |
| 11 |
"cell_type": "code", |
| 12 |
"execution_count": null, |
| 13 |
"metadata": {}, |
| 14 |
"outputs": [], |
| 15 |
"source": [ |
| 16 |
"# Extract compressed file\n", |
| 17 |
"import tarfile\n", |
| 18 |
"\n", |
| 19 |
"with tarfile.open(\"trees.tar.gz\", \"r:gz\") as tar:\n", |
| 20 |
" tar.extractall()" |
| 21 |
] |
| 22 |
}, |
| 23 |
{ |
| 24 |
"cell_type": "code", |
| 25 |
"execution_count": null, |
| 26 |
"metadata": {}, |
| 27 |
"outputs": [], |
| 28 |
"source": [ |
| 29 |
"from json import load\n", |
| 30 |
"import pandas as pd\n", |
| 31 |
"\n", |
| 32 |
"from ipywidgets import Text, VBox\n", |
| 33 |
"from bqplot import LinearScale, ColorScale, OrdinalColorScale, OrdinalScale\n", |
| 34 |
"\n", |
| 35 |
"from py2vega.functions.color import rgb\n", |
| 36 |
"\n", |
| 37 |
"from ipydatagrid import StreamingDataGrid, TextRenderer, BarRenderer, Expr\n", |
| 38 |
"\n", |
| 39 |
"with open(\"./trees.json\") as fobj:\n", |
| 40 |
" data = load(fobj)\n", |
| 41 |
"\n", |
| 42 |
"df = pd.DataFrame(data[\"data\"]).drop(\"index\", axis=1)\n", |
| 43 |
"df = df[sorted(df.columns)]\n", |
| 44 |
"\n", |
| 45 |
"# Get list of possible tree species and kinds for the ordinal color scales\n", |
| 46 |
"species = list(\n", |
| 47 |
" set(\n", |
| 48 |
" [\n", |
| 49 |
" tree[\"Species\"]\n", |
| 50 |
" for tree in data[\"data\"]\n", |
| 51 |
" if tree[\"Species\"] is not None\n", |
| 52 |
" ]\n", |
| 53 |
" )\n", |
| 54 |
")\n", |
| 55 |
"kinds = list(\n", |
| 56 |
" set([tree[\"Kind\"] for tree in data[\"data\"] if tree[\"Kind\"] is not None])\n", |
| 57 |
")\n", |
| 58 |
"\n", |
| 59 |
"# Text box for highlighting tree names\n", |
| 60 |
"highlight_box = Text(description=\"Highlight: \", value=\"Platane\")\n", |
| 61 |
"name_background_color = Expr(\n", |
| 62 |
" \"'lightgreen' if '{}' in cell.value else default_value\".format(\n", |
| 63 |
" highlight_box.value\n", |
| 64 |
" )\n", |
| 65 |
")\n", |
| 66 |
"\n", |
| 67 |
"\n", |
| 68 |
"def on_textbox_changed(*args, **kwargs):\n", |
| 69 |
" name_background_color.value = (\n", |
| 70 |
" \"'lightgreen' if '{}' in cell.value else default_value\".format(\n", |
| 71 |
" highlight_box.value\n", |
| 72 |
" )\n", |
| 73 |
" )\n", |
| 74 |
"\n", |
| 75 |
"\n", |
| 76 |
"highlight_box.observe(on_textbox_changed, \"value\")\n", |
| 77 |
"\n", |
| 78 |
"\n", |
| 79 |
"def circumference_color(cell):\n", |
| 80 |
" color_value = 255 if cell.value > 300 else (cell.value / 300) * 255\n", |
| 81 |
" return rgb(0, color_value, color_value)\n", |
| 82 |
"\n", |
| 83 |
"\n", |
| 84 |
"renderers = {\n", |
| 85 |
" \"French_name\": TextRenderer(background_color=name_background_color),\n", |
| 86 |
" \"Height_m\": BarRenderer(\n", |
| 87 |
" bar_value=LinearScale(min=0, max=30),\n", |
| 88 |
" bar_color=ColorScale(min=0, max=30, scheme=\"viridis\"),\n", |
| 89 |
" horizontal_alignment=\"center\",\n", |
| 90 |
" ),\n", |
| 91 |
" \"Species\": TextRenderer(\n", |
| 92 |
" text_color=\"black\", background_color=OrdinalColorScale(domain=species)\n", |
| 93 |
" ),\n", |
| 94 |
" \"Kind\": TextRenderer(\n", |
| 95 |
" text_color=\"black\", background_color=OrdinalColorScale(domain=kinds)\n", |
| 96 |
" ),\n", |
| 97 |
" \"Type\": TextRenderer(missing=\"Unspecified\"),\n", |
| 98 |
" \"Circumference_cm\": BarRenderer(\n", |
| 99 |
" bar_value=LinearScale(min=0, max=300),\n", |
| 100 |
" bar_color=Expr(circumference_color),\n", |
| 101 |
" horizontal_alignment=\"center\",\n", |
| 102 |
" ),\n", |
| 103 |
"}\n", |
| 104 |
"\n", |
| 105 |
"datagrid = StreamingDataGrid(\n", |
| 106 |
" df, base_row_size=32, base_column_size=90, renderers=renderers\n", |
| 107 |
")\n", |
| 108 |
"VBox((highlight_box, datagrid))" |
| 109 |
] |
| 110 |
}, |
| 111 |
{ |
| 112 |
"cell_type": "code", |
| 113 |
"execution_count": null, |
| 114 |
"metadata": {}, |
| 115 |
"outputs": [], |
| 116 |
"source": [ |
| 117 |
"df.iloc[87642]" |
| 118 |
] |
| 119 |
}, |
| 120 |
{ |
| 121 |
"cell_type": "code", |
| 122 |
"execution_count": null, |
| 123 |
"metadata": {}, |
| 124 |
"outputs": [], |
| 125 |
"source": [ |
| 126 |
"datagrid._handle_comm_msg(\n", |
| 127 |
" None,\n", |
| 128 |
" dict(\n", |
| 129 |
" type=\"data-request\",\n", |
| 130 |
" r1=87641,\n", |
| 131 |
" r2=87643,\n", |
| 132 |
" c1=0,\n", |
| 133 |
" c2=3,\n", |
| 134 |
" ),\n", |
| 135 |
" None\n", |
| 136 |
")" |
| 137 |
] |
| 138 |
}, |
| 139 |
{ |
| 140 |
"cell_type": "code", |
| 141 |
"execution_count": null, |
| 142 |
"metadata": {}, |
| 143 |
"outputs": [], |
| 144 |
"source": [ |
| 145 |
"def circumference_color(cell):\n", |
| 146 |
" color_value = 255 if cell.value > 300 else (cell.value / 300) * 255\n", |
| 147 |
" return rgb(color_value, 0, 0)\n", |
| 148 |
"\n", |
| 149 |
"\n", |
| 150 |
"renderers[\"Circumference_cm\"].bar_color.value = circumference_color" |
| 151 |
] |
| 152 |
}, |
| 153 |
{ |
| 154 |
"cell_type": "code", |
| 155 |
"execution_count": null, |
| 156 |
"metadata": {}, |
| 157 |
"outputs": [], |
| 158 |
"source": [ |
| 159 |
"datagrid.transform(\n", |
| 160 |
" [\n", |
| 161 |
" {\"type\": \"sort\", \"column\": \"Height_m\"},\n", |
| 162 |
" {\n", |
| 163 |
" \"type\": \"filter\",\n", |
| 164 |
" \"operator\": \"=\",\n", |
| 165 |
" \"column\": \"Kind\",\n", |
| 166 |
" \"value\": \"Platanus\",\n", |
| 167 |
" },\n", |
| 168 |
" ]\n", |
| 169 |
")" |
| 170 |
] |
| 171 |
} |
| 172 |
], |
| 173 |
"metadata": { |
| 174 |
"kernelspec": { |
| 175 |
"display_name": "Python 3 (ipykernel)", |
| 176 |
"language": "python", |
| 177 |
"name": "python3" |
| 178 |
}, |
| 179 |
"language_info": { |
| 180 |
"codemirror_mode": { |
| 181 |
"name": "ipython", |
| 182 |
"version": 3 |
| 183 |
}, |
| 184 |
"file_extension": ".py", |
| 185 |
"mimetype": "text/x-python", |
| 186 |
"name": "python", |
| 187 |
"nbconvert_exporter": "python", |
| 188 |
"pygments_lexer": "ipython3", |
| 189 |
"version": "3.12.3" |
| 190 |
} |
| 191 |
}, |
| 192 |
"nbformat": 4, |
| 193 |
"nbformat_minor": 4 |
| 194 |
} |