将绘图图形保存到html文件后,您以后可以将其作为图形重新读取吗



我想编辑数据,可能会向图中添加更多跟踪。我找到了一种将html文件显示为图形的方法,但不进行编辑

from IPython.display import HTML
HTML(filename='file_name.html')

通常,Plotly JSON应用于图表(例如figure.write_jsonplotly.read_json(的串行化。但是,如果您只有图表的HTML表示,那么相同的Plotly JSON会被馈送到plotly.js,并且可以提取它。

只是为了示范。使用CCD_ 4。

import json
import re
import plotly.express

def write():
fig = plotly.express.bar(y=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
fig.write_html('plot.html', full_html=True)
fig.write_json('plot.json')

def read_from_json():
return plotly.io.read_json('plot.json')

def read_from_html():
with open('plot.html') as f:
html = f.read()
call_arg_str = re.findall(r'Plotly.newPlot((.*))', html[-2**16:])[0]
call_args = json.loads(f'[{call_arg_str}]')
plotly_json = {'data': call_args[1], 'layout': call_args[2]}    
return plotly.io.from_json(json.dumps(plotly_json))

if __name__ == '__main__':
write()
read_from_json()
read_from_html()

最新更新