Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Upload plotly image to AWS s3 Bucket

I have this code, and I want to upload the output PNG figure to the S3 bucket:

import plotly.graph_objects as go
import numpy as np
np.random.seed(1)

N = 100
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sz = np.random.rand(N) * 30

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=x,
    y=y,
    mode="markers",
    marker=go.scatter.Marker(
        size=sz,
        color=colors,
        opacity=0.6,
        colorscale="Viridis"
    )
))

fig.write_image("fig1.png")

How can I do that? FYI

fig.write_image("fig1.png") 

will convert the Plotly figure to a PNG image

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do that by using the .put_object() method.

# generate an S3 key for the resulting plot
Bucket_Name = 'your_S3_Bucket_name'                 
image_name = 'TestImage.png'

# Write the image on the S3
img_data = BytesIO()          
plot.write_image(img_data, format='png')  
img_data.seek(0)    
        
# put plot in S3 bucket            
bucket = boto3.resource('s3').Bucket(Bucket_Name)     
bucket.put_object(Body=img_data, ContentType='image/png', Key=image_name)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading