Unable to insert altair visualisation as popup in folium map

Viewed 1230

I'm relatively new to python and this is the first project that I'm working on. I'm trying to plot a simple multi line chart using altair onto a folium marker's popup. I followed the examples on this page https://github.com/python-visualization/folium/blob/master/examples/Popups.ipynb. But when I click on the marker, it's just plain white. The map and markers are plotted fine except the popup. This is what my whole dataset looks like :

enter image description here

Here is the code:

import pandas as pd
import folium
import altair as alt
import json

chart = alt.Chart(df).mark_line().encode(
        x='index',
        y='Ambala',
        color='Variable')

chart_2 = json.loads(chart.to_json())
m = folium.Map([30,-80],zoom_start=2)
popup = folium.Popup(max_width=650)
folium.Vega(chart_2, height=350, width=650).add_to(popup)
folium.Marker([30, -80], popup=popup).add_to(m)
m

Note that this just a sample of code. I'll be implementing this on a larger scale in my project. I tried to plot a visualization (which I found here : https://github.com/python-visualization/folium/blob/master/examples/data/vis1.json) and this seemed to work fine. I just don't understand why the same code works for their plot but not for mine even though altair produces a fine looking chart from my code. Sorry if this is sounds silly, I'm still a beginner and python is my first language. Thanks

1 Answers

If you want to use an Altair chart inside a folium map, you need to use a VegaLite object instead of a Vega object. You just need to change the line with the Vega object like that : folium.features.VegaLite(chart_2, height=350, width=650).add_to(popup)folium map

Related