I'd like to edit the data, probably adding more traces to the graph. I've found a way to display the html file as a graph, but not to edit it.
from IPython.display import HTML
HTML(filename='file_name.html')
I'd like to edit the data, probably adding more traces to the graph. I've found a way to display the html file as a graph, but not to edit it.
from IPython.display import HTML
HTML(filename='file_name.html')
Normally Plotly JSON should be used for (de)serialisation of charts (e.g. figure.write_json and plotly.read_json). However, if you only have the HTML representation of the chart, the same Plotly JSON is fed to plotly.js there, and it can be extracted.
For the sake of demonstration only. plotly==5.1.0 used.
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()