From 34076fcda33b10bfeeca512a88e653a2fff49b7a Mon Sep 17 00:00:00 2001 From: chfw Date: Tue, 1 Aug 2017 12:08:49 +0100 Subject: [PATCH] :racehorse: major speed improvement for browser loading. all javascript files are local in render.html. NO more fetching from the cloud. It works when you do NOT have INTERNET access. --- MANIFEST.in | 1 + pyecharts/base.py | 1 + pyecharts/template.py | 32 +++++++++++++ pyecharts/templates/js/get.sh | 1 + pyecharts/templates/template.html | 78 ++++++++++++++++--------------- setup.py | 1 + 6 files changed, 76 insertions(+), 38 deletions(-) create mode 100644 pyecharts/templates/js/get.sh diff --git a/MANIFEST.in b/MANIFEST.in index 61fec855..8b005d7e 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,4 @@ include README.md include changelog.md include pyecharts/templates/*.html +include pyecharts/templates/js/*.js diff --git a/pyecharts/base.py b/pyecharts/base.py index bf927db8..844288e6 100644 --- a/pyecharts/base.py +++ b/pyecharts/base.py @@ -422,6 +422,7 @@ class Base(object): my_option = json.dumps(self._option, indent=4, ensure_ascii=False) tmp = self._jinja2_env.get_template(temple) html = tmp.render(myOption=my_option, myWidth=self._width, myHeight=self._height) + html = template.freeze_js(html) if PY2: html = html.encode('utf-8') with open(path, "w+") as fout: diff --git a/pyecharts/template.py b/pyecharts/template.py index 10a5fc19..87c54dfd 100644 --- a/pyecharts/template.py +++ b/pyecharts/template.py @@ -1,6 +1,38 @@ #!/usr/bin/env python #coding=utf-8 import os +import re + + +JS_PATTERN = re.compile(r'(.*)', + re.IGNORECASE | re.MULTILINE | re.DOTALL) +JS_SRC_PATTERN = re.compile(r'src=\"(.*?)\"') + + +def freeze_js(html_content): + matches = JS_PATTERN.finditer(html_content) + + if not matches: + return html_content + + for match in reversed(tuple(matches)): + # JS file block + src_matches = JS_SRC_PATTERN.findall(match.group(1)) + + js_content = "" + for src in src_matches: + file_path = os.path.join(get_resource_dir('templates'), src.strip()) + + with open(file_path, "r") as f: + js_content += f.read() + '\n' + # Replace matched string with inline JS + fmt = '' + js_content = fmt.format(js_content) + html_content = (html_content[:match.start()] + js_content + + html_content[match.end():]) + + return html_content + _mapindex = { "安徽": "anhui: '//oog4yfyu0.bkt.clouddn.com/anhui'", diff --git a/pyecharts/templates/js/get.sh b/pyecharts/templates/js/get.sh new file mode 100644 index 00000000..a8ee5010 --- /dev/null +++ b/pyecharts/templates/js/get.sh @@ -0,0 +1 @@ +curl http://oog4yfyu0.bkt.clouddn.com/$1.js -o $1.js diff --git a/pyecharts/templates/template.html b/pyecharts/templates/template.html index 959998f7..e271d45c 100644 --- a/pyecharts/templates/template.html +++ b/pyecharts/templates/template.html @@ -4,44 +4,46 @@ ECharts - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/setup.py b/setup.py index f0cc38bb..7ad2b4e5 100644 --- a/setup.py +++ b/setup.py @@ -30,6 +30,7 @@ setup( keywords=__keywords__, install_requires=__requires__, zip_safe=False, + include_package_data=True, classifiers=[ 'Development Status :: 4 - Beta', 'Environment :: Console',