Jupyter Notebook中Ipywidget文件选择框无法打开的问题
问题描述
我在Jupyter Notebook中运行leafmap,尝试添加一个文件选择器以打开选择本地文件的部件,单元格中的代码如下:
import ipywidgets as widgets from ipyfilechooser import FileChooser import os padding = "0px 0px 0px 5px" style = {"description_width": "initial"} tool_output = widgets.Output( layout=widgets.Layout(max_height="150px", max_width="500px", overflow="auto") ) file_type = widgets.ToggleButtons( options=["GeoTIFF", "COG", "STAC", "Microsoft"], tooltips=[ "Open a local GeoTIFF file", "Open a remote COG file", "Open a remote STAC item", "Create COG from Microsoft Planetary Computer", ], ) file_type.style.button_width = "110px" file_chooser = FileChooser( os.getcwd(), sandbox_path=m.sandbox_path, layout=widgets.Layout(width="454px") ) file_chooser.filter_pattern = ["*.tif", "*.tiff"] file_chooser.use_dir_icons = True source_widget = widgets.VBox([file_chooser])
运行该单元格后,没有弹出选择框,请问我哪里操作出错了?
解决方案
你没有将创建好的source_widget部件触发渲染,这是Jupyter Notebook中使用ipywidgets的常见疏漏——定义了部件但没有让它显示出来。
可以通过两种方式解决:
- 在单元格末尾单独添加一行
source_widget,让Jupyter自动渲染该部件:source_widget - 导入并使用
display()函数显式渲染:from IPython.display import display display(source_widget)
另外注意代码中的m.sandbox_path,如果m是未初始化的leafmap对象,会引发错误。需要确保已提前创建leafmap实例(比如m = leafmap.Map()),或者移除sandbox_path参数、替换为合法的本地路径。
内容的提问来源于stack exchange,提问作者user1592380
相关产品推荐
相关产品推荐

