Skip to content

Instantly share code, notes, and snippets.

@jojonki
Last active December 12, 2019 06:13
Show Gist options
  • Save jojonki/61f47f9446e4880ebfba154e406ab3de to your computer and use it in GitHub Desktop.
Save jojonki/61f47f9446e4880ebfba154e406ab3de to your computer and use it in GitHub Desktop.
Call quay.io/plotly/orca API
"""Use plotly orca (Docker) to save png/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment