生成global_earthquakes.html时无反应的解决方法

背景

在《Python编程从入门到实践》的16.2节中,在Pycharm中导入pandas模块后想要再生成global_earthquakes.html时可能会出现无反应的情况,但其实html文件已经处理好了,下面有两个解决方法:

解决方法

方法1:在点击运行后即可点击停止,随后找到生成的html文件,右键在浏览器打开即可

方法2(推荐):用代码在浏览器打开

先导入两个模块(参考我发的上一篇blog) PyCharm中用Python打开HTML文件-CSDN博客文章浏览阅读20次。方法1:使用webbrowser模块。方法2:使用subprocess模块。https://blog.csdn.net/2401_84561760/article/details/146113934?spm=1001.2014.3001.5501

import webbrowser
import os

然后将原文件的最后这样修改

fig.write_html('graph/global_earthquakes.html')
#注释该行
# fig.show()

#添加以下部分(路径根据个人修改)
edge_path = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
webbrowser.register('edge', None, webbrowser.BackgroundBrowser(edge_path))

html_file = os.path.abspath('graph/global_earthquakes.html')
url = f"file:///{html_file}"

webbrowser.get('edge').open(url)

        

你可能感兴趣的:(#,Matplotlib,html,数据库,前端)