Plotly save as pdf

I want to be able to save a plotly as a pdf

Steps to reproduce

Code snippet:

if st.button("Run " + str(test) + " Test Tool"):
	
    # use specs parameter in make_subplots function to create secondary y-axis
    fig = make_subplots(specs=[[{"secondary_y": True}]])
    
    # plot a scatter chart by specifying the x and y values
    # Use add_trace function to specify secondary_y axes.
    fig.add_trace(
        go.Line(x=df.index, y=df[' Power'], name="KW"),
        secondary_y=False)
    
    # Use add_trace function and specify secondary_y axes = True.
    fig.add_trace(
        go.Line(x=df.index, y=df['Hz'], name="Hz"),
        secondary_y=True,)
    
    # Adding title text to the figure
    fig.update_layout(
        title_text="Hz Vs KW"
    )
    
    # Naming x-axis
    fig.update_xaxes(title_text="Datetime")
    
    # Naming y-axes
    fig.update_yaxes(title_text="<b>KW</b> axis ", secondary_y=False)
    fig.update_yaxes(title_text="<b>Hz</b> axis ", secondary_y=True)

    #Ensure line is connected
    fig.update_traces(connectgaps=True)

    st.plotly_chart(fig)
    
if st.button("Save Excel Data"):
        save_path = "../../data/output/" + test + '/' + customer
        ensure_dir(save_path)
        df.to_excel(save_path +'_' + datetime.datetime.today().strftime("%d-%m-%Y") + '_' + test + '.xlsx')
        pio.write_image(fig, save_path + datetime.datetime.today().strftime("%d-%m-%Y") + '_' + test + '_' + customer + '.pdf')

The excel save works but the pdf save keeps failing.

When you click the second button, the first one reverts back to False, so the fig is not created. You can try keeping the figure in the st.session_state so it persists.


Check this other post: How to do nested button and print both button outputs? - #2 by mathcatsand

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.