简体   繁体   English

保存显示 Plotly Express 小部件的 Jupyter Notebook

[英]Save Jupyter Notebook with Plotly Express widgets displaying

I have a Jupyter notebook (python) where I used plotly express to plot in the notebook for analysis purposes.我有一个 Jupyter 笔记本(python),我使用 plotly express 在笔记本中绘图以进行分析。 I want to share this notebook with non-coders, and have the interactive visuals be available still - but it does not seem to work.我想与非编码人员共享这个笔记本,并且仍然可以使用交互式视觉效果 - 但它似乎不起作用。

I tried following recommendations made here but even after saving widgets state and using nbconvert , when I open the new HTML file, the visuals are not available.我尝试遵循此处提出的建议,但即使在保存小部件状态并使用nbconvert ,当我打开新的 HTML 文件时,视觉效果也不可用。

A sample line of plotting can be seen below:绘制的示例线如下所示:

import plotly_express as px
fig = px.scatter(
    df, 
    x='size', 
    y='size_y', 
    color='clients',
    hover_data=['id'], 
    marginal_y="histogram", 
    marginal_x="histogram"
)
fig.show()

在单元格中运行plotly.offline.init_notebook_mode()后,您可以通过文件菜单导出具有完全交互性的笔记本: File --> Export Notebook as... --> Export Notebook to HTML

I was having similar issues but with JupyterLab.我遇到了类似的问题,但使用 JupyterLab。 Followed the instructions here: https://plot.ly/python/renderers/ .按照此处的说明进行操作: https : //plot.ly/python/renderers/

import plotly.io as pio
pio.renderers.keys()

I had to add the following snippet to my script:我必须将以下代码段添加到我的脚本中:

import plotly.io as pio
pio.renderers.default = 'jupyterlab'

Exporting as HTML after that worked for me.之后导出为 HTML 对我有用。 You can read "Overriding the default renderer".您可以阅读“覆盖默认渲染器”。

I suppose you would need我想你需要

pio.renderers.default = 'notebook' 

You can specify the default renderers from plotly with:您可以使用以下命令从 plotly 指定默认渲染器:

    import plotly.io as pio
    pio.renderers.default = 'pdf'

or when displaying the images with:或显示图像时:

    fig.show(renderer="pdf")

The 2 choices for you are:您的 2 个选择是:

  • 'notebook': work well with jupyter notebook; 'notebook':与 jupyter notebook 配合使用;
  • 'pdf': perfect when using nbconvert to convert to HTML or LATEX 'pdf':使用nbconvert转换为 HTML 或 LATEX 时完美

You can also join the 2 with "notebook+pdf" so you have iterative plots when running the notebook and static images when converting with nbconvert.您还可以使用“notebook+pdf”加入 2,以便在运行笔记本时获得迭代图,并在使用 nbconvert 进行转换时获得静态图像。

I agree with @hamiq.我同意@hamiq。 With the new version of plotly and Dash, it's a lot easier to use both inside JupyterLab and to work with interactive plots.使用新版本的 plotly 和 Dash,在 JupyterLab 中使用和处理交互式绘图变得更加容易。 Here's a link for a tutorial I made that explains how to get started with JupyterLab and JupyterDash.这是我制作的教程的链接,该教程解释了如何开始使用 JupyterLab 和 JupyterDash。 https://youtu.be/QkOKkrKqI-k https://youtu.be/QkOKkrKqI-k

I hope this helps everyone with the installation and the set-up.我希望这可以帮助每个人进行安装和设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM