From 4fc33c57ad364a1f5a66f73de059de90f7dc4fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=96=87=E5=8B=87=20wenyong=2Exu?= Date: Thu, 17 Aug 2017 17:34:04 +0800 Subject: [PATCH 1/2] update the version of echarts.min.js; enable user pass param js_path as the echarts's js file path. --- echarts/__init__.py | 11 ++++++++--- echarts/plot.j2 | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/echarts/__init__.py b/echarts/__init__.py index 4d1683b..50ec12f 100644 --- a/echarts/__init__.py +++ b/echarts/__init__.py @@ -25,7 +25,8 @@ class Echart(Base): - def __init__(self, title, description=None, axis=True, **kwargs): + def __init__(self, title,js_path=None, description=None, axis=True, **kwargs): + self.js_path = js_path self.title = { 'text': title, 'subtext': description, @@ -91,9 +92,13 @@ def json(self): return json def _html(self): + echarts_js_path = 'https://cdnjs.cloudflare.com/ajax/libs/echarts/3.5.4/echarts.min.js' + if self.js_path: + echarts_js_path=self.js_path with open(os.path.join(os.path.dirname(__file__), 'plot.j2')) as f: template = f.read() - return template.replace('{{ opt }}', json.dumps(self.json, indent=4)) + return template.replace('{{ opt }}', json.dumps(self.json, indent=4) + ).replace('{{echarts_js_path}}', echarts_js_path) def plot(self, persist=True): """ @@ -106,7 +111,7 @@ def plot(self, persist=True): fobj.flush() webbrowser.open('file://' + os.path.realpath(fobj.name)) persist or raw_input('Press enter for continue') - + def save(self, path, name): """ Save html file into project dir diff --git a/echarts/plot.j2 b/echarts/plot.j2 index b6edc3e..f0a9599 100644 --- a/echarts/plot.j2 +++ b/echarts/plot.j2 @@ -4,7 +4,7 @@ ECharts - + From d1ce57e3ec3bb7df3fb6d87ba132659ee045d9f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=96=87=E5=8B=87=20wenyong=2Exu?= Date: Mon, 21 Aug 2017 16:09:29 +0800 Subject: [PATCH 2/2] update to enable user pass all valid options by a dict; --- echarts/__init__.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/echarts/__init__.py b/echarts/__init__.py index 50ec12f..4f71669 100644 --- a/echarts/__init__.py +++ b/echarts/__init__.py @@ -11,6 +11,7 @@ """ import os +import sys import json import logging import tempfile @@ -25,7 +26,18 @@ class Echart(Base): - def __init__(self, title,js_path=None, description=None, axis=True, **kwargs): + validOptions = ['title', 'legend', 'grid', 'xAxis', 'yAxis', 'polar', + 'radiusAxis', 'angleAxis', 'radar', 'dataZoom', 'visualMap', 'tooltip', + 'axisPointer', 'toolbox', 'brush', 'geo', 'parallel', 'parallelAxis', + 'singleAxis', 'timeline', 'graphic', 'calendar', 'series', 'color', + 'backgroundColor', 'textStyle', 'animation', 'animationThreshold', + 'animationDuration', 'animationEasing', 'animationDelay', + 'animationDurationUpdate', 'animationEasingUpdate', + 'animationDelayUpdate', 'progressive', 'progressiveThreshold', + 'blendMode', 'hoverLayerThreshold', 'useUTC'] + + def __init__(self, title, js_path=None, description=None, axis=True, + **kwargs): self.js_path = js_path self.title = { 'text': title, @@ -60,6 +72,8 @@ def use(self, obj): self.toolbox = obj elif isinstance(obj, VisualMap): self.visualMap = obj + elif isinstance(obj, dict): + self.others = obj return self @@ -87,6 +101,13 @@ def json(self): json['toolbox'] = self.toolbox.json if hasattr(self, 'visualMap'): json['visualMap'] = self.visualMap.json + if hasattr(self, 'others'): + for key in self.others.keys(): + if key in self.validOptions: + json[key] = self.others[key] + else: + sys.stderr('Invalid Option: %s' % key) + exit(-1) json.update(self.kwargs) return json @@ -94,7 +115,7 @@ def json(self): def _html(self): echarts_js_path = 'https://cdnjs.cloudflare.com/ajax/libs/echarts/3.5.4/echarts.min.js' if self.js_path: - echarts_js_path=self.js_path + echarts_js_path = self.js_path with open(os.path.join(os.path.dirname(__file__), 'plot.j2')) as f: template = f.read() return template.replace('{{ opt }}', json.dumps(self.json, indent=4) @@ -106,7 +127,8 @@ def plot(self, persist=True): :param persist: persist output html to disk """ - with tempfile.NamedTemporaryFile(suffix='.html', delete=not persist) as fobj: + with tempfile.NamedTemporaryFile(suffix='.html', + delete=not persist) as fobj: fobj.write(self._html()) fobj.flush() webbrowser.open('file://' + os.path.realpath(fobj.name)) @@ -120,5 +142,5 @@ def save(self, path, name): """ if not os.path.exists(path): os.makedirs(path) - with open(path+str(name)+".html", "w") as html_file: - html_file.write(self._html()) + with open(path + str(name) + ".html", "w") as html_file: + html_file.write(self._html()) \ No newline at end of file