原因:Pyecharts生成的圖表所依賴的 JS 是通過外鏈的形式引用的(查過源碼發(fā)現(xiàn)只" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運(yùn)營 > pyecharts 采用本地靜態(tài)資源生成html,離線可訪問。

pyecharts 采用本地靜態(tài)資源生成html,離線可訪問。

時間:2023-07-21 03:48:01 | 來源:網(wǎng)站運(yùn)營

時間:2023-07-21 03:48:01 來源:網(wǎng)站運(yùn)營

pyecharts 采用本地靜態(tài)資源生成html,離線可訪問。:

問題:

pyecharts生成圖表,打開網(wǎng)頁后一片空白,或者離線情況下無法打開,無法查看繪制的圖表。

原因:

Pyecharts生成的圖表所依賴的 JS 是通過外鏈的形式引用的(查過源碼發(fā)現(xiàn)只能以外鏈的形式依賴),如果因某種條件無法訪問依賴的 JS ,就會導(dǎo)致改 html 打開是空白頁面,無法展示已畫的圖表。

查看生成html的源代碼內(nèi)容:

html源代碼

解決問題:

官方提供的解決辦法:

pyecharts-assets 提供了 pyecharts 的靜態(tài)資源文件。

可通過 localhost-server 或者 notebook-server 啟動本地服務(wù)。首先將項(xiàng)目下載到本地:

# 通過 git clone$ git clone https://github.com/pyecharts/pyecharts-assets.git# 或者直接下載壓縮包$ wget https://github.com/pyecharts/pyecharts-assets/archive/master.zipLocalhost-Server

啟動python3內(nèi)置的服務(wù)器,首先切換到靜態(tài)資源目錄下

$ cd pyecharts-assets$ python -m http.server設(shè)置 host

# 只需要在頂部聲明 CurrentConfig.ONLINE_HOST 即可from pyecharts.globals import CurrentConfigCurrentConfig.ONLINE_HOST = "http://127.0.0.1:8000/assets/"# 接下來所有圖形的靜態(tài)資源文件都會來自剛啟動的服務(wù)器from pyecharts.charts import Barbar = Bar()再次運(yùn)行生成html源代碼如下,離線斷網(wǎng)情況也可以正常打開:

本地localhost
可以更進(jìn)一步,脫離本地python http server,可以嘗試將js/css 引用路徑直接替換為引用本地靜態(tài)文件的路徑。

# 只需要在頂部聲明 CurrentConfig.ONLINE_HOST 即可from pathlib import Pathfrom pyecharts.globals import CurrentConfig# 本地靜態(tài)資源assets 目錄路徑static_resource = Path(__file__).parents[1] / 'pyecharts' / 'pyecharts-assets' / 'assets'# windows環(huán)境下 / 替換為 /, *nix環(huán)境請按需修改CurrentConfig.ONLINE_HOST = str(static_resource).replace("//", r'/') + '/'# 接下來所有圖形的靜態(tài)資源文件都會來自本地指定的靜態(tài)資源from pyecharts.charts import Barbar = Bar()生成的html源代碼如下,不啟用http.server情況下斷網(wǎng)時可正常打開:

引用本地靜態(tài)資源
Notebook-Server

安裝擴(kuò)展插件

$ cd pyecharts-assets# 安裝并激活插件$ jupyter nbextension install assets$ jupyter nbextension enable assets/main設(shè)置 host

# 只需要在頂部聲明 CurrentConfig.ONLINE_HOST 即可from pyecharts.globals import CurrentConfig, OnlineHostType# OnlineHostType.NOTEBOOK_HOST 默認(rèn)值為 http://localhost:8888/nbextensions/assets/CurrentConfig.ONLINE_HOST = OnlineHostType.NOTEBOOK_HOST# 接下來所有圖形的靜態(tài)資源文件都會來自剛啟動的服務(wù)器from pyecharts.charts import Barbar = Bar()

關(guān)鍵詞:訪問,采用,本地,靜態(tài),資源

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉