'Build a plot from JSON with plotly.js

I have built a plot using plotly.py and convert it to JSON file:

plotly.io.write_json(fig, 'name.json')

After creating the JSON file I need to build the same plot using plotly.js as well. How can build it from generated JSON file? File consists of two main parts. first part is data (parameter of about 100+ lines from plot) and second part is layout of plot. Is there any standard function in plotly.js to build a plot from JSON generated by plotly rules?



Solution 1:[1]

This is possible in javascript by first importing your file and then grabing the data, layout and config keys.

import testData from './name.json';

Plotly.newPlot('myDiv', testData.data, testData.layout, testData.config);
<head>
    <!-- Load plotly.js into the DOM -->
    <script src='https://cdn.plot.ly/plotly-2.11.1.min.js'></script>
</head>

<body>
    <div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>
</body>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stigson