gpt4 book ai didi

python - 如何在 Python 中实现在 Azure Jupyter Notebook 中打开的文件对话框?

转载 作者:太空宇宙 更新时间:2023-11-03 21:10:44 26 4
gpt4 key购买 nike

我的代码:

%gui qt

from PyQt5.QtWidgets import QFileDialog

def gui_fname(dir=None):
"""Select a file via a dialog and return the file name."""
if dir is None: dir ='./'
fname = QFileDialog.getOpenFileName(None, "Select data file...",
dir, filter="All files (*);; SM Files (*.sm)")
return fname[0]

from IPython.display import display
button = widgets.Button(description="Open The File !!")
button.style.button_color = 'yellow'
display(button)


def on_button_clicked(b):
print("Button clicked.")
f=gui_fname()
#import fileplot
#fileplot.load_file(f)



button.on_click(on_button_clicked)
#b1 = Button(description='Open File !!')
#b1.style.button_color = 'yellow'
#b1.on_click(on_button_clicked)
#b1

问题:在本地电脑上运行良好 python 版本2.7操作系统Linux

但是当我尝试在 Azure Jupyter Notebook 中远程实现它时,每次运行代码时内核都会死掉,并且我无法获得结果。有没有其他方法可以在 ipython 小部件中实现带有 html5 小部件的文件对话框??

我需要什么我需要用户选择他/她的电脑中本地可用的文件。1

enter image description here

最佳答案

实际上,您无法在 Azure Jupyter Notebook 上使用任何 native GUI API,因为 Azure Jupyter Notebook 在远程 Linux 服务器上运行,没有任何 GUI 支持,并且它是用于远程访问的 Web 应用程序。

因此,如果您想为客户端用户显示文件对话框,HTML 文件对话框是您唯一的选择。简单的解决方案是使用 IPython 模块的 displayHTML 类来显示 Choose File 按钮,请参阅代码和结果如下。

from IPython.display import display, HTML

display(HTML("""
<input type="file" name="myfile">
"""))

结果是:

enter image description here

对于更复杂的情况,可以引用这些博客IPython Notebook: Javascript/Python Bi-directional CommunicationReading files in JavaScript using the File APIs创建您自己的。但目前缺少接收和保存上传文件的服务,需要自行实现。或者,您可以简单地使用名为 fileupload( PyPIGitHub )的 IPython 现有项目,该项目是“使用 FileReader 上传文件的 IPython 笔记本小部件。”

关于python - 如何在 Python 中实现在 Azure Jupyter Notebook 中打开的文件对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55084232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com