diff --git a/docs/notebooks/01_leafmap_intro.ipynb b/docs/notebooks/01_leafmap_intro.ipynb index 81147bcd81..b85ebb60ab 100644 --- a/docs/notebooks/01_leafmap_intro.ipynb +++ b/docs/notebooks/01_leafmap_intro.ipynb @@ -2,7 +2,9 @@ "cells": [ { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "id": "1CFzMrw5zmzc" + }, "source": [ "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/giswqs/leafmap/blob/master/examples/notebooks/01_leafmap_intro.ipynb)\n", "[![image](https://binder.pangeo.io/badge_logo.svg)](https://gishub.org/leafmap-pangeo)\n", @@ -13,7 +15,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "Z-EC7VcMzmzd" + }, "outputs": [], "source": [ "# !pip install leafmap" @@ -22,7 +26,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "-R1zCh5IjYTn" + }, "outputs": [], "source": [ "import leafmap" @@ -31,7 +37,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "-t_ER_sLzmze" + }, "outputs": [], "source": [ "m = leafmap.Map()\n", @@ -41,7 +49,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "QMRsdUtEzmze" + }, "outputs": [], "source": [ "m = leafmap.Map(center=[50, 19], zoom=4) #center=[lat, lon]\n", @@ -51,7 +61,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "WoB3Qz2wzmze" + }, "outputs": [], "source": [ "m = leafmap.Map(draw_control=False, measure_control=False, fullscreen_control=False, attribution_control=True)\n", @@ -62,16 +74,21 @@ "cell_type": "code", "execution_count": null, "metadata": { + "id": "EJ3NluXUzmze", "scrolled": false }, "outputs": [], "source": [ - "m = leafmap.Map(height=\"450px\")\n", + "m = leafmap.Map(height=\"450px\", width=\"800px\")\n", "m" ] } ], "metadata": { + "colab": { + "name": "01_leafmap_intro.ipynb", + "provenance": [] + }, "hide_input": false, "kernelspec": { "display_name": "Python 3", @@ -134,5 +151,5 @@ } }, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 1 } diff --git a/docs/notebooks/19_map_to_html.ipynb b/docs/notebooks/19_map_to_html.ipynb new file mode 100644 index 0000000000..a4ffa99fbc --- /dev/null +++ b/docs/notebooks/19_map_to_html.ipynb @@ -0,0 +1,161 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/giswqs/leafmap/blob/master/examples/notebooks/01_leafmap_intro.ipynb)\n", + "[![image](https://binder.pangeo.io/badge_logo.svg)](https://gishub.org/leafmap-pangeo)\n", + "\n", + "Uncomment the following line to install [leafmap](https://leafmap.org) if needed." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import leafmap.foliumap as leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# leafmap.update_package()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "m = leafmap.Map()\n", + "m.add_basemap(\"HYBRID\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "m.to_html(\"mymap.html\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "html = m.to_html()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "# print(html)" + ] + } + ], + "metadata": { + "hide_input": false, + "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.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/examples/notebooks/00_notebook_template.ipynb b/examples/notebooks/00_notebook_template.ipynb new file mode 100644 index 0000000000..79a38decfa --- /dev/null +++ b/examples/notebooks/00_notebook_template.ipynb @@ -0,0 +1,122 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/giswqs/leafmap/blob/master/examples/notebooks/01_leafmap_intro.ipynb)\n", + "[![image](https://binder.pangeo.io/badge_logo.svg)](https://gishub.org/leafmap-pangeo)\n", + "\n", + "Uncomment the following line to install [leafmap](https://leafmap.org) if needed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# leafmap.update_package()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = leafmap.Map()\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "hide_input": false, + "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.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/examples/notebooks/01_leafmap_intro.ipynb b/examples/notebooks/01_leafmap_intro.ipynb index 685aa0d775..b85ebb60ab 100644 --- a/examples/notebooks/01_leafmap_intro.ipynb +++ b/examples/notebooks/01_leafmap_intro.ipynb @@ -1,195 +1,155 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "hide_input": false, - "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.8.5" - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": true, - "sideBar": true, - "skip_h1_title": false, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": false, - "toc_position": {}, - "toc_section_display": true, - "toc_window_display": false - }, - "varInspector": { - "cols": { - "lenName": 16, - "lenType": 16, - "lenVar": 40 - }, - "kernels_config": { - "python": { - "delete_cmd_postfix": "", - "delete_cmd_prefix": "del ", - "library": "var_list.py", - "varRefreshCmd": "print(var_dic_list())" - }, - "r": { - "delete_cmd_postfix": ") ", - "delete_cmd_prefix": "rm(", - "library": "var_list.r", - "varRefreshCmd": "cat(var_dic_list()) " - } - }, - "types_to_exclude": [ - "module", - "function", - "builtin_function_or_method", - "instance", - "_Feature" - ], - "window_display": false - }, - "colab": { - "name": "01_leafmap_intro.ipynb", - "provenance": [] - } + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "1CFzMrw5zmzc" + }, + "source": [ + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/giswqs/leafmap/blob/master/examples/notebooks/01_leafmap_intro.ipynb)\n", + "[![image](https://binder.pangeo.io/badge_logo.svg)](https://gishub.org/leafmap-pangeo)\n", + "\n", + "Uncomment the following line to install [leafmap](https://leafmap.org) if needed." + ] }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "1CFzMrw5zmzc" - }, - "source": [ - "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/giswqs/leafmap/blob/master/examples/notebooks/01_leafmap_intro.ipynb)\n", - "[![image](https://binder.pangeo.io/badge_logo.svg)](https://gishub.org/leafmap-pangeo)\n", - "\n", - "Uncomment the following line to install [leafmap](https://leafmap.org) if needed." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "Z-EC7VcMzmzd" - }, - "source": [ - "# !pip install leafmap" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "NMaUEGU61AjG" - }, - "source": [ - "**Note**\n", - "\n", - "- The line: `leafmap.update_package()`\n", - "makes sure that the figure \n", - "windows are adjusted to height/width specifications.\n", - "- You need to run the following cell only once. \n", - "- Running the cell itself will enforce the notebook to restart.\n", - "- See: https://github.com/giswqs/leafmap/issues/21\n", - "for more details.\n", - "- After running this cell, set the parmeter `FIRSTRUN = False` and rerun the notebook." - ] - }, - { - "cell_type": "code", - "metadata": { - "cellView": "form", - "id": "gSnIF9gyzmzd" - }, - "source": [ - "#@title Run this cell only once\n", - "#@markdown Unckeck (set to *False*) the parameter `FIRSTRUN` when you run the notebook **AFTER** the *auto-restart*. \n", - "FIRSTRUN = True #@param {type:\"boolean\"}\n", - "## Run this cell once and then \n", - "## comment it out after first run\n", - "if FIRSTRUN:\n", - " import leafmap\n", - " leafmap.update_package()\n", - " print(\"Restarting notebook server...\")\n", - " import os\n", - " os.kill(os.getpid(), 9)" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "-R1zCh5IjYTn" - }, - "source": [ - "import leafmap" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "-t_ER_sLzmze" - }, - "source": [ - "m = leafmap.Map()\n", - "m" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "QMRsdUtEzmze" - }, - "source": [ - "m = leafmap.Map(center=[50, 19], zoom=4) #center=[lat, lon]\n", - "m" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "WoB3Qz2wzmze" - }, - "source": [ - "m = leafmap.Map(draw_control=False, measure_control=False, fullscreen_control=False, attribution_control=True)\n", - "m" - ], - "execution_count": null, - "outputs": [] + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Z-EC7VcMzmzd" + }, + "outputs": [], + "source": [ + "# !pip install leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "-R1zCh5IjYTn" + }, + "outputs": [], + "source": [ + "import leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "-t_ER_sLzmze" + }, + "outputs": [], + "source": [ + "m = leafmap.Map()\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "QMRsdUtEzmze" + }, + "outputs": [], + "source": [ + "m = leafmap.Map(center=[50, 19], zoom=4) #center=[lat, lon]\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "WoB3Qz2wzmze" + }, + "outputs": [], + "source": [ + "m = leafmap.Map(draw_control=False, measure_control=False, fullscreen_control=False, attribution_control=True)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "EJ3NluXUzmze", + "scrolled": false + }, + "outputs": [], + "source": [ + "m = leafmap.Map(height=\"450px\", width=\"800px\")\n", + "m" + ] + } + ], + "metadata": { + "colab": { + "name": "01_leafmap_intro.ipynb", + "provenance": [] + }, + "hide_input": false, + "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.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" }, - { - "cell_type": "code", - "metadata": { - "scrolled": false, - "id": "EJ3NluXUzmze" - }, - "source": [ - "m = leafmap.Map(height=\"450px\")\n", - "m" - ], - "execution_count": null, - "outputs": [] + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " } - ] -} \ No newline at end of file + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/examples/notebooks/19_map_to_html.ipynb b/examples/notebooks/19_map_to_html.ipynb new file mode 100644 index 0000000000..8a19b9342a --- /dev/null +++ b/examples/notebooks/19_map_to_html.ipynb @@ -0,0 +1,147 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/giswqs/leafmap/blob/master/examples/notebooks/01_leafmap_intro.ipynb)\n", + "[![image](https://binder.pangeo.io/badge_logo.svg)](https://gishub.org/leafmap-pangeo)\n", + "\n", + "Uncomment the following line to install [leafmap](https://leafmap.org) if needed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import leafmap.foliumap as leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# leafmap.update_package()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "m = leafmap.Map()\n", + "m.add_basemap(\"HYBRID\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "m.to_html(\"mymap.html\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "html = m.to_html()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "# print(html)" + ] + } + ], + "metadata": { + "hide_input": false, + "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.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/leafmap/foliumap.py b/leafmap/foliumap.py index 57812c4dae..892db2d570 100644 --- a/leafmap/foliumap.py +++ b/leafmap/foliumap.py @@ -1106,6 +1106,36 @@ def publish( except Exception as e: raise Exception(e) + def to_html(self, outfile=None, **kwargs): + """Exports a map as an HTML file. + + Args: + outfile (str, optional): File path to the output HTML. Defaults to None. + + Raises: + ValueError: If it is an invalid HTML file. + + Returns: + str: A string containing the HTML code. + """ + if outfile is not None: + if not outfile.endswith(".html"): + raise ValueError("The output file extension must be html.") + outfile = os.path.abspath(outfile) + out_dir = os.path.dirname(outfile) + if not os.path.exists(out_dir): + os.makedirs(out_dir) + self.save(outfile, **kwargs) + else: + outfile = os.path.abspath(random_string() + ".html") + self.save(outfile, **kwargs) + out_html = "" + with open(outfile) as f: + lines = f.readlines() + out_html = "".join(lines) + os.remove(outfile) + return out_html + def delete_dp_report(name): """Deletes a datapane report. diff --git a/leafmap/leafmap.py b/leafmap/leafmap.py index 8d4a448149..3a8d465cb4 100644 --- a/leafmap/leafmap.py +++ b/leafmap/leafmap.py @@ -1237,16 +1237,17 @@ def video_overlay(self, url, bounds, name): def to_html( self, - outfile, + outfile=None, title="My Map", width="100%", height="880px", add_layer_control=True, + **kwargs, ): """Saves the map as a HTML file. Args: - outfile (str): The output file path to the HTML file. + outfile (str, optional): The output file path to the HTML file. title (str, optional): The title of the HTML file. Defaults to 'My Map'. width (str, optional): The width of the map in pixels or percentage. Defaults to '100%'. height (str, optional): The height of the map in pixels. Defaults to '880px'. @@ -1255,14 +1256,17 @@ def to_html( """ try: - outfile = os.path.abspath(outfile) - if not outfile.endswith(".html"): - print("The output file must end with .html") - return - - out_dir = os.path.dirname(outfile) - if not os.path.exists(out_dir): - os.makedirs(out_dir) + save = True + if outfile is not None: + if not outfile.endswith(".html"): + raise ValueError("The output file extension must be html.") + outfile = os.path.abspath(outfile) + out_dir = os.path.dirname(outfile) + if not os.path.exists(out_dir): + os.makedirs(out_dir) + else: + outfile = os.path.abspath(random_string() + ".html") + save = False if add_layer_control and self.layer_control is None: layer_control = ipyleaflet.LayersControl(position="topright") @@ -1291,11 +1295,19 @@ def to_html( self.layout.width = width self.layout.height = height - self.save(outfile, title=title) + self.save(outfile, title=title, **kwargs) self.layout.width = before_width self.layout.height = before_height + if not save: + out_html = "" + with open(outfile) as f: + lines = f.readlines() + out_html = "".join(lines) + os.remove(outfile) + return out_html + except Exception as e: raise Exception(e) diff --git a/mkdocs.yml b/mkdocs.yml index 6287b4df2e..21c23af86c 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -78,3 +78,4 @@ nav: - notebooks/16_heremap.ipynb - notebooks/17_vector_tile_layer.ipynb - notebooks/18_point_layer.ipynb + - notebooks/19_map_to_html.ipynb