Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable user pass param js_path as the echarts's js file path. #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions echarts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import os
import sys
import json
import logging
import tempfile
Expand All @@ -25,7 +26,19 @@


class Echart(Base):
def __init__(self, title, 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,
'subtext': description,
Expand Down Expand Up @@ -59,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

Expand Down Expand Up @@ -86,27 +101,39 @@ 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

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):
"""
Plot into html file

: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))
persist or raw_input('Press enter for continue')

def save(self, path, name):
"""
Save html file into project dir
Expand All @@ -115,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())
2 changes: 1 addition & 1 deletion echarts/plot.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<title>ECharts</title>
<!-- 引入 echarts.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/3.1.10/echarts.min.js"></script>
<script src="{{echarts_js_path}}"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
Expand Down