Skip to content

How to embed map images in your notebooks

Atma Mani edited this page Feb 1, 2019 · 1 revision

Prescribed method:

Note: You need ArcGIS API for Python version 1.6 and above for this. This version will release in March 2019.

  1. run your map cell as normal, add layers to it etc.
  2. right below your map cell, insert a new cell
  3. in the new cell type map_obj.take_screenshot(). This will print your map into a png and embed that as a base64 encoded string. This image gets embedded as an output to that new cell (similar to how matplotlib plots get embedded)
  4. Now replace the screenshot cell's code with the code from the cell that created the map.
  5. Remove the cell that created the map.

Now when you share the notebook as is, or export it to HTML, your map stays embedded. When your users run the notebook, the map cell will dynamically remove the embedded screenshot and produce a live map widget. It happens seamlessly to the user.

Below is an animation showing the above steps: embedding_maps_take_screenshot

If for some reason this does not work for you, or if you are using an older version of Python API, follow this alternate approach:

Alternate method:

Below is a wiki to help you embed your maps in your notebooks such that, when you export as html, the maps are present.

Converting your images to base64

  1. run your map cell as normal, add layers to it etc.
  2. take a screen shot of the map output cell. If its a png, export it to jpg so its smaller in size
  3. turn your image into a base-64 encoded string using this web tool: https://www.base64-image.de/
  4. click on "show code". In the pop up window, click "copy to clipboard" on the first option that presents <img> html tag code. Copy this to a notepad. See animation below image2base64

Inserting map images in notebooks

  1. In the cell that creates your map, copy the code and paste it in notepad.
  2. Then type the following
from IPython.display import Image
Image(url="<paste the long base64 encoded string here")
  1. Run the cell. Your image would be displayed.
  2. Now replace the image cell with the original code. See animation below replace_map_base64

Now export your notebook to html. Your maps will stay embedded. Next time, when user runs the notebook, the screenshot of the map will be dynamically replaced with the actual interactive map with the user knowing it. Congrats!