I am trying to upload plotly image via api

Hi, I am trying to to upload plotly image via api. I am trying to convert that image as binary then upload it but my code get stucks at pio.to_image and i have tried .write_image also and same issue and i have tried different methods but sometimes it just uploads blank png image. Any suggestions how to fix this

And this is my code to generate plot

def create_stacked_bar_chart(df, x_columns, y_columns, hover_data):
“”"summary

Args:
    df (_type_): _description_
    x_columns (_type_): _description_
    y_columns (_type_): _description_

Returns:
    _type_: _description_
"""
# Create a trace for each experiment
traces = []

for i, exp_num in enumerate(df['Exp #'].unique()):
    exp_df = df[df['Exp #'] == exp_num]
    x_labels = exp_df.apply(lambda row: ' - '.join(str(row[x]) for x in x_columns), axis=1).tolist()
    trace = go.Bar(
        x=x_labels,
        y=exp_df[y_columns],
        name=f'Experiment {exp_num}',
        text=exp_df[y_columns],
        textposition='auto',
    )
    traces.append(trace)

fig = go.Figure(data=traces)

fig.update_layout(
    title='Stacked Bar Chart for',
    xaxis_title='Reaction Conditions',
    yaxis_title= y_columns,
    barmode='stack',  # Use 'stack' for a stacked bar chart
    legend_title_text='Experiments',
    plot_bgcolor="rgba(255, 255, 255, 255)",
    showlegend=True,
    width=1000,
    height=600,
    xaxis_showgrid=False,  # Remove x-axis grid lines
    yaxis_showgrid=False,  # Remove y-axis grid lines
)

return fig

and this code is to upload image

def upload_png_to_api(png_filename, API_BASE_URL, API_KEY):
try:
# Convert the Plotly figure to an image in bytes format
img_bytes = pio.to_image(fig, format=“png”)

        # Create a file-like object from the bytes data
        img_file = io.BytesIO(img_bytes)

        # Create a dictionary containing the file (image) to be uploaded
        files = {'file': ('chart.png', img_file)}


        headers = {
            'x-api-key': API_KEY
        }

        # In the request body, include the PNG file data
        response = requests.post(API_BASE_URL, headers=headers, files=files)

        if response.status_code == 201:
            st.write("PNG image uploaded successfully.")
            return response.json()  # You can return the API's response if it provides any
        else:
            st.write(f"Failed to upload PNG image. Status code: {response.status_code}")
            return None
except Exception as e:
    st.write(f"Error uploading PNG image: {str(e)}")
    return None

HI @Catsci welcome to the forums.

Are you working on a Windows machine? Without having really read your code, but this might be related to kaleido. We always have similar topics with exporting plotly figures to static images unfortunately.

https://community.plotly.com/search?q=kaleido%20export