-
Notifications
You must be signed in to change notification settings - Fork 1.1k
How to embed map images in your notebooks
Note: You need ArcGIS API for Python version 1.6
and above for this. This version will release in March 2019.
- run your map cell as normal, add layers to it etc.
- right below your map cell, insert a new cell
- 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) - Now replace the screenshot cell's code with the code from the cell that created the map.
- 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:
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:
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
- run your map cell as normal, add layers to it etc.
- take a screen shot of the map output cell. If its a png, export it to jpg so its smaller in size
- turn your image into a
base-64
encoded string using this web tool: https://www.base64-image.de/ - 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
Inserting map images in notebooks
- In the cell that creates your map, copy the code and paste it in notepad.
- Then type the following
from IPython.display import Image
Image(url="<paste the long base64 encoded string here")
- Run the cell. Your image would be displayed.
- Now replace the image cell with the original code. See animation below
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!