简体   繁体   English

在 Jupyter Notebook 中嵌入幻灯片

[英]Embedding slideshow in Jupyter Notebook

I want a pythonic way to embed a slideshow in a jupyter notebook cell similar to the slideshow here:我想要一种 pythonic 方式在类似于幻灯片的 jupyter 笔记本单元格中嵌入幻灯片:

https://www.w3schools.com/howto/howto_js_slideshow.asp https://www.w3schools.com/howto/howto_js_slideshow.asp

I don't need any CSS styling, and I don't need any buttons other than the next/previous slide arrows.我不需要任何 CSS 样式,除了下一个/上一个幻灯片箭头之外,我不需要任何按钮。

I don't want to turn the entire notebook into a slideshow, only a single cell, so I can't use reveal.js.我不想把整个笔记本变成幻灯片,只有一个单元格,所以我不能使用reveal.js。

I find it very difficult to believe that there is no simple way to embed a slideshow in a notebook considering all of these other interactive widgets exist:考虑到所有这些其他交互式小部件都存在,我发现很难相信没有简单的方法可以在笔记本中嵌入幻灯片:

http://jupyter.org/widgets http://jupyter.org/widgets

Is there really no easy way to do this?真的没有简单的方法可以做到这一点吗?

http://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html http://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html

It turns out an IntSlider, once it has been focused (clicked on), can be incremented to the next / previous slider value with the arrow keys, which is good enough for my purposes. 事实证明,一旦将IntSlider聚焦(单击),就可以使用箭头键将其递增到下一个/上一个滑块值,这对我来说已经足够了。 Here's an example of how I'm using the slider widget: 这是我如何使用滑块小部件的示例:

import ipywidgets as wg
from IPython.display import SVG

def f(x):
    if x == 1:
        return SVG(filename='./path/to/pic1')
    elif x == 2:
        return SVG(filename='./path/to/pic2')
    elif x == 3:
        return SVG(filename='./path/to/pic3')
    else:
        return SVG(filename='./path/to/pic4')

wg.interact(f, x=wg.IntSlider(min=1,max=4,step=1));

I had the same issue but could not use ipywidgets (the notebook is eventually converted to html via fastpages ).我遇到了同样的问题,但无法使用 ipywidgets(笔记本最终通过fastpages转换为 html)。 I found an ok way by creating an xarray with all the images, and then using plotly.js animation features.我通过创建一个包含所有图像的 xarray,然后使用 plotly.js 动画功能找到了一个不错的方法。 It adds a slider that makes it easy to switch between the images.它添加了一个滑块,可以轻松地在图像之间切换。 Here is an example:下面是一个例子:

import numpy as np
import plotly.express as px
from imageio import imread
import xarray as xr

# Show a list of numpy images as a carousel
# key is the label of the slider
def show_images_carousel(images, labels, key: str, title: str, height:int):        
    stacked = np.stack(images, axis=0)
    xrData = xr.DataArray(
        data   = stacked,
        dims   = [key, 'row', 'col', 'rgb'],
        coords = {key: labels}
    )
    # Hide the axes
    layout_dict = dict(yaxis_visible=False, yaxis_showticklabels=False, xaxis_visible=False, xaxis_showticklabels=False)
    return px.imshow(xrData, title=title, animation_frame=key).update_layout(layout_dict)

# Show a list of URLs as a carousel, loading then as numpy images first
def show_images_carousel_from_urls(image_urls, labels, key: str, title:str, height:int):
    images = [imread(url, pilmode='RGB') for url in image_urls]
    return show_images_carousel(images, labels, key, title, height)

Usage is then something like:用法是这样的:

images = {
    "simulation_images/rgbspan.png": 'Original',
    "simulation_images/rgbspan_protan_brettel1997.png": "Brettel 1997",
    "simulation_images/rgbspan_protan_vienot1999.png": "Viénot 1999",
    "simulation_images/rgbspan_protan_machado2009.png": 'Machado 2009',    
    "simulation_images/rgbspan_protan_coblisv1.png": "Coblis V1",
    "simulation_images/rgbspan_protan_coblisv2.png": "Coblis V2" 
}

fig = show_images_carousel_from_urls (images.keys(), list(images.values()), 'Method', None, 700)
fig.show()

输出示例

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

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