python - Python:运行应用程序时如何在浏览器中打开用folium创建的地图
问题描述
我有一个使用 python3 用 PyCharm 编写的非常小的应用程序:
import folium
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save('map2.html')
这将创建一个map2.html
我可以使用 pycharm 在我的浏览器中访问的,并且 url 看起来像:http://localhost:63342/iss-country/map2.html?_ijt=dcsefdg8om4ddfovlt5ooq6ro5
如何在浏览器中自动打开它?因此,当我运行应用程序时,它不仅会生成 html 页面,还会立即访问它。我找到了webbrowser
有用的模块,但我怎么知道正确的 localhost url?
解决方案
我没有看到使用webbrowser模块的问题。只需将文件名和路径设为变量并调用 webbrowser open 方法。
output_file = "map2.html"
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save(output_file)
webbrowser.open(output_file, new=2) # open in new tab
推荐阅读
- html - 标签背景颜色 CSS 在 Safari 中不起作用
- okhttp - 无中介的 OkHttp 客户端证书
- css - 将像素值添加到 CSS 百分比变量
- javascript - 从 vanilla javascript 到 react hooks
- php - 使刷新页面功能每个会话只运行一次
- python-3.x - 如何使用熊猫将行与平均值合并
- c++ - 保存函数参数并稍后调用函数的最佳方法是什么?
- python - 如何取消数组第 24 和第 25 成员之间的计数
- fonts - idDelta 和 idRangeOffset 在 TrueType 字体中都可以不为零吗?
- google-sheets - 如何确定一个单元格是否在 Google 表格的特定行范围内?