From a1f7d6ea883c75771fe7e20316e460a4e66476a4 Mon Sep 17 00:00:00 2001 From: chfw Date: Wed, 27 Mar 2019 22:04:52 +0000 Subject: [PATCH] :sparkles: enable the integration with the existing echarts-maps assets, that are hosted on github --- pyecharts/commons/utils.py | 16 ++++++++++++---- pyecharts/datasets/__init__.py | 15 ++++++++++++++- pyecharts/render/engine.py | 13 ++++++++++--- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/pyecharts/commons/utils.py b/pyecharts/commons/utils.py index 2af19a7a..a7931d03 100644 --- a/pyecharts/commons/utils.py +++ b/pyecharts/commons/utils.py @@ -1,5 +1,5 @@ # coding=utf-8 -from pyecharts.datasets import FILENAMES +from pyecharts.datasets import FILENAMES, EXTRA class OrderedSet: @@ -22,9 +22,17 @@ def filter_js_func(fn: str) -> str: def produce_require_dict(js_dependencies, js_host) -> dict: confs, libraries = [], [] for name in js_dependencies.items: - f, _ = FILENAMES[name] - confs.append("'{}':'{}{}'".format(name, js_host, f)) - libraries.append("'{}'".format(name)) + if name in FILENAMES: + f, _ = FILENAMES[name] + confs.append("'{}':'{}{}'".format(name, js_host, f)) + libraries.append("'{}'".format(name)) + else: + for url, files in EXTRA.items(): + if name in files: + f, _ = files[name] + confs.append("'{}':'{}{}'".format(name, url, f)) + libraries.append("'{}'".format(name)) + break return dict(config_items=confs, libraries=libraries) diff --git a/pyecharts/datasets/__init__.py b/pyecharts/datasets/__init__.py index 24081afe..a424d65f 100644 --- a/pyecharts/datasets/__init__.py +++ b/pyecharts/datasets/__init__.py @@ -1,11 +1,24 @@ # coding=utf-8 import json import os +import urllib.request + __HERE = os.path.abspath(os.path.dirname(__file__)) - +EXTRA = {} with open(os.path.join(__HERE, "map_filename.json"), "r", encoding="utf8") as f: FILENAMES = json.load(f) with open(os.path.join(__HERE, "city_coordinates.json"), "r", encoding="utf8") as f: COORDINATES = json.load(f) + + +def register(additional_asset_url): + contents = urllib.request.urlopen(additional_asset_url + '/registry.json').read() + contents = json.loads(contents) + files = {} + for name, pinyin in contents['PINYIN_MAP'].items(): + file_name = contents['FILE_MAP'][pinyin] + files[name] = [file_name, 'js'] + + EXTRA[contents['GITHUB_URL'] + '/'] = files diff --git a/pyecharts/render/engine.py b/pyecharts/render/engine.py index 1b675f93..f87776ce 100644 --- a/pyecharts/render/engine.py +++ b/pyecharts/render/engine.py @@ -6,7 +6,7 @@ from jinja2 import Environment from pyecharts.commons.types import Any, Optional from ..commons.utils import write_utf8_html_file -from ..datasets import FILENAMES +from ..datasets import FILENAMES, EXTRA from ..globals import CurrentConfig @@ -21,8 +21,15 @@ class RenderEngine: links = [] for dep in chart.js_dependencies.items: # TODO: if? - f, ext = FILENAMES[dep] - links.append("{}{}.{}".format(chart.js_host, f, ext)) + if dep in FILENAMES: + f, ext = FILENAMES[dep] + links.append("{}{}.{}".format(chart.js_host, f, ext)) + else: + for url, files in EXTRA.items(): + if dep in files: + f, ext = files[dep] + links.append("{}{}.{}".format(url, f, ext)) + break chart.dependencies = links return chart