跳转到主要内容

从Jupyter笔记本导出交互式HTML页面

项目描述

nbinteract

Read the Docs Gitter

Build Status PyPI npm

nbinteract是一个Python包,可以从Jupyter笔记本创建交互式网页。nbinteract还支持交互式绘图。这些交互由数据驱动,而非回调,使作者能够专注于程序的逻辑。

nbinteract最适用于

  • 想要创建简单的交互式博客文章,而不必了解/使用JavaScript的数据科学家。
  • 想要在教科书中包含交互式示例的教师。
  • 想要发布包含交互式演示的数据分析的学生。

目前,由于API快速变化,nbinteract处于alpha阶段。

示例

大多数来自其他库的绘图函数(例如matplotlib)以数据为输入。nbinteract的绘图函数以函数为输入。

import numpy as np
import nbinteract as nbi

def normal(mean, sd):
    '''Returns 1000 points drawn at random fron N(mean, sd)'''
    return np.random.normal(mean, sd, 1000)

# Pass in the `normal` function and let user change mean and sd.
# Whenever the user interacts with the sliders, the `normal` function
# is called and the returned data are plotted.
nbi.hist(normal, mean=(0, 10), sd=(0, 2.0), options=options)

example1

使用nbinteract创建模拟很容易。在这个模拟中,我们掷骰子并绘制滚动平均数。我们可以看到,随着掷骰子次数的增加,平均值越来越接近期望值:3.5。

rolls = np.random.choice([1, 2, 3, 4, 5, 6], size=300)
averages = np.cumsum(rolls) / np.arange(1, 301)

def x_vals(num_rolls):
    return range(num_rolls)

# The function to generate y-values gets called with the
# x-values as its first argument.
def y_vals(xs):
    return averages[:len(xs)]

nbi.line(x_vals, y_vals, num_rolls=(1, 300))

example2

发布

从笔记本单元

# Run in a notebook cell to convert the notebook into a publishable HTML page:
#
# nbi.publish('my_binder_spec', 'my_notebook.ipynb')
#
# Replace my_binder_spec with a Binder spec in the format
# {username}/{repo}/{branch} (e.g. SamLau95/nbinteract-image/master).
#
# Replace my_notebook.ipynb with the name of the notebook file to convert.
#
# Example:
nbi.publish('SamLau95/nbinteract-image/master', 'homepage.ipynb')

从命令行

# Run on the command line to convert the notebook into a publishable HTML page.
#
# nbinteract my_binder_spec my_notebook.ipynb
#
# Replace my_binder_spec with a Binder spec in the format
# {username}/{repo}/{branch} (e.g. SamLau95/nbinteract-image/master).
#
# Replace my_notebook.ipynb with the name of the notebook file to convert.
#
# Example:
nbinteract SamLau95/nbinteract-image/master homepage.ipynb

有关发布的更多信息,请参阅教程,其中包含将笔记本发布到网络上的完整教程。

安装

使用pip

pip install nbinteract

# The next two lines can be skipped for notebook version 5.3 and above
jupyter nbextension enable --py --sys-prefix widgetsnbextension
jupyter nbextension enable --py --sys-prefix bqplot

您现在可以在Python代码中导入nbinteract包,并使用nbinteract CLI命令将笔记本转换为HTML页面。

教程和文档

以下是本项目的教程和文档链接。

开发者安装

如果您想在本地上开发此项目,请运行以下操作

git clone https://github.com/SamLau95/nbinteract
cd nbinteract

# Installs the nbconvert exporter
pip install -e .

# To export a notebook to interactive HTML format:
jupyter nbconvert --to interact notebooks/Test.ipynb

pip install -U ipywidgets
jupyter nbextension enable --py --sys-prefix widgetsnbextension

brew install yarn
yarn install

# Start notebook and webpack servers
make -j2 serve

反馈

如果您有任何问题或建议,请在Gitter频道给我们留言。我们非常感谢您的反馈!

贡献者

nbinteract最初由Sam Lau和Caleb Siu作为加州大学伯克利分校的硕士项目开发,代码遵循BSD 3许可证,我们欢迎社区贡献和Pull请求。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

此版本没有可用的源分布文件。请参阅生成分布存档的教程

构建分布

nbinteract-0.2.6-py3-none-any.whl (29.6 kB 查看散列值)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面