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

标签 python azure jupyter-notebook jupyter-kernel azure-notebooks

我的代码:

%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/

相关文章:

python - 比较 pandas 数据框中的两列以创建第三列

azure - Terraform - 部署到多个azure订阅

python-3.x - 从 jupyter 实验室复制 matlibplot 绘图图像

python - JSON 文件不读取 pandas

python - Python 3.Beginner与多重错误处理相混淆。尝试并除外

python - 在python中按键/id划分行中的值

c# - 我可以更改 Windows Azure 网站上的区域设置吗?

azure - 如何在 Microsoft Azure 上部署简单的静态微型网站

python-3.x - 如何在 keras 模型中可视化学习到的训练权重?

python - 如何检测子列表中的逻辑和字符串索引并将其删除