"""Use plotly orca (Docker) to save a figure as png and html files. Run plotly-orca server before running this code. % docker run -d -p 9091:9091 quay.io/plotly/orca I refer the following forum to write this code and slightly modify the code for proper import. https://community.plot.ly/t/plotly-io-with-external-orca/25600/2 """ import io import requests import json from PIL import Image import plotly.io as pio import plotly.graph_objects as go from plotly.offline import plot from plotly.utils import PlotlyJSONEncoder def dumpImage(plotly_server, filename, figure, img_format='png'): request_params = { 'figure': figure.to_dict(), 'format': img_format, # any format from 'png', 'jpeg', 'webp', 'svg', 'pdf', 'eps' 'scale': 1, 'width': 500, 'height': 500 } json_str = json.dumps(request_params, cls=PlotlyJSONEncoder) response = requests.post(plotly_server, data=json_str) image = response.content Image.open(io.BytesIO(image)).save(filename + '.png') plot(figure, filename=filename + '.html') def main(): plotly_server = 'http://localhost:9091' fig = go.Figure(data=[go.Bar(y=[2, 3, 1])]) dumpImage(plotly_server, 'out', fig) if __name__ == '__main__': main()