Python数据仪表盘创建报错求助:排查第7、29、141行格式问题
解决Panel仪表盘PNG加载报错:PNG pane cannot parse string that is not a filename or URL
问题描述
运行基于Panel的FastListTemplate仪表盘时触发ValueError,报错提示PNG面板无法解析非文件名/URL的字符串,错误指向代码中加载climate_day.png的行。
报错信息
ValueError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_1648\3613514742.py in <module> 5 sidebar=[pn.pane.Markdown("# CO2 Emissions and Climate Change"), 6 pn.pane.Markdown("#### Carbon dioxide emissions are the primary driver of global climate change. It’s widely recognised that to avoid the worst impacts of climate change, the world needs to urgently reduce emissions. But, how this responsibility is shared between regions, countries, and individuals has been an endless point of contention in international discussions."), ----> 7 pn.pane.PNG('climate_day.png', sizing_mode='scale_both'), 8 pn.pane.Markdown("## Settings"), 9 year_slider], ~\Anaconda3\lib\site-packages\panel\pane\image.py in __init__(self, object, **params) 27 if isinstance(object, PurePath): 28 object = str(object) ---> 29 super().__init__(object=object, **params) 30 31 def _type_error(self, object): ~\Anaconda3\lib\site-packages\panel\pane\base.py in __init__(self, object, **params) 139 applies = self.applies(object, **(params if self._applies_kw else {})) 140 if (isinstance(applies, bool) and not applies) and object is not None : ---> 141 self._type_error(object) 142 143 super().__init__(object=object, **params) ~\Anaconda3\lib\site-packages\panel\pane\image.py in _type_error(self, object) 31 def _type_error(self, object): 32 if isinstance(object, str): ---> 33 raise ValueError("%s pane cannot parse string that is not a filename " 34 "or URL." % type(self).__name__) 35 super()._type_error(object) ValueError: PNG pane cannot parse string that is not a filename or URL.
完整代码
template = pn.template.FastListTemplate( title='World CO2 emission dashboard', sidebar=[pn.pane.Markdown("# CO2 Emissions and Climate Change"), pn.pane.Markdown("#### Carbon dioxide emissions are the primary driver of global climate change. It’s widely recognised that to avoid the worst impacts of climate change, the world needs to urgently reduce emissions. But, how this responsibility is shared between regions, countries, and individuals has been an endless point of contention in international discussions."), pn.pane.PNG('climate_day.png', sizing_mode='scale_both'), pn.pane.Markdown("## Settings"), year_slider], main=[pn.Row(pn.Column(yaxis_co2, co2_plot.panel(width=700), margin=(0,25)), co2_table.panel(width=500)), pn.Row(pn.Column(co2_vs_gdp_scatterplot.panel(width=600), margin=(0,25)), pn.Column(yaxis_co2_source, co2_source_bar_plot.panel(width=600)))], accent_base_color="#88d8b0", header_background="#88d8b0", ) # template.show() template.servable();
报错原因
核心问题是Panel的PNG面板无法找到指定的climate_day.png文件,可能的诱因包括:
- 图片文件不在程序运行的当前工作目录
- 文件名拼写错误(含大小写、后缀名不一致)
- 图片文件已损坏或未存在
解决方案
1. 确认文件路径
- 先打印当前工作目录,在代码开头添加:
将import os print(os.getcwd()) # 输出程序运行的目录路径climate_day.png复制到该目录下,再重新运行代码。 - 或者直接使用图片的绝对路径,例如:
# Windows系统示例 pn.pane.PNG('C:/Users/YourUsername/Documents/climate_day.png', sizing_mode='scale_both') # Linux/macOS系统示例 pn.pane.PNG('/home/yourusername/Documents/climate_day.png', sizing_mode='scale_both')
2. 校验文件名
确保代码中的文件名与实际文件完全一致,比如不要写成Climate_Day.png(大小写错误)或climate_day.jpg(后缀错误)。
3. 验证文件有效性
打开climate_day.png确认文件未损坏,能正常显示。
内容的提问来源于stack exchange,提问作者Gser
相关产品推荐
相关产品推荐

