Skip to content

Adding IFrame or Script Capability to GeoJsonPopup #1362

Closed

Description

@Tyllis0

Currently you can embed a IFrame to Popup. This opens up a lot of possibilities for the popups. For example, you can create interactive visualizations via libraries such as Plotly, retrieve the html code and embed in a IFrame, which can then be added to the map as popups.

This capability does not seem to work with GeoJsonPopup. GeoJsonPopup seems to only read plain text or plain html. Is it possible to have GeoJsonPopup recognize IFrame object or html with javascript in the "fields" of geojson "properties"?

Activity

  1. joaopalotti commented on Sep 7, 2020

    @joaopalotti

    I am currently with the same question. Any updates @junyan0?

  2. Tyllis commented on Sep 8, 2020

    @Tyllis

    I found a work around with Plotly. Use plotly.offline.plot function to turn the interactive plot into html. Then you stuff that into this function:

    def plot_to_iframe(plot, width, height):
    html_string = '''


    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>


    ''' + plot + '''

    '''
    iframe = folium.IFrame(html_string, width=width, height=height)
    return iframe

    Then you can turn that iframe object into a folium popup: folium.Popup(iframe), which can be attached to the map.

  3. joaopalotti commented on Sep 8, 2020

    @joaopalotti

    Hi Tyllis,

    Thanks for your quick reply! What I was curious about was how to use GeoJsonPopup with custom HTML.
    I think what you are describing is swapping GeoJsonPopoup with Popup, but I am not sure if you would be able to have multiple popups, e.g., one for each location of your GeoJson.

    After some try-and-errors, I think I found a solution to my problem that worked well.
    My code is heavily based on this folium tutorial notebook, but instead of only showing a given dataframe column as a pre-defined HTML table, as the tutorial does, I created a new col with my HTML code and displayed only it.
    I am sharing some snippets below to help whoever has the same problem in the future:

    #  .... some code to get the df as in the tutorial
    
    def getHTML(row):
       return "<b> Place: %s </b>" % row["name"]
    
    df["html_to_display"] = df.apply(getHTML, axis=1)
    
    # ...
    popup = GeoJsonPopup(
       # Replaced fields and aliases with my own 'html_to_dsplay' 
       #fields=["name", "medianincome", "change"],
        #aliases=["State:", "2015 Median Income(USD):", "Median % Change:"],
        #labels=True,
        labels= False, fields = ["html_to_display"],
        localize=True,
        style="background-color: yellow;",
    )
    
    g = folium.GeoJson(
        df,
        popup=popup
    ).add_to(m)
  4. tarynblack commented on Sep 8, 2021

    @tarynblack

    @joaopalotti this was exactly what I needed, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions