Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export / save Folium map as static image (PNG) #1850

Closed
FlorinAndrei opened this issue Dec 22, 2023 · 5 comments
Closed

export / save Folium map as static image (PNG) #1850

FlorinAndrei opened this issue Dec 22, 2023 · 5 comments

Comments

@FlorinAndrei
Copy link

Code:

colormap = branca.colormap.linear.plasma.scale(vmin, vmax).to_step(100)
r_map = folium.Map(location=[lat, long], tiles='openstreetmap')
for i in range(0, len(df)):
    r_lat = ...
    r_long = ...
    r_score = ...
    Circle(location=[r_lat, r_long], radius=5, color=colormap(r_score)).add_to(r_map)
colormap.add_to(r_map)
r_map

This works fine, but there seems to be no way to generate the map, optionally, as a fixed-size, non-zoomable bitmap in a decent format like PNG.

A side-effect of this is - the map does not show in a PDF export of the Jupyter notebook where the map is generated.

It would be nice if folium.Map() had a way to generate fixed bitmap output, e.g. like Matplotlib.pyplot.

I've read the documentation, searched the web, there really seems to be no good solution other than a Selenium hack which requires too many moving parts and extra libraries. This should be instead a standard Folium feature.

To be clear, I am running all this code in a Jupyter notebook.

I don't think I have the time to implement a PR myself.

@Conengmo
Copy link
Member

Hi Florin,

You're right that converting a map to a fixed image is not supported by default. That's because Folium only creates Javascript code. Then your browser runs that Javascript to display a map. Python is not involved in that last part, only your browser.

Perhaps a Leaflet plugin to export a map to an image would be useful: https://leafletjs.com/plugins.html#printexport. We don't have any of these in Folium at the moment.

I don't think Selenium is a 'hack', but you're right that setting it up is not as straight forward as just doing pip install. Perhaps something like SeleniumBase, which automates driver installation, could be useful.

I don't think we should have Selenium as a required dependency for Folium. I do think our documentation could contain a better example on how to use Selenium to create a screenshot. Help is welcome for that.

If somebody has ideas on how Folium could provide better support for creating screenshots, I'd love to hear it. For now, I'll close this issue, since you indicate you don't have time to help with this, and I don't plan on working on this topic. Sorry that you seem disappointed, I hope you can appreciate we don't really have a core team working on this, it's all volunteering with very limited people.

@Conengmo Conengmo closed this as not planned Won't fix, can't repro, duplicate, stale Dec 24, 2023
@prusswan
Copy link
Contributor

What might be helpful is a way for folium to export the map object as a self-contained HTML/JS output, which the user can further modify based on their knowledge of Leaflet and JS etc. This functionality would also be useful for the purpose of using folium in offline mode.

@Conengmo
Copy link
Member

@prusswan thanks for your suggestion. How would what you describe be different from using Map().save()? That also creates a self-contained HTML/JS output.

If you want offline mode, you also need to host external resources and especially tiles locally, the latter of which is more involved.

@prusswan
Copy link
Contributor

prusswan commented Jan 18, 2024

@Conengmo probably by extending Map().save() with option to specify default_js, default_css, and explore how ipyleaflet bundles/inlines the JS/CSS. bokeh also has similar functionality.

If you want offline mode, you also need to host external resources and especially tiles locally, the latter of which is more involved.

PMTiles will be pretty useful for this, but if you have access to local tiles, it is fairly easy to set up everything to work under file:// (granted, this is only workable for raster images which do not require CORS to work). I have been using a modified version of @and-viceversa 's solution in #351, which might have turned into a plugin if there was more interest.

@kburchfiel
Copy link

The Plotly graphing library uses kaleido to easily save .png versions of interactive charts. I believe these charts are originally rendered within HTML and javascript by default, so perhaps Folium could use a similar setup?

That being said, I find that using Selenium to save static versions of Folium charts works quite well once you get everything set up (which seems to be easier now than in the past due to updates to the Selenium library).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants