'Use Python plotly chart in PHP

I have created a pie graph with Python plotly library

fig = go.Figure(data=[go.Pie(labels=data["x"], values=data['value'])])
fig = dcc.Graph(id='pie_graph', figure=fig)
fig = fig.to_plotly_json()

And I want to depict this chart in a PHP page.

$command = escapeshellcmd('python /home/mypath/demo.py');
$output = shell_exec($command);
echo $output;

Since I am new to PHP, this approach returns a JSON with the characteristics of the chart and doesn't depict the chart.

I also tried

json_decode($output, TRUE) 

But without success.

I know that plotly needs a layout to be depicted, but I don't know how to integrate it with the already implemented PHP page.



Solution 1:[1]

Try

import plotly.io

print(plotly.io.to_html(fig))

(instead of fig = fig.to_plotly_json()). This will create a complete HTML document. For more details, see https://plotly.com/python-api-reference/generated/plotly.io.to_html.html.

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 ASchommer