Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Call API
"""Use plotly orca (Docker) to save png/html files.
Run plotly-orca server before running this code.
% docker run -d -p 9091:9091
I refer the following forum to write this code and slightly modify the code for proper import.
import io
import requests
import json
from PIL import Image
import 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 =, data=json_str)
image = response.content + '.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__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.