I have seen similar questions up before without answer.
The aim is to show the image on the Flask application but with the altered config.
How is the config setting on Plotly charts altered when converting the graph into JSON and not using the fig.show()? If the fig.show() was used then the config would be amended inside there.
Flask Code:
Of course, if this was just to show the graph fig.show would be used but this is passing it through to JSON.
#example flask application with graph
from flask import Flask, render_template, request
import plotly.express as px
import plotly
import numpy as np
import json
#Flask settings.
secret_key = 'verysecretkey123lol' #secret key
app = Flask(__name__)
#simple image
img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[0, 255, 0], [0, 0, 255], [255, 0, 0]]
], dtype=np.uint8)
fig = px.imshow(img_rgb)
#Flask
@app.route("/", methods=['GET', 'POST'])
def create():
image = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return render_template('example.html', image = image)
#Run
if __name__ == "__main__":
app.secret_key = secret_key
app.run(debug = False)
HTML Code (example.html):
I have added the Config as a javascript variable below.
<!DOCTYPE html>
<html>
<head lang="en">
<title>Dissertation</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<div class="chart" id="image">
<script>
var graphs = {{image | safe}};
var config = {"displaylogo": FALSE, 'modeBarButtonsToAdd': ['drawclosedpath', 'eraseshape']};
Plotly.plot('image', graphs, {});
</script>
</div>
</body>
</html>
Any help would be greatly appreciated, thanks
Andrew