Skip to main content

Apache Echarts Jupyter Widget.

Project description

ipecharts

Github Actions Status Documentation Status Try on lite

Apache Echarts Jupyter Widget

ipecharts brings interactive widgets based on Apache ECharts charting library to the Jupyter ecosystem. By using the Jupyter Widget protocol, ipecharts is fully compatible with other widget libraries and tools in the Jupyter ecosystem.

https://github.com/trungleduc/ipecharts/assets/4451292/c6e73b4d-61ef-4098-a274-92233d0801b0

[!NOTE]
pyecharts also supports using Echarts in the notebook, but they are not using Jupyter Widget like ipecharts. In this library, HTML code is injected into the notebook to render the chart.

Try it online!

You can try it online by clicking on this badge:

Try on lite

Documentation

You can read the documentation following this link: https://ipecharts.readthedocs.io/

Installation

To install the extension, execute:

pip install ipecharts

or with conda:

conda install -c conda-forge  ipecharts

Usage

ipecharts widgets are generated automatically from ECharts 5.5.0. It provides two high-level widgets to create charts in notebooks: EChartsRawWidget and EChartsWidget.

Create charts using EChartsRawWidget

EChartsRawWidget is a simple widget to render ECharts option dictionary. It is fully compatible with the JavaScript version of ECharts. Here is an example of converting the following JS example:

import * as echarts from 'echarts';

var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;

option = {
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      data: [820, 932, 901, 934, 1290, 1330, 1320],
      type: 'line',
      areaStyle: {}
    }
  ]
};

option && myChart.setOption(option);

into using EChartsRawWidget:

from ipecharts import EChartsRawWidget

option = {
  'xAxis': {
    'type': 'category',
    'boundaryGap': False,
    'data': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  'yAxis': {
    'type': 'value'
  },
  'series': [
    {
      'data': [820, 932, 901, 934, 1290, 1330, 1320],
      'type': 'line',
      'areaStyle': {}
    }
  ]
}

EChartsRawWidget(option=option)

EChartsRawWidget

Create charts using EChartsWidget

While the raw widget can render the charts correctly, it lacks the interactivity of a Jupyter widget. ipecharts provides EChartsWidget and configuration classes for nearly all available options of ECharts to correct this issue.

Here is the equivalent of the above chart but using EChartsWidget:

from ipecharts import EChartsWidget
from ipecharts.option import Option, XAxis, YAxis
from ipecharts.option.series import Line

line = Line(data=[820, 932, 901, 934, 1290, 1330, 1320], areaStyle={})
option = Option(
    xAxis=XAxis(
        type="category",
        boundaryGap=False,
        data=["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
    ),
    yAxis=YAxis(type="value"),
    series=[line],
)
EChartsWidget(option=option)

While it looks more verbose, the advantage is the reactivity. We can update the line data and have the chart updated automatically.

ipechart

Configure EChartsWidget with traitlets

Each key in the option dictionary of ECharts has an equivalent configuration class with the same name. These classes contain traits with the same name as the corresponding ECharts option. Any change to these traits will be propagated to the top-level widget, and the chart will be updated automatically.

For instance, you can compare the scatter option of ECharts at https://echarts.apache.org/en/option.html#series-scatter.type and the equivalent Scatter class in the ipecharts documentation. The Python class is generated automatically from the ECharts option.

By using Traitlets to configure your widget, you can use EChartsWidget with other widgets in the Jupyter ecosystem. Here is an example of controlling the chart with an ipywidgets Button:

from ipecharts.option import Option, XAxis, YAxis
from ipecharts.option.series import Line
from ipywidgets.widgets import Button

line = Line(smooth=True, areaStyle={}, data=numpy.random.rand(10).tolist())
option = Option(
    xAxis=XAxis(type="category"),
    yAxis=YAxis(type="value"),
    series=[line],
)
chart = EChartsWidget(option=option)

button = Button(description="Generate data")
def on_button_clicked(b):
    data = numpy.random.rand(10).tolist()
    line.data = data

button.on_click(on_button_clicked)

display(button, chart)

ipechart2

Customize the chart container style

Both EChartsWidget and EChartsRawWidget classes allow you to customize the style of the chart container by setting the style attribute. The style attribute accepts a dictionary where keys are CSS property names in camelCase or kebab-case (as strings), and values are the corresponding CSS values.

Example: 'backgroundColor': '#f0f0f0' or 'background-color': '#f0f0f0'

from ipecharts import EChartsWidget
from ipecharts.option import Option, XAxis, YAxis
from ipecharts.option.series import Line

# Define the data for the line series
line = Line(
    data=[820, 932, 901, 934, 1290, 1330, 1320],
    areaStyle={}
)

# Create the option object with xAxis, yAxis, and series
option = Option(
    xAxis=XAxis(
        type="category",
        boundaryGap=False,
        data=["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
    ),
    yAxis=YAxis(type="value"),
    series=[line]
)

# Define the style for the widget
style = {
    'width': '450px',
    'height': '300px',
    'border': '5px solid #ccc'
}

# Create the EChartsWidget with the option and style
chart = EChartsWidget(option=option, style=style)

# Display the chart
chart

After the widget has been created and displayed, you can update its style by modifying the style attribute.

# Update the style of the chart
chart.style = {
    'width': '800px',
    'height': '600px',
    'border': '2px solid #000'
}

# The widget will automatically update to reflect the new styles.

updateStyle

Contributing

Development install

Note: You will need NodeJS to build the extension package.

The jlpm command is JupyterLab's pinned version of yarn that is installed with JupyterLab. You may use yarn or npm in lieu of jlpm below.

# Clone the repo to your local environment
# Change directory to the ipecharts 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 build

You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.

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

With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt).

By default, the jlpm build command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command:

jupyter lab build --minimize=False

Development uninstall

pip uninstall ipecharts

In development mode, you will also need to remove the symlink created by jupyter labextension develop command. To find its location, you can run jupyter labextension list to figure out where the labextensions folder is located. Then you can remove the symlink named ipecharts within that folder.

Packaging the extension

See RELEASE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipecharts-1.0.7.tar.gz (7.6 MB view details)

Uploaded Source

Built Distribution

ipecharts-1.0.7-py3-none-any.whl (874.5 kB view details)

Uploaded Python 3

File details

Details for the file ipecharts-1.0.7.tar.gz.

File metadata

  • Download URL: ipecharts-1.0.7.tar.gz
  • Upload date:
  • Size: 7.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for ipecharts-1.0.7.tar.gz
Algorithm Hash digest
SHA256 260118e071b12cc823b95b3a57c3579112b3322b5a9fd6993d40952360c3149d
MD5 ac0a69a33178f7751624a6373f591045
BLAKE2b-256 41fe138471b8f90e69eeff2b98442801c3e12e0ec338ea761f960ff80a737c70

See more details on using hashes here.

Provenance

File details

Details for the file ipecharts-1.0.7-py3-none-any.whl.

File metadata

  • Download URL: ipecharts-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 874.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for ipecharts-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 817b3476c13c14b3f109311786702a9602df51448eea8a14b4b11a0466407bb4
MD5 5449416ba7b4f45f74f9c5db59ba9072
BLAKE2b-256 80bbcafe173bf8c7cc08bd729ccacbab6befd931271f3f1a23cbf3a2f3195667

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page