js: Uncaught ReferenceError: echarts is not defined【错误3】
\AppData\Local\Temp\_MEI4802\pyecharts\datasets\map_filename.json【错误1】
jinja2.exceptions.TemplateNotFound: simple_chart.html【错误2】
File “site-packages\pyecharts\datasets_init_.py”, line 93, in
FileNotFoundError: [Errno 2] No such file or directory:
‘C:\Users\???\AppData\Local\Temp\_MEI4802\pyecharts\datasets\map_filename.json’
直接把python安装的pyecharts(Lib\site-packages\pyecharts)贴到此exe所在的路径下
/
…
pyrcharts
…
XXX.exe
打开文件 pyecharts\datasets_init_.py
找到 __HERE = os.path.abspath(os.path.dirname(file))
重新定义 __HERE 为你的项目路径
看到报错是在pyecharts\datasets_init_.py文件93行,下面是代码
__HERE = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(__HERE, "map_filename.json"), "r", encoding="utf8") as f:
FILENAMES: FuzzyDict = FuzzyDict()
for k, v in json.load(f).items():
FILENAMES[k] = v
with open(os.path.join(__HERE, "city_coordinates.json"), "r", encoding="utf8") as f:
COORDINATES: FuzzyDict = FuzzyDict()
for k, v in json.load(f).items():
COORDINATES[k] = v
os.path.abspath(XXX) 的锅
os.path.abspath(XXX) 没打包前是exe所在的路径打包后就变成 C:\Users\???\AppData\Local\Temp\_MEI4802\XXX了
错误2也是这个os.path.abspath的锅,但是用不着改源码改配置就可以了
这就需要改源码了
File “site-packages\jinja2\loaders.py”, line 187, in get_source
jinja2.exceptions.TemplateNotFound: simple_chart.html
自己定义html路径
(我的版本目录是这样的,你不一样的部分,照猫画虎)
from jinja2 import FileSystemLoader,Environment
import os
myenv = Environment(
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True,
loader=FileSystemLoader(
os.path.join(
"你的项目路径", "pyecharts","render","templates"
)))
grid.render(r'my.html', env=myenv)
当调用render方法时,我们没有定义env的话,他自己就默认用jinja2创建一个
pyecharts/charts/base.py
def render(
self,
path: str = "render.html",
template_name: str = "simple_chart.html",
env: Optional[Environment] = None,
**kwargs,
) -> str:
self._prepare_render()
return engine.render(self, path, template_name, env, **kwargs)
它创建的env里面有一句 os.path.abspath!!!!
pyecharts/globals.py
class _CurrentConfig:
PAGE_TITLE = "Awesome-pyecharts"
ONLINE_HOST = OnlineHostType.DEFAULT_HOST
NOTEBOOK_TYPE = NotebookType.JUPYTER_NOTEBOOK
GLOBAL_ENV = Environment(
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True,
loader=FileSystemLoader(
os.path.join(
os.path.abspath(os.path.dirname(__file__)), "render", "templates"
)
),
)
CurrentConfig = _CurrentConfig()
pyecharts之所以需要联网,是他的JS们
这种方式属于暴力解决,用法非常不地道
例如:echarts.min.js等引用的文件
下载到本地,手动修改生成html
更改配置项
注意我这边是grid,你如果是别的就照猫画虎即可,总之谁生成的html谁的设置项里定义就行
grid =Grid(init_opts=opts.InitOpts(#bg_color='rgba(255,250,205,0.2)',
width='1800px',
height='700px',
page_title='page',
js_host = './pkg_ui/js/',
theme=ThemeType.WALDEN))
grid.render(r'cache.html')
最终生成的html就引用本地js了
page
问题3,设置的图表在html里看不到
解决:所有同样类型的图表写在一起,别跳着加入布局
参考文档:
https://pyecharts.readthedocs.io/projects/pyecharts-en/zh/latest/en-us/api/#template-functions