跳转到主要内容

JupyterLab - Vega 3和Vega-Lite 2 MIME渲染扩展

项目描述

jupyterlab-vega3

A JupyterLab extension for rendering Vega 3 and Vega-Lite 2

Vega 3已弃用。最新版本默认包含JupyterLab。只有当您有与最新版本不兼容的规范时才使用此扩展。

demo

需求

  • JupyterLab >= 3.0

安装

pip install jupyterlab-vega3

用法

要在IPython中渲染Vega-Lite输出

from IPython.display import display

display({
    "application/vnd.vegalite.v2+json": {
        "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
        "description": "A simple bar chart with embedded data.",
        "data": {
            "values": [
                {"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
                {"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
                {"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
            ]
        },
        "mark": "bar",
        "encoding": {
            "x": {"field": "a", "type": "ordinal"},
            "y": {"field": "b", "type": "quantitative"}
        }
    }
}, raw=True)

使用altair库

import altair as alt

cars = alt.load_dataset('cars')

chart = alt.Chart(cars).mark_point().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
)

chart

通过元数据提供vega-embed选项

from IPython.display import display

display({
    "application/vnd.vegalite.v2+json": {
        "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
        "description": "A simple bar chart with embedded data.",
        "data": {
            "values": [
                {"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
                {"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
                {"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
            ]
        },
        "mark": "bar",
        "encoding": {
            "x": {"field": "a", "type": "ordinal"},
            "y": {"field": "b", "type": "quantitative"}
        }
    }
}, metadata={
    "application/vnd.vegalite.v2+json": {
        "embed_options": {
            "actions": False
        }
    }
}, raw=True)

通过altair提供vega-embed选项

import altair as alt

alt.renderers.enable('default', embed_options={'actions': False})

cars = alt.load_dataset('cars')

chart = alt.Chart(cars).mark_point().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
)

chart

要渲染.vl.vgvl.json.vg.json文件,只需打开它

贡献

开发安装

注意:您需要NodeJS来构建扩展包。

jlpm命令是JupyterLab附带并安装的yarn的固定版本。您可以使用下面的yarnnpm代替jlpm

# Clone the repo to your local environment
# Change directory to the jupyterlab-vega3 directory
# Install package in development mode
pip install -e .
# Link your development version of the extension with JupyterLab
jupyter labextension develop . --overwrite
# Rebuild extension Typescript source after making changes
jlpm run build

您可以在不同的终端同时监视源目录并运行JupyterLab,以监视扩展源的变化并自动重新构建扩展。

# Watch the source directory in one terminal, automatically rebuilding when needed
jlpm run watch
# Run JupyterLab in another terminal
jupyter lab

在运行监视命令时,每次保存的更改都将立即在本地上构建并提供给您运行的JupyterLab。刷新JupyterLab以在浏览器中加载更改(您可能需要等待几秒钟以重建扩展)。

默认情况下,jlpm run build 命令会为此扩展生成源映射,以便使用浏览器开发者工具更容易地进行调试。要同时为 JupyterLab 核心扩展生成源映射,可以运行以下命令

jupyter lab build --minimize=False

卸载

pip uninstall jupyterlab-vega3

项目详情


下载文件

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

源分布

jupyterlab_vega3-3.3.0.tar.gz (279.1 kB 查看哈希值)

上传时间

构建分布

jupyterlab_vega3-3.3.0-py3-none-any.whl (259.0 kB 查看哈希值)

上传时间 Python 3

支持者