【问题标题】:How to import `displayHTML` in DataBricks?如何在 DataBricks 中导入“displayHTML”?
【发布时间】:2022-07-20 18:52:24
【问题描述】:

我创建了一个 python 包,它依赖于 IPython 在 Jupyter 或 Google Collab 等不同环境中的笔记本中显示 HTML。
在使用 DataBricks 进行测试时,我注意到它根本不起作用。
代码示例:

from IPython import display, HTML

my_html_str = '<img src="https://raw.githubusercontent.com/karolzak/boxdetect/master/images/checkboxes-details.jpg"/>'
display(HTML(my_html_str))

上面的代码可以在任何地方工作,但在 DataBricks 中除外。对于 DataBricks,我需要在其中一个单元格中运行 displayHTML(my_html_str) 以使其显示我的 HTML。
问题是我需要从我的 python 包代码级别运行displayHTML,当我在那里执行它时,它会引发一个错误,指出displayHTML 不存在。只有当我从 DB notebook 中的一个代码单元运行 displayHTML 时,它才会正确运行。
在我的包中显示 HTML 的代码:

if "DATABRICKS_RUNTIME_VERSION" in os.environ:
    displayHTML(html_viewer)
    return displayHTML(html)
else:
    from IPython import display, HTML
    display(HTML(html_viewer))
    return display(HTML(html))

使用上面的代码在 DataBricks 中运行时出现以下错误:

问题是我应该如何在我的包代码中导入displayHTML 以使其在 DB 中正常工作?

【问题讨论】:

    标签: python databricks azure-databricks


    【解决方案1】:

    在 Databricks 中,显示函数来自 PythonShell

    import PythonShell as pi
    pi.PythonShell.display(temps)
    

    这在 Databricks 中有效,但未列入白名单供外部使用。

    【讨论】:

      【解决方案2】:

      我不知道是否可以以标准方式导入displayHTML。显然,该函数是在 PythonShell 类中定义的,该类在 /databricks/python_shell/scripts/PythonShellImpl.py 中实例化和执行,因此无法从您的外部包中调用该函数。

      为了解决这个问题,我使用了inspect 模块(就像在databricks 中使用plotly 库一样)。这是一个适用于您的情况的代码 sn-p(当然,您可以在类中使用动态属性使其更漂亮 - 请参阅 https://github.com/plotly/plotly.py/blob/master/packages/python/plotly/plotly/io/_base_renderers.py 第 780 行以获取灵感):

      def display_html(html: str) -> None:
          """
          Use databricks displayHTML from an external package
          
          Args:
          - html : html document to display
          """
          import inspect
          for frame in inspect.getouterframes(inspect.currentframe()):
              global_names = set(frame.frame.f_globals)
              # Use multiple functions to reduce risk of mismatch
              if all(v in global_names for v in ["displayHTML", "display", "spark"]):
                  return frame.frame.f_globals["displayHTML"](html)
          raise EnvironmentError(
              "Unable to detect displayHTML function"
          )
      
      my_html_str = '<img src="https://raw.githubusercontent.com/karolzak/boxdetect/master/images/checkboxes-details.jpg"/>'
      display_html(my_html_str)
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:

      这是将所需包导入块的另一种方法。

      from IPython.core.display import display, HTML
      display(HTML("your content"))
      

      【讨论】:

      • 对不起,伙计,但我认为你没有理解我的问题。 IPython 显示在 DataBricks 环境中不起作用。我需要使用 DataBricks 函数 displayHTML 但它仅在直接在代码单元中调用时才有效。当我将它添加到我的包代码中时它不起作用。
      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-25
      • 2022-11-07
      • 1970-01-01
      • 2020-06-23
      相关资源
      最近更新 更多