python-visualization / branca

This library is a spinoff from folium, that would host the non-map-specific features.
https://python-visualization.github.io/branca/
MIT License
111 stars 63 forks source link

Getting `Make this Notebook Trusted to load map: File -> Trust Notebook` with latest version #81

Closed SebastianoF closed 1 year ago

SebastianoF commented 4 years ago

When running folium on streamlit I'm getting the error above, unless I do not downgrade to version 0.3.1. As the map is not visualised via a notebook, it is not possible to entrust it.

Is it possible to add a flag or global variable to bypass the message in the next version?

Conengmo commented 4 years ago

I'm not familiar with Streamlit. What do you see when you create a folium map in Streamlit? Just that message? Or does the map show?

SebastianoF commented 4 years ago

Thanks for your question. I see just the message in the place where the map is supposed to appear.

Conengmo commented 4 years ago

okay, so you don't see a map. The thing is that folium/branca requires Javascript to dehydrate the map code. Probably the execution of Javascript is blocked. In Jupyter notebooks that happens if the code is loaded from somewhere else and hasn't been marked as trusted. Maybe something similar happens in Streamlit?

Relevant code in branca: https://github.com/python-visualization/branca/blob/master/branca/element.py#L322

I found this related issue: https://github.com/streamlit/streamlit/issues/1514

Again, not familiar with Streamlit, but I'd be happy to cooperate to make this work.

SebastianoF commented 4 years ago

Thanks, yes, the issue you mentioned is spot on. Probably they have not tried to downgrade branca.

As for the code, I can not understand what is the connection between self.height being None and the trust of the Jupiter notebook. ...Nonetheless, I suspect branca thinks of being in a Jupiter notebook also when on streamlit and ask to push a non-existing Trust button.

If you can add a bypass to make branca think it is on a trusted notebook even when Trust button is not pushed, such as a flag or a global variable, to be set by the user, then I think this would solve the issue on streamlit too, and on any other platform that is not a Jupiter notebook where branca will run.

Conengmo commented 4 years ago

It hasn't to do with the height actually. It's this part here that loads the code: https://github.com/python-visualization/branca/blob/master/branca/element.py#L332 If that succeeds, the map wil be loaded on top of that error message. If loading the map fails, the error message stays visible.

Removing the error message will not make the map load alright. It's just a sign that loading the map failed.

branca cannot have influence on whether the code is trusted or not. Which makes sense from a security perspective.

Apparently the people at Streamlit are aware that additional work is needed to make folium work in Streamlit. Let's await that. If anything is needed on the branca or folium side we can help with that.

SebastianoF commented 4 years ago

@Conengmo Thanks for the answer!

Overlaying the map to an error message seems to be a feature that begs for a re-design. In particular since, as you are saying, the error message is not related to the height class attribute and it is nonetheless shown only if self.height is None.

branca cannot have influence on whether the code is trusted or not.

I agree, and this makes sense in theory! Though in practice the map is correctly visualised with branca==0.3.1 and not visualised with the latest version (same exact code, only changing branca version from one run to another).

This fact makes me think that the problem is not related to streamlit alone.

Please do let me know if you and branca team are planning to investigate further to change this feature, and if I can help in some way for future release.

Conengmo commented 4 years ago

You can read more about the change we made in v0.4.0 here: https://github.com/python-visualization/branca/pull/66. In short, we changed the way the map code is 'hydrated' and 'rehydrated'. Before we used a data URI, now we use a data attribute with JS to unpack it.

The self.height thing is a bit of a red herring I think, it has nothing to do with this. The warning message is shown as a courtesy to Jupyter users to remind them they have to trust the notebook for the data attribute to be unpacked.

If the maps don't load on Streamlit, this has nothing to do with self.height nor with the warning message. It's because the Javascript that unpacks the data-html attribute on the iframe doesn't execute.

There isn't really a branca team :) so there's no planning here. Volunteers are welcome. A good start is maybe to check why the onload event on the iframe doesn't trigger in Streamlit.

SebastianoF commented 4 years ago

OK, thanks for the explanation. Now it is clearer why I was getting the message!

Though not clear enough to know about where to put my hands to try to solve the issue. Do you have any idea why the new hydration logic would not work in streamlit (or why it was working before)?

chrenova commented 4 years ago

i ve got the same, i create the map in flask, then do the following to show the page with the map: map._repr_html_()

i do not use jupyter notebook of course, so there is no way to "trust notebook" afaik,

i got a few of those messages, but then the map started properly showing up after a few page reloads

SebastianoF commented 3 years ago

For who is looking for a solution, I found this one here: https://github.com/streamlit/streamlit/issues/1514

Pin the library branca to version 0.3.1 or

pip install branca==0.3.1 

I leave the issue open, as there should be a way to find a less tricky solution.

ChahedHassine commented 2 years ago

Try folium_static, I had the same problem and I resolved it by adding this library. https://github.com/randyzwitch/streamlit-folium

Conengmo commented 1 year ago

Since folium now has support in Streamlit with the streamlit-folium library, I'll close this issue.

https://github.com/randyzwitch/streamlit-folium

If other users see "Make this Notebook Trusted to load map" instead of the map they expected, please open a new issue.