使用b64encode在叶片弹出标记内显示图像

问题描述 投票:0回答:1

当用户单击叶片标记时,我试图显示图像

It will show like this

有一个来自jupyter的小教程可以做到这一点:tutorial

但是当我执行所有步骤时,它会向我显示如上图所示的卸载图像My result !!

我什至复制所有步骤:(

import folium
import numpy as np
import pandas as pd
import numpy.ma as ma

print('folium.__version__' , folium.__version__)

def make_data():
    x = np.linspace(-np.pi, np.pi, 101)
    sin = np.sin(x)
    cos = np.cos(x)
    cos[20:50] = np.NaN
    return pd.DataFrame(np.asanyarray([sin, cos]).T, columns=['sin', 'cos'], index=x)

df = make_data()
resolution, width, height = 75, 7, 3

station = '42'
lon, lat = -42, -21
m = folium.Map(location=[lat, lon], zoom_start=5)

import base64
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(width, height))
ax = df.plot(ax=ax, legend=False)
ax.set_ylabel('Sea surface height (m)')
png = 'mpld3_{}.png'.format(station)
fig.savefig(png, dpi=resolution)

encoded = base64.b64encode(open(png, 'rb').read())

from folium import IFrame

html = '<img src="data:image/png;base64,{}">'.format
iframe = IFrame(html(encoded), width=(width*resolution)+20, height=(height*resolution)+20)
popup = folium.Popup(iframe, max_width=2650)

icon = folium.Icon(color="red", icon="ok")
marker = folium.Marker(location=[lat-2, lon-1], popup=popup, icon=icon)
marker.add_to(m)

m
python python-3.x maps jupyter folium
1个回答
0
投票

使用folium-0.10.1,您只需要修改此行:

encoded = base64.b64encode(open(png, 'rb').read())

进入

encoded = base64.b64encode(open(png, 'rb').read()).decode()

您得到:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.