Folium + Bokeh: Poor performance and massive memory usage
<p>I'm using Folium and Bokeh together in a Jupyter notebook.  I'm looping through a dataframe, and for each row inserting a marker on the Folium map, pulling some data from a separate dataframe, creating a Bokeh chart out of that data, and then embedding the Bokeh chart in the Folium map popup in an IFrame.  Code is as follows:</p>

<pre><code>map = folium.Map(location=[36.710021, 35.086146],zoom_start=6)

for i in range (0,len(duty_station_totals)):

    popup_table = station_dept_totals.loc[station_dept_totals['Duty Station'] == duty_station_totals.iloc[i,0]]

    chart = Bar(popup_table,label=CatAttr(columns=['Department / Program'],sort=False),values='dept_totals',
               title=duty_station_totals.iloc[i,0] + ' Staff',xlabel='Department / Program',ylabel='Staff',plot_width=350,plot_height=350)

    hover = HoverTool(point_policy='follow_mouse')
    hover.tooltips=[('Staff','@height'),('Department / Program','@{Department / Program}'),('Duty Station',duty_station_totals.iloc[i,0])]
    chart.add_tools(hover)

    html = file_html(chart, INLINE, "my plot")

    iframe = folium.element.IFrame(html=html, width=400, height=400)

    popup = folium.Popup(iframe, max_width=400)

    marker = folium.CircleMarker(duty_station_totals.iloc[i,2],
                                radius=duty_station_totals.iloc[i,1] * 150,
                                color=duty_station_totals.iloc[i,3],
                                fill_color=duty_station_totals.iloc[i,3])

    marker.add_to(map)

    folium.Marker(duty_station_totals.iloc[i,2],icon=folium.Icon(color='black',icon_color=duty_station_totals.iloc[i,3]),popup=popup).add_to(map)


map
</code></pre>

<p>This loop runs extremely slowly, and adds approx. 200mb to the memory usage of the associated python 3.5 process, per run of the loop!  In fact, after running the loop a couple times my entire macbook is slowing down to a crawl - even the mouse is lagging.  The associated map also lags heavily when scrolling and zooming, and the popups are slow to open.  In case it isn't obvious, I'm pretty new to the python analytics and web visualization world so maybe there is something clearly very inefficient here.  </p>

<p>I'm wondering why this is and if there is a better way of having Bokeh charts appear in the map popups.  From some basic experiments I've done, it doesn't seem that the issue is with the calls to <code>Bar</code> - the memory usage seems to really skyrocket when I include calls to <code>file_html</code> and just get worse as calls to <code>folium.element.IFrame</code> are added.  Seems like there is some sort of memory leak going on due to the increasing memory usage on re-running of the same code.  </p>

<p>If anyone has ideas as to how to achieve the same effect (Bokeh charts opening when clicking a Folium marker) in a more efficient manner I would really appreciate it!</p>

<p><strong>Update following some experimentation</strong></p>

<p>I've run through the loop step by step and observed changes in memory usage as more steps are added in to try and isolate what piece of code is driving this issue.  On the Bokeh side, the biggest culprit seems to be the calls to <code>file_html()</code> - when running the loop through this step it adds about 5mb of memory usage to the associated python 3.5 process per run (the loop is creating 18 charts), even when including <code>bokeh.io.curdoc().clear()</code>.</p>

<p>The bigger issue, however, seems to be driven by Folium.  running the whole loop including the creation of the Folium IFrames with the Bokeh-generated HTML and the map markers linked to the IFrames adds 25-30mb to the memory usage of the Python process per run.</p>

<p>So, I guess this is turning in to more of a Folium question.  Why is this structure so memory intensive and is there a better way?  By the way, saving the resulting Folium map as an HTML file with <code>map.save('map.html')</code> creates a huge, 22mb HTML file.    </p>