Refactor render process building new engine every render

This commit is contained in:
kinegratii 2017-11-15 09:16:31 +08:00
parent dc4bc70ad3
commit 4e155b5413
9 changed files with 70 additions and 92 deletions

View File

@ -38,5 +38,5 @@ from pyecharts.custom.timeline import Timeline
# misc
from pyecharts.template import online
from pyecharts.engine import configure
from pyecharts.template import configure
from pyecharts.style import Style

View File

@ -7,7 +7,6 @@ import datetime
import pyecharts.utils as utils
import pyecharts.template as template
import pyecharts.constants as constants
from pyecharts.engine import CURRENT_CONFIG
class Base(object):
@ -71,7 +70,8 @@ class Base(object):
所需要的js 依赖文件
"""
embed = 'chart_component.html'
tmp = template.JINJA2_ENV.get_template(embed)
default_engine = template.create_buildin_template_engine()
tmp = default_engine.get_template(embed)
my_option = utils.json_dumps(self._option, indent=4)
html = tmp.render(my_option=my_option,
chart_id=self._chart_id,
@ -89,7 +89,8 @@ class Base(object):
template_name='simple_chart.html',
object_name='chart',
extra_context=None):
tpl = template.JINJA2_ENV.get_template(template_name, parent=CURRENT_CONFIG.echarts_template_dir)
default_engine = template.create_buildin_template_engine()
tpl = default_engine.get_template(template_name)
context = {object_name: self}
context.update(extra_context or {})
html = tpl.render(**context)
@ -103,7 +104,8 @@ class Base(object):
"""
_tmp = "local.html"
my_option = utils.json_dumps(self._option, indent=4)
tmp = template.JINJA2_ENV.get_template(_tmp)
default_engine = template.create_buildin_template_engine()
tmp = default_engine.get_template(_tmp)
script_list = template.produce_html_script_list(self._js_dependencies)
html = tmp.render(
my_option=my_option,
@ -156,7 +158,7 @@ class Base(object):
_tmp = 'notebook.html'
dom = self._render_notebook_dom_()
component = self._render_notebook_component_()
tmp = template.JINJA2_ENV.get_template(_tmp)
tmp = template.create_buildin_template_engine().get_template(_tmp)
require_config = template.produce_require_configuration(
self._js_dependencies, self._jshost)
html = tmp.render(
@ -167,7 +169,7 @@ class Base(object):
""" 为 notebook 渲染 dom 模板
"""
_tmp = "notebook_dom.html"
tmp = template.JINJA2_ENV.get_template(_tmp)
tmp = template.create_buildin_template_engine().get_template(_tmp)
component = tmp.render(
chart_id=self._chart_id,
chart_width=self.width,
@ -179,7 +181,7 @@ class Base(object):
"""
_tmp = "notebook_chart_component.html"
my_option = utils.json_dumps(self._option, indent=4)
tmp = template.JINJA2_ENV.get_template(_tmp)
tmp = template.create_buildin_template_engine().get_template(_tmp)
component = tmp.render(
my_option=my_option, chart_id=self._chart_id)
return component

View File

@ -62,4 +62,4 @@ class PyEchartsConfig(object):
return jshost
CURRENT_CONFIG = PyEchartsConfig()

View File

@ -32,7 +32,7 @@ class Page(list):
template_name='simple_page.html',
object_name='page',
extra_context=None):
tpl = template.JINJA2_ENV.get_template(template_name)
tpl = template.create_buildin_template_engine().get_template(template_name)
context = {object_name: self}
context.update(extra_context or {})
html = tpl.render(**context)
@ -49,7 +49,7 @@ class Page(list):
chart_content = self.render_embed()
dependencies = self._merge_dependencies()
script_list = template.produce_html_script_list(dependencies)
tmp = template.JINJA2_ENV.get_template(template_name)
tmp = template.create_buildin_template_engine().get_template(template_name)
html = tmp.render(multi_chart_content=chart_content,
page_title=self._page_title,
script_list=script_list)
@ -86,7 +86,7 @@ class Page(list):
require_config = template.produce_require_configuration(
dependencies, self._jshost)
tmp = template.JINJA2_ENV.get_template(_tmp)
tmp = template.create_buildin_template_engine().get_template(_tmp)
html = tmp.render(
single_chart=components, dom=doms, **require_config)
return html

View File

@ -5,7 +5,7 @@ import os
from jinja2 import Environment, environmentfunction
from pyecharts.utils import json_dumps
from pyecharts import constants
from pyecharts.conf import CURRENT_CONFIG
from pyecharts.conf import PyEchartsConfig
class Helpers(object):
@ -52,14 +52,15 @@ def echarts_js_dependencies(env, *args):
:param args:
:return:
"""
current_config = env.pyecharts_config
dependencies = Helpers.merge_js_dependencies(*args)
js_names = [constants.DEFAULT_JS_LIBRARIES.get(x, x) for x in dependencies]
if CURRENT_CONFIG.js_embed:
if current_config.js_embed:
contents = Helpers.read_file_contents_from_local(js_names)
return '\n'.join(['<script type="text/javascript">\n{}\n</script>'.format(c) for c in contents])
else:
jshost = CURRENT_CONFIG.get_current_jshost_for_script()
jshost = current_config.get_current_jshost_for_script()
js_links = Helpers.generate_js_link(jshost, js_names)
return '\n'.join(['<script type="text/javascript" src="{}"></script>'.format(j) for j in js_links])
@ -154,7 +155,8 @@ class EchartsEnvironment(Environment):
"""
def __init__(self, *args, **kwargs):
def __init__(self, pyecharts_config=None, *args, **kwargs):
self._pyecharts_config = pyecharts_config or PyEchartsConfig()
super(EchartsEnvironment, self).__init__(
keep_trailing_newline=True,
trim_blocks=True,
@ -171,25 +173,10 @@ class EchartsEnvironment(Environment):
'echarts_js_content_wrap': echarts_js_content_wrap
})
@property
def pyecharts_config(self):
return self._pyecharts_config
def configure(
jshost=None,
echarts_template_dir=None,
force_js_embed=None,
**kwargs
):
"""
Config all items for pyecharts.
:param jshost:
:param echarts_template_dir:
:param force_js_embed:
:param kwargs:
:return:
"""
if jshost:
constants.CONFIGURATION['HOST'] = jshost
CURRENT_CONFIG.jshost = jshost
if echarts_template_dir:
CURRENT_CONFIG.echarts_template_dir = echarts_template_dir
if force_js_embed is not None:
CURRENT_CONFIG.force_js_embed = force_js_embed
def configure_pyecharts(self, **kwargs):
for k, v in kwargs.items():
setattr(self._pyecharts_config, k, v)

View File

@ -5,21 +5,51 @@ from __future__ import unicode_literals
import warnings
from jinja2 import FileSystemLoader
from pyecharts.engine import EchartsEnvironment
import pyecharts.constants as constants
from pyecharts.utils import get_resource_dir, LazyObject
from pyecharts.conf import CURRENT_CONFIG
from pyecharts.conf import PyEchartsConfig
from pyecharts.engine import EchartsEnvironment
from pyecharts.utils import get_resource_dir
CURRENT_CONFIG = PyEchartsConfig()
def get_current_engine():
def configure(
jshost=None,
echarts_template_dir=None,
force_js_embed=None,
**kwargs
):
"""
Config all items for pyecharts when use chart.render() or page.render().
:param jshost:
:param echarts_template_dir:
:param force_js_embed:
:param kwargs:
:return:
"""
if jshost:
constants.CONFIGURATION['HOST'] = jshost
CURRENT_CONFIG.jshost = jshost
if echarts_template_dir:
CURRENT_CONFIG.echarts_template_dir = echarts_template_dir
if force_js_embed is not None:
CURRENT_CONFIG.force_js_embed = force_js_embed
def online(host=constants.DEFAULT_HOST):
warnings.warn('The online will be deprecated,use "pyecharts.configure" instead.', DeprecationWarning)
constants.CONFIGURATION['HOST'] = host
CURRENT_CONFIG.jshost = host
def create_buildin_template_engine():
return EchartsEnvironment(
pyecharts_config=CURRENT_CONFIG,
loader=FileSystemLoader([CURRENT_CONFIG.echarts_template_dir, get_resource_dir('templates')])
)
JINJA2_ENV = LazyObject(get_current_engine)
def produce_require_configuration(dependencies, jshost):
"""
:param dependencies:
@ -69,9 +99,3 @@ def ensure_echarts_is_in_the_front(dependencies):
else:
raise Exception("No js library found. Nothing works!")
return dependencies
def online(host=constants.DEFAULT_HOST):
warnings.warn('The online will be deprecated,use "pyecharts.configure" instead.', DeprecationWarning)
constants.CONFIGURATION['HOST'] = host
CURRENT_CONFIG.jshost = host

View File

@ -111,34 +111,3 @@ def proxy_method(func):
return func(self._wrapped, *args)
return inner
class LazyObject(object):
_wrapped = None
def __init__(self, func):
self.__dict__['_setupfunc'] = func
__getattr__ = proxy_method(getattr)
def __setattr__(self, key, value):
if key == '_wrapped':
self.__dict__['_wrapped'] = value
else:
if self._wrapped is None:
self._setup()
setattr(self._wrapped, key, value)
def _setup(self):
self._wrapped = self._setupfunc()
__getitem__ = proxy_method(operator.getitem)
if PY2:
__str__ = proxy_method(str)
__unicode__ = proxy_method(unicode) # NOQA: unicode undefined on PY3
__nonzero__ = proxy_method(bool)
else:
__bytes__ = proxy_method(bytes)
__str__ = proxy_method(str)
__bool__ = proxy_method(bool)

View File

@ -1,16 +1,12 @@
# coding=utf8
from __future__ import unicode_literals
import json
import codecs
from test.constants import RANGE_COLOR, CLOTHES, WEEK
from pyecharts import (
Bar, Scatter3D, Line, Pie, Map,
Kline, Radar, WordCloud, Liquid)
from pyecharts import (Bar, Scatter3D)
from pyecharts import Page
from pyecharts.utils import get_resource_dir
from pyecharts.engine import configure
from nose.tools import eq_
from pyecharts.template import configure
from test.constants import RANGE_COLOR, CLOTHES
def create_three():

View File

@ -2,7 +2,7 @@
from pyecharts.utils import get_resource_dir
from pyecharts import Bar
from pyecharts.engine import EchartsEnvironment, configure
from pyecharts.engine import EchartsEnvironment
ECHARTS_ENV = EchartsEnvironment()
@ -20,7 +20,7 @@ def create_demo_bar(chart_id_demo=None):
def test_echarts_js_dependencies():
configure(jshost='http://localhost/echarts')
ECHARTS_ENV.configure_pyecharts(jshost='http://localhost/echarts')
tpl = ECHARTS_ENV.from_string('{{ echarts_js_dependencies(bar) }}')
bar = create_demo_bar()
html = tpl.render(bar=bar)
@ -28,7 +28,7 @@ def test_echarts_js_dependencies():
def test_echarts_js_dependencies_embed():
configure(jshost=get_resource_dir('templates', 'js', 'echarts'))
ECHARTS_ENV.configure_pyecharts(jshost=get_resource_dir('templates', 'js', 'echarts'))
tpl = ECHARTS_ENV.from_string('{{ echarts_js_dependencies_embed("echarts.min") }}')
bar = create_demo_bar()
html = tpl.render(bar=bar)