社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

使用Python制作对开本工具提示包装文本

Adam • 3 年前 • 1820 次点击  

我正在使用Folium创建一个交互式传单地图。我的地图上有从屏幕上消失的工具提示。如何将文本框设置为换行?一个建议是创建一个独立的CSS文件,并以这种方式修改HTML输出,但我宁愿用Python来做。

我的代码和图片如下:

import folium
from folium.plugins import MarkerCluster

m = folium.Map(location=df[["lat", "lon"]].mean().to_list(), zoom_start=4)
title_html = '''
              <h3 align="center" style="font-size:16px"><b>{}</b></h3>
             '''.format(f'Russian Oligarch Real Estate Transactions/investments')

marker_cluster = MarkerCluster().add_to(m)

for i,r in df.iterrows():
    location = (r["lat"], r["lon"])
    info = (r['Name'],r['Full_Address'],r['Oligarch_Description'])
    info = list(info)
    new_line = '<br>'
    bold_start = '<strong>'
    bold_end = '</strong>'
    text = f'Name: {bold_start}{info[0]}{bold_end}{new_line} \
    Address: {bold_start}{info[1]}{bold_end}{new_line}Brief Bio: \
    {bold_start}{info[2]}{bold_end}'
    folium.Marker(location=location,
                      tooltip=text)\
    .add_to(marker_cluster)

m.get_root().html.add_child(folium.Element(title_html))

m

enter image description here

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131111
 
1820 次点击  
文章 [ 1 ]  |  最新文章 3 年前
furas
Reply   •   1 楼
furas    3 年前

我不知道这是否能解决问题,但可以使用Python中的字符串函数来包装它。

你可以用 <br> -所以每一个句子都是新的

text = text.replace(". ", ". <br>")

或者你可以分成50个字符的行,然后使用 <br>

import textwrap

desc = r['Oligarch_Description']

splited = textwrap.wrap(desc, 50)

desc = '<br>'.join(splitted)

print(desc)

设备截图 480x320 (使用Firefox) Ctrl+Shift+M )

enter image description here


完整的工作代码和代码中的示例数据。

它保存在文件中,并在浏览器中打开(我以普通脚本的形式运行它,而不是在浏览器中) Jupyter ).

import folium
from folium.plugins import MarkerCluster

import pandas as pd
import webbrowser
import textwrap

df = pd.DataFrame({
  'lat':[55],
  'lon': [38],
  'Name': ['Ivan'],
  'Full_Address': ['Moscow'],
  'Oligarch_Description': ["Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."],
})    

m = folium.Map(location=df[["lat", "lon"]].mean().to_list(), zoom_start=4)

title = 'Russian Oligarch Real Estate Transactions/investments'
title_html = '<h3 align="center" style="font-size:16px"><b>{}</b></h3>'.format(title)

marker_cluster = MarkerCluster().add_to(m)

for i, r in df.iterrows():
    location = (r["lat"], r["lon"])
    
    desc = r['Oligarch_Description']
    #desc = desc.replace('. ', '. <br>')
    desc = '<br>'.join(textwrap.wrap(desc, 50))
    print(desc)
    
    text = f"Name: <strong>{r['Name']}</strong></br> \
Address: <strong>{r['Full_Address']}</strong></br> \
Brief Bio:<br> \
<strong>{desc}</strong>"
    
    folium.Marker(location=location, tooltip=text).add_to(marker_cluster)

m.get_root().html.add_child(folium.Element(title_html))

m.save('folim.html')
webbrowser.open('folim.html')