# coding=utf8
"""
Test cases for rendering in jupyter notebook
"""
from __future__ import unicode_literals
import json
from test.constants import CLOTHES, WEEK
import pyecharts.commons.consts as constants
from pyecharts import Bar, Line, Page, Pie, configure, enable_nteract, online
from pyecharts.conf import CURRENT_CONFIG
TITLE = "柱状图数据堆叠示例"
def create_a_bar(title):
v1 = [5, 20, 36, 10, 75, 90]
v2 = [10, 25, 8, 60, 20, 80]
bar = Bar(title)
bar.add("商家A", CLOTHES, v1, is_stack=True)
bar.add("商家B", CLOTHES, v2, is_stack=True)
return bar
def test_single_chart():
bar = create_a_bar(TITLE)
html = bar._repr_html_()
json_encoded_title = json.dumps(TITLE)
assert json_encoded_title in html
assert "require.config" in html
assert "function(options)" in html
assert "nbextensions/options" in html
assert "width:800px" in html
assert "height:400px" in html
assert "
") == html.count("") == 2
assert html.count("
") == 3
assert html.count("require.config") == html.count("function(options)") == 1
# Test some chart attributes
json_encoded_title = json.dumps(TITLE)
assert json_encoded_title in html
assert "nbextensions/options" in html # default jshost
assert html.count("height:400px") == 3
assert html.count("width:600px") == 1
assert html.count("width:800px") == 2
assert html.count("id_my_cell_line") == 6
def test_online_feature():
online()
bar = create_a_bar(TITLE)
html = bar._repr_html_()
expected_jshost = "https://pyecharts.github.io/jupyter-options/options"
assert expected_jshost in html
CURRENT_CONFIG.hosted_on_github = False
def test_online_with_custom_jshost():
online(host="https://my-site.com/js")
bar = create_a_bar(TITLE)
html = bar._repr_html_()
expected_jshost = "https://my-site.com/js"
assert expected_jshost in html
CURRENT_CONFIG.jshost = None
def test_nteract_feature():
enable_nteract()
bar = create_a_bar(TITLE)
html = bar._repr_html_()
assert "https://pyecharts.github.io/assets/js/options.min.js" in html
assert "require" not in html
# restore configuration
configure(output_image=constants.DEFAULT_HTML)
CURRENT_CONFIG.jshost = None
CURRENT_CONFIG.hosted_on_github = False