简体   繁体   English

在jupyter笔记本幻灯片中渲染交互式绘图

[英]Render interactive plotly figure in jupyter notebook slideshow

I'm trying to embed an interactive plotly plot as a slide in a jupyter notebook slideshow. 我正在尝试在jupyter笔记本幻灯片中嵌入交互式绘图作为幻灯片。 I've tried to take the HTML output from plotly and embed it in a markdown slide, however nothing gets rendered. 我试图从plotly中获取HTML输出并将其嵌入markdown幻灯片中,但是什么也没有呈现。 I'm not sure what else to try. 我不确定还有什么尝试。 Other attempts to render HTML within a markdown slide usually work as expected so I'm not sure what I'm doing wrong here. 在markdown幻灯片中进行HTML呈现的其他尝试通常都可以按预期进行,因此我不确定自己在做什么错。

Here is an idea of the code I'm running: 这是我正在运行的代码的想法:

Cell 1 (code cell): 单元格1(代码单元格):

import numpy as np
import plotly
import plotly.graph_objs as pgo

m_t = 3.5
b_t = 4.0

x = np.linspace(0.0, 10.0)
y = m_t* x + b_t + np.random.normal(size=x.size)

Cell 2 (code cell): 单元格2(代码单元格):

plotly.offline.init_notebook_mode()

trace = pgo.Scatter(
    x=x,
    y=y,
    mode='markers'
)

fig = pgo.Figure(data=[trace])
x = plotly.offline.plot(fig, output_type='div', include_plotlyjs=False)

So now the HTML string is saved in variable x . 因此,现在HTML字符串保存在变量x Using either python markdown in the next slide such as 在下一张幻灯片中使用任一python markdown,例如

Cell 3 (markdown): 单元3(降价):

{{x}}

Or if I just get the value of x and copy and paste it into another markdown cell, it doesn't work, and it just renders a little blank space in the notebook. 或者,如果我只是获得x的值并将其复制并粘贴到另一个markdown单元中,则它将不起作用,并且只会在笔记本中呈现一些空白。

I would really like to get this working so I can include an interactive plot in the slide. 我真的很想让这个工作正常进行,因此我可以在幻灯片中包含一个交互式图。

I have been trying to do the same thing and I saw it worked here : 我一直在尝试做同样的事情,我发现它在这里起作用:

Checking my console I get : WARNING:tornado.access:404 GET /plotly.js which leads me to believe it can find the plotly.js library, maybe you could confirm the same behavior? 检查我的控制台,我得到: WARNING:tornado.access:404 GET /plotly.js ,这使我相信它可以找到plotly.js库,也许您可​​以确认相同的行为?

It would appear that you need to use plotly.offline.init_notebook_mode(connected=True) in every cell where you want a graph to appear, I believe this embeds the plotly.js library into that cell and in my case rendered the graph. 看来您需要在要显示图形的每个单元格中使用plotly.offline.init_notebook_mode(connected=True) ,我相信这会将plotly.js库嵌入到该单元格中,并且在我的情况下呈现了图形。

I am still exploring trying to do something similar to Damian avila for using a local copy of reveal and see if this is possible with plotly, http://www.damian.oquanta.info/posts/using-a-local-revealjs-library-with-your-ipython-slides.html 我仍在探索尝试使用本地显示的副本来做类似于Damian avila的操作,看看是否有可能通过密谋http://www.damian.oquanta.info/posts/using-a-local-revealjs- library-with-your-ipython-slides.html

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

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