From dc5594a33881a05042c5c89e19268336e19d8979 Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Tue, 16 Aug 2022 17:24:21 +0200 Subject: [PATCH] Propose a solution for using Choropleth layer without giving an initial choro_data. --- examples/Choropleth.ipynb | 31 ++++----------- examples/Choropleth_with_NANS.ipynb | 60 +++++++++++++++++++++-------- ipyleaflet/leaflet.py | 11 +++++- 3 files changed, 62 insertions(+), 40 deletions(-) diff --git a/examples/Choropleth.ipynb b/examples/Choropleth.ipynb index f14ac6826..8414df5da 100644 --- a/examples/Choropleth.ipynb +++ b/examples/Choropleth.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -34,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -45,38 +45,23 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "layer = ipyleaflet.Choropleth(\n", " geo_data=geo_json_data,\n", - " choro_data=unemployment,\n", + " #choro_data=unemployment,\n", " colormap=linear.YlOrRd_04,\n", - " style={\"fillOpacity\": 0.8, \"dashArray\": \"5, 5\"},\n", + " #style={\"fillOpacity\": 1.0, \"dashArray\": \"5, 5\"},\n", ")" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "6228f855244746258a6ad0553d1326f4", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Map(center=[43, -100], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_out_t…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "m.add(layer)\n", "m" @@ -205,7 +190,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.5" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/examples/Choropleth_with_NANS.ipynb b/examples/Choropleth_with_NANS.ipynb index e8ca81a2f..e8acb5e0b 100644 --- a/examples/Choropleth_with_NANS.ipynb +++ b/examples/Choropleth_with_NANS.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -36,7 +36,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -47,7 +47,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -61,19 +61,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TraitError", + "evalue": "The 'choro_data' trait of a Choropleth instance expected a dict, not the DataFrame State Unemployment\n0 AL NaN\n1 AK NaN\n2 AZ NaN\n3 AR NaN\n4 CA NaN\n5 CO NaN\n6 CT NaN\n7 DE NaN\n8 FL NaN\n9 GA NaN\n10 HI NaN\n11 ID NaN\n12 IL NaN\n13 IN NaN\n14 IA NaN\n15 KS NaN\n16 KY NaN\n17 LA NaN\n18 ME NaN\n19 MD NaN\n20 MA 6.7\n21 MI 9.1\n22 MN 5.6\n23 MS 9.1\n24 MO NaN\n25 MT 5.8\n26 NE 3.9\n27 NV 10.3\n28 NH 5.7\n29 NJ NaN\n30 NM 6.8\n31 NY 8.4\n32 NC 9.4\n33 ND 3.2\n34 OH 6.9\n35 OK 5.2\n36 OR 8.5\n37 PA 8.0\n38 RI 10.1\n39 SC 8.8\n40 SD 4.4\n41 TN 7.8\n42 TX NaN\n43 UT 5.5\n44 VT 5.0\n45 VA NaN\n46 WA 7.8\n47 WV 7.5\n48 WI 6.8\n49 WY 5.1.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTraitError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m colormap \u001b[38;5;241m=\u001b[39m linear\u001b[38;5;241m.\u001b[39mYlOrRd_04\n\u001b[0;32m----> 2\u001b[0m layer \u001b[38;5;241m=\u001b[39m \u001b[43mipyleaflet\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mChoropleth\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[43m \u001b[49m\u001b[43mgeo_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgeo_json_data\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[43mchoro_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43munemployment\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[43m \u001b[49m\u001b[43mcolormap\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcolormap\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[43mstyle\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfillOpacity\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0.8\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mdashArray\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m5, 5\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/ipyleaflet/ipyleaflet/leaflet.py:1474\u001b[0m, in \u001b[0;36mChoropleth.__init__\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 1473\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m-> 1474\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mChoropleth\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1475\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdata \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_data()\n", + "File \u001b[0;32m~/ipyleaflet/ipyleaflet/leaflet.py:1257\u001b[0m, in \u001b[0;36mGeoJSON.__init__\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 1254\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 1255\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mupdating \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m-> 1257\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mGeoJSON\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1259\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdata \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_data()\n\u001b[1;32m 1260\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mupdating \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n", + "File \u001b[0;32m~/ipyleaflet/ipyleaflet/leaflet.py:146\u001b[0m, in \u001b[0;36mLayer.__init__\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m--> 146\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mLayer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 147\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mon_msg(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_handle_mouse_events)\n", + "File \u001b[0;32m~/mambaforge/envs/ipyleaflet-dev/lib/python3.9/site-packages/ipywidgets/widgets/widget.py:477\u001b[0m, in \u001b[0;36mWidget.__init__\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 475\u001b[0m \u001b[38;5;124;03m\"\"\"Public constructor\"\"\"\u001b[39;00m\n\u001b[1;32m 476\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_model_id \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmodel_id\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[0;32m--> 477\u001b[0m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mWidget\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 479\u001b[0m Widget\u001b[38;5;241m.\u001b[39m_call_widget_constructed(\u001b[38;5;28mself\u001b[39m)\n\u001b[1;32m 480\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mopen()\n", + "File \u001b[0;32m~/mambaforge/envs/ipyleaflet-dev/lib/python3.9/site-packages/traitlets/traitlets.py:1232\u001b[0m, in \u001b[0;36mHasTraits.__init__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1230\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m key, value \u001b[38;5;129;01min\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mitems():\n\u001b[1;32m 1231\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhas_trait(key):\n\u001b[0;32m-> 1232\u001b[0m \u001b[38;5;28;43msetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1233\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1234\u001b[0m \u001b[38;5;66;03m# passthrough args that don't set traits to super\u001b[39;00m\n\u001b[1;32m 1235\u001b[0m super_kwargs[key] \u001b[38;5;241m=\u001b[39m value\n", + "File \u001b[0;32m~/mambaforge/envs/ipyleaflet-dev/lib/python3.9/site-packages/traitlets/traitlets.py:712\u001b[0m, in \u001b[0;36mTraitType.__set__\u001b[0;34m(self, obj, value)\u001b[0m\n\u001b[1;32m 710\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m TraitError(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mThe \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m trait is read-only.\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m%\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname)\n\u001b[1;32m 711\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 712\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mset\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/mambaforge/envs/ipyleaflet-dev/lib/python3.9/site-packages/traitlets/traitlets.py:686\u001b[0m, in \u001b[0;36mTraitType.set\u001b[0;34m(self, obj, value)\u001b[0m\n\u001b[1;32m 685\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mset\u001b[39m(\u001b[38;5;28mself\u001b[39m, obj, value):\n\u001b[0;32m--> 686\u001b[0m new_value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_validate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 687\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 688\u001b[0m old_value \u001b[38;5;241m=\u001b[39m obj\u001b[38;5;241m.\u001b[39m_trait_values[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname]\n", + "File \u001b[0;32m~/mambaforge/envs/ipyleaflet-dev/lib/python3.9/site-packages/traitlets/traitlets.py:718\u001b[0m, in \u001b[0;36mTraitType._validate\u001b[0;34m(self, obj, value)\u001b[0m\n\u001b[1;32m 716\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m value\n\u001b[1;32m 717\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalidate\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[0;32m--> 718\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# type:ignore[attr-defined]\u001b[39;00m\n\u001b[1;32m 719\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m obj\u001b[38;5;241m.\u001b[39m_cross_validation_lock \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m:\n\u001b[1;32m 720\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_cross_validate(obj, value)\n", + "File \u001b[0;32m~/mambaforge/envs/ipyleaflet-dev/lib/python3.9/site-packages/traitlets/traitlets.py:3180\u001b[0m, in \u001b[0;36mDict.validate\u001b[0;34m(self, obj, value)\u001b[0m\n\u001b[1;32m 3179\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mvalidate\u001b[39m(\u001b[38;5;28mself\u001b[39m, obj, value):\n\u001b[0;32m-> 3180\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3181\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m value \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 3182\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m value\n", + "File \u001b[0;32m~/mambaforge/envs/ipyleaflet-dev/lib/python3.9/site-packages/traitlets/traitlets.py:2029\u001b[0m, in \u001b[0;36mInstance.validate\u001b[0;34m(self, obj, value)\u001b[0m\n\u001b[1;32m 2027\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m value\n\u001b[1;32m 2028\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 2029\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43merror\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/mambaforge/envs/ipyleaflet-dev/lib/python3.9/site-packages/traitlets/traitlets.py:824\u001b[0m, in \u001b[0;36mTraitType.error\u001b[0;34m(self, obj, value, error, info)\u001b[0m\n\u001b[1;32m 818\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 819\u001b[0m e \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m trait expected \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m, not \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m.\u001b[39mformat(\n\u001b[1;32m 820\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname,\n\u001b[1;32m 821\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minfo(),\n\u001b[1;32m 822\u001b[0m describe(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthe\u001b[39m\u001b[38;5;124m\"\u001b[39m, value),\n\u001b[1;32m 823\u001b[0m )\n\u001b[0;32m--> 824\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m TraitError(e)\n", + "\u001b[0;31mTraitError\u001b[0m: The 'choro_data' trait of a Choropleth instance expected a dict, not the DataFrame State Unemployment\n0 AL NaN\n1 AK NaN\n2 AZ NaN\n3 AR NaN\n4 CA NaN\n5 CO NaN\n6 CT NaN\n7 DE NaN\n8 FL NaN\n9 GA NaN\n10 HI NaN\n11 ID NaN\n12 IL NaN\n13 IN NaN\n14 IA NaN\n15 KS NaN\n16 KY NaN\n17 LA NaN\n18 ME NaN\n19 MD NaN\n20 MA 6.7\n21 MI 9.1\n22 MN 5.6\n23 MS 9.1\n24 MO NaN\n25 MT 5.8\n26 NE 3.9\n27 NV 10.3\n28 NH 5.7\n29 NJ NaN\n30 NM 6.8\n31 NY 8.4\n32 NC 9.4\n33 ND 3.2\n34 OH 6.9\n35 OK 5.2\n36 OR 8.5\n37 PA 8.0\n38 RI 10.1\n39 SC 8.8\n40 SD 4.4\n41 TN 7.8\n42 TX NaN\n43 UT 5.5\n44 VT 5.0\n45 VA NaN\n46 WA 7.8\n47 WV 7.5\n48 WI 6.8\n49 WY 5.1." + ] + } + ], "source": [ - "# To display the colormap bar on the map\n", - "from ipywidgets import Output\n", - "from ipyleaflet import WidgetControl\n", - "\n", - "out = Output()\n", - "\n", - "with out:\n", - " colormap = colormap_choice.scale(layer.value_min,layer.value_max)\n", - " display(colormap)" + "colormap = linear.YlOrRd_04\n", + "layer = ipyleaflet.Choropleth(\n", + " geo_data=geo_json_data,\n", + " choro_data=unemployment,\n", + " colormap=colormap,\n", + " style={\"fillOpacity\": 0.8, \"dashArray\": \"5, 5\"},\n", + ")" ] }, { @@ -83,9 +104,16 @@ "outputs": [], "source": [ "m.add(layer)\n", - "m.add(WidgetControl(widget=out, position='topright'))\n", + "m.add(colormap)\n", "m" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -104,7 +132,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.5" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/ipyleaflet/leaflet.py b/ipyleaflet/leaflet.py index 0f5deed85..2b2183aef 100644 --- a/ipyleaflet/leaflet.py +++ b/ipyleaflet/leaflet.py @@ -1446,8 +1446,14 @@ def compute_style(feature, colormap, choro_data): def _get_data(self): if not self.geo_data: return {} + if not self.choro_data: + self.choro_data = {} + for i in range(len(self.geo_data['features'])): + key = self.geo_data['features'][i][self.key_on] + self.choro_data[key] = 0 choro_data_values_list = [x for x in self.choro_data.values() if not isnan(x)] + check_identical = len(choro_data_values_list) > 0 and all(elem == choro_data_values_list[0] for elem in choro_data_values_list) if self.value_min is None: self.value_min = min(choro_data_values_list) @@ -1461,7 +1467,10 @@ def _get_data(self): for feature in data['features']: feature['properties']['style'] = self.style_callback(feature, colormap, self.choro_data[feature[self.key_on]]) - + if check_identical: # check if all values of choro_data are the same + feature['properties']['style']['fillColor'] = self.nan_color + feature['properties']['style']['fillOpacity'] = self.nan_opacity + warnings.warn("All the values of choro_data are identical, affected style is nan style", UserWarning) return data def __init__(self, **kwargs):