Skip to content

Commit

Permalink
Optimize extra map registry.json (pyecharts#1172)
Browse files Browse the repository at this point in the history
* 🐴 no long github url is required in registry.json. and English name can stay outside pinyin_map

* ✨ illustrate the usage of external map packages

* Format: code
  • Loading branch information
chfw authored and chenjiandongx committed Jun 6, 2019
1 parent 7e45d79 commit bd6b0ba
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
12 changes: 12 additions & 0 deletions example/echarts_china_counties_js_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.datasets import register_url

register_url("https://echarts-maps.github.io/echarts-china-counties-js/")

g = (
Geo()
.add_schema(maptype="海淀区")
.set_global_opts(title_opts=opts.TitleOpts(title="海淀区"))
)
g.render()
12 changes: 12 additions & 0 deletions example/echarts_countries_js_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.datasets import register_url

register_url("https://echarts-maps.github.io/echarts-countries-js/")

g = (
Geo()
.add_schema(maptype="瑞士")
.set_global_opts(title_opts=opts.TitleOpts(title="瑞士"))
)
g.render()
15 changes: 13 additions & 2 deletions pyecharts/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ def register_url(asset_url: str):
except Exception as e:
raise e
files = {}
pinyin_names = set()
for name, pinyin in contents["PINYIN_MAP"].items():
file_name = contents["FILE_MAP"][pinyin]
files[name] = [file_name, "js"]

js_file_prefix = f'{contents["GITHUB_URL"]}/{contents["JS_FOLDER"]}/'
pinyin_names.add(pinyin)

for key, file_name in contents["FILE_MAP"].items():
if key not in pinyin_names:
# English names
files[key] = [file_name, "js"]

js_folder_name = contents["JS_FOLDER"]
if js_folder_name == "/":
js_file_prefix = f"{asset_url}/"
else:
js_file_prefix = f"{asset_url}/{js_folder_name}/"
EXTRA[js_file_prefix] = files


Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"安庆": "an1_qing4"
},
"FILE_MAP": {
"an1_qing4": "shape-with-internal-borders/an1_hui1_an1_qing4"
"an1_qing4": "shape-with-internal-borders/an1_hui1_an1_qing4",
"English Name": "shape-with-internal-borders/an1_hui1_an1_qing4"
}
}
15 changes: 9 additions & 6 deletions test/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
@patch("pyecharts.datasets.urllib.request.urlopen")
def test_register_url(fake):
current_path = os.path.dirname(__file__)
with open(
os.path.join(current_path, "fixtures", "registry.json"), encoding="utf8"
) as f:
fake_registry = os.path.join(current_path, "fixtures", "registry.json")
with open(fake_registry, encoding="utf8") as f:
fake.return_value = f
register_url("fake_url")
register_url("http://register.url/is/used")
eq_(
EXTRA,
{
"https://echarts-maps.github.io/echarts-china-cities-js/js/": {
"安庆": ["shape-with-internal-borders/an1_hui1_an1_qing4", "js"]
"http://register.url/is/used/js/": {
"安庆": ["shape-with-internal-borders/an1_hui1_an1_qing4", "js"],
"English Name": [
"shape-with-internal-borders/an1_hui1_an1_qing4",
"js",
],
}
},
)

0 comments on commit bd6b0ba

Please sign in to comment.