Skip to content

Commit

Permalink
Fix: dump_options (pyecharts#1188)
Browse files Browse the repository at this point in the history
* Fix: dump_options

* Test: protect harddisk

* Fix: typehint
  • Loading branch information
chenjiandongx authored Jun 14, 2019
1 parent 4367827 commit a94c5ff
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
7 changes: 4 additions & 3 deletions pyecharts/charts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import os
import uuid
from typing import Sequence

from jinja2 import Environment

Expand All @@ -13,7 +12,7 @@
from ..options.series_options import BasicOpts
from ..render.display import HTML, Javascript
from ..render.engine import RenderEngine
from ..types import Optional, Union
from ..types import Optional, Sequence, Union


class Base:
Expand Down Expand Up @@ -50,7 +49,9 @@ def get_options(self) -> dict:
return utils.remove_key_with_none_value(self.options)

def dump_options(self) -> str:
return json.dumps(self.get_options(), indent=4, default=default)
return utils.replace_placeholder(
json.dumps(self.get_options(), indent=4, default=default)
)

def render(
self,
Expand Down
6 changes: 5 additions & 1 deletion pyecharts/commons/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from pyecharts.datasets import EXTRA, FILENAMES
from ..datasets import EXTRA, FILENAMES


class JsCode:
Expand Down Expand Up @@ -51,6 +51,10 @@ def write_utf8_html_file(file_name, html_content):
html_file.write(html_content)


def replace_placeholder(html: str) -> str:
return re.sub('"?--x_x--0_0--"?', "", html)


def _flat(obj):
if hasattr(obj, "js_dependencies"):
return list(obj.js_dependencies)
Expand Down
2 changes: 1 addition & 1 deletion pyecharts/options/series_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get(self, key: str) -> Any:
class ItemStyleOpts(BasicOpts):
def __init__(
self,
color: Optional[str] = None,
color: Optional[JSFunc] = None,
color0: Optional[str] = None,
border_color: Optional[str] = None,
border_color0: Optional[str] = None,
Expand Down
14 changes: 4 additions & 10 deletions pyecharts/render/engine.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import re

from jinja2 import Environment

from ..commons.utils import write_utf8_html_file
from ..commons.utils import replace_placeholder, write_utf8_html_file
from ..datasets import EXTRA, FILENAMES
from ..globals import CurrentConfig
from ..types import Any, Optional
Expand Down Expand Up @@ -42,17 +40,13 @@ def render_chart_to_file(self, template_name: str, chart: Any, path: str):
:param template_name: The name of template file.
"""
tpl = self.env.get_template(template_name)
html = self._replace_html(tpl.render(chart=self.generate_js_link(chart)))
html = replace_placeholder(tpl.render(chart=self.generate_js_link(chart)))
write_utf8_html_file(path, html)

def render_chart_to_template(self, template_name: str, chart: Any) -> str:
tpl = self.env.get_template(template_name)
return self._replace_html(tpl.render(chart=self.generate_js_link(chart)))
return replace_placeholder(tpl.render(chart=self.generate_js_link(chart)))

def render_chart_to_notebook(self, template_name: str, **kwargs) -> str:
tpl = self.env.get_template(template_name)
return self._replace_html(tpl.render(**kwargs))

@staticmethod
def _replace_html(html) -> str:
return re.sub('"?--x_x--0_0--"?', "", html)
return replace_placeholder(tpl.render(**kwargs))
11 changes: 11 additions & 0 deletions test/test_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ def test_extra_geo_parameters(fake_writer):
"""
assert_in(center_string, content)
assert_in('"zoom": 9', content)


def test_geo_dump_options():
c = (
Geo()
.add_schema(maptype="china", center=[39, 117.7], zoom=9)
.add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
.set_global_opts(visualmap_opts=opts.VisualMapOpts())
)
formatter = """"formatter": function (params) { return params.name + ' : ' + params.value[2]; }""" # noqa
assert_in(formatter, c.dump_options())
7 changes: 5 additions & 2 deletions test/test_heatmap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
from unittest.mock import patch

from nose.tools import eq_

Expand All @@ -7,14 +8,16 @@
from pyecharts.charts import HeatMap


def test_heatmap_base():
@patch("pyecharts.render.engine.write_utf8_html_file")
def test_heatmap_base(fake_writer):
value = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)]
c = (
HeatMap()
.add_xaxis(Faker.clock)
.add_yaxis("series0", Faker.week, value)
.set_global_opts(visualmap_opts=opts.VisualMapOpts())
)
c.render()
_, content = fake_writer.call_args[0]
eq_(c.theme, "white")
eq_(c.renderer, "canvas")
c.render()

0 comments on commit a94c5ff

Please sign in to comment.