Plotly fig.write_html not working with Docker

Hello Plotly Community,

I am working on developing a Flask App using Plotly. Right now, when I run the app on my local computer the fig.write_html(“figure.html”, auto_open=True) will launch the new html page containing the Plotly figure; however, when I take the same code and run it with Docker no page is launched. I attempted to fix this by having fig.write_html(os.getcwd() + "/templates/figure.html") and then have the @app.route return render_template("figure.html") and that works… the first time. If you return to the homepage and run through the app again, the site will always launch the figure from the first run.

I attempted to fix this by having the app check for the figure.html file within the template fold and delete if present with each visit to the index but that doesn’t fix the problem either. I have also attempted to clear the cache for the site between runs and that doesn’t seem to work either.

Any advise on this topic would be greatly appreciated.

I ended up figuring it out so I will post this here in case someone runs across this in the future.

Within your @app-route you can write:

def see_data ():
    filename = request.form['output_file']
    df_file="."+filename
    df=np.loadtxt(df_file, skiprows=1, usecols=[2],dtype=str)
    fig = go.Figure(data=go.Bar(y=df,marker_color="#1359c2"))
    fig.update_layout(xaxis=dict(title="Sequence"),yaxis=dict(title="Z-SCORE"))
    run_filename= filename[8:] + "_figure.html"
    fig.write_html('./templates'+run_filename)
    return render_template(run_filename)

And then within your function for the index @app.route include:

    for document in os.listdir('./templates/'):
        if document.endswith('figure.html'):
            os.remove('./templates/' + document)

This will write the plotly figure.html to your template folder for each unique run so that it can be accessed by Flask and then remove it so that Flask doesn’t call upon it in the future.