Skip to content

Instantly share code, notes, and snippets.

@jojonki
Last active December 12, 2019 06:13
Show Gist options
  • Select an option

  • Save jojonki/61f47f9446e4880ebfba154e406ab3de to your computer and use it in GitHub Desktop.

Select an option

Save jojonki/61f47f9446e4880ebfba154e406ab3de to your computer and use it in GitHub Desktop.

Revisions

  1. jojonki revised this gist Dec 11, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion plotly_orca_example.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    """Use plotly orca (Docker) to save a figure as png and html files.
    """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
  2. jojonki revised this gist Dec 11, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion plotly_orca_example.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    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 to proper import.
    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
  3. jojonki revised this gist Dec 11, 2019. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions plotly_orca_example.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    """Use plotly orca (Docker)
    """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
    @@ -12,27 +12,29 @@
    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, file_name, figure):
    def dumpImage(plotly_server, filename, figure, img_format='png'):
    request_params = {
    'figure': figure.to_dict(),
    'format': 'png', # any format from 'png', 'jpeg', 'webp', 'svg', 'pdf', 'eps'
    '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(file_name)
    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.png', fig)
    dumpImage(plotly_server, 'out', fig)


    if __name__ == '__main__':
  4. jojonki created this gist Dec 11, 2019.
    39 changes: 39 additions & 0 deletions plotly_orca_example.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    """Use plotly orca (Docker)
    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 to 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.utils import PlotlyJSONEncoder


    def dumpImage(plotly_server, file_name, figure):
    request_params = {
    'figure': figure.to_dict(),
    'format': 'png', # 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(file_name)


    def main():
    plotly_server = 'http://localhost:9091'
    fig = go.Figure(data=[go.Bar(y=[2, 3, 1])])
    dumpImage(plotly_server, 'out.png', fig)


    if __name__ == '__main__':
    main()