Google可视化库Facets:如何离线保存python模块Facets输出html到本地驱动器?

2024-06-05 23:54:32 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我发现了python可视化库“Facets”,并想知道是否可以脱机生成html输出。你知道吗

  • 我使用的是chrome浏览器,不需要webcomponents-lite.js。你知道吗
  • 另外,我在MacOS中使用自制软件安装了bazel。你知道吗
  • 我已经将“facets dist”文件夹从facets official repo复制到jupyter path~/.ipython/nbextensions/

有用的链接:
-https://github.com/PAIR-code/facets -https://colab.research.google.com/github/PAIR-code/facets/blob/master/colab_facets.ipynb

我的尝试:

import numpy as np
import pandas as pd
from IPython.core.display import display, HTML


df = pd.DataFrame({'a':[10,20],'b':[100,200]})
jsonstr = df.to_json(orient='records')

HTML_TEMPLATE = """<link rel="import" href="/nbextensions/facets-dist/facets-jupyter.html">
        <facets-dive id="elem" height="600"></facets-dive>
        <script>
          var data = {jsonstr};
          document.querySelector("#elem").data = data;
        </script>"""

html_str = HTML_TEMPLATE.format(jsonstr=jsonstr)

# write the html to a file
with open('output.html', 'w') as f:
    f.write(html_str)
    #f.write(HTML(html_str).data)

!open output.html

它创造了一些输出.html文件,但不显示任何可视化。如何创建正确的输出html?你知道吗


Tags: httpsimportgithubdata可视化disthtmlas