I want to embed a nnv render in flask but my code semms to not be rendering the neural-network diagram.
My code:
JavaScript
x
22
22
1
from nnv import NNV
2
3
layersList = [
4
{"title":"input", "units": 3, "color": "darkBlue"},
5
{"title":"hiddennlayer", "units": 3},
6
{"title":"output", "units": 6,"color": "darkBlue"},
7
]
8
9
10
11
12
from flask import Flask
13
14
app = Flask(__name__)
15
16
@app.route('/')
17
def index():
18
return f"{NNV(layersList).render()}"
19
20
if __name__ == '__main__':
21
app.run()
22
Advertisement
Answer
The problem is that matplotlib is used to plot the neural network graph; in order to return an image as an HTTP response, you need to convert the plot to bytes.
JavaScript
1
9
1
import io
2
from flask import Flask, Response
3
4
@app.route('/')
5
def index():
6
output = io.BytesIO()
7
NNV(layersList).render(save_to_file=output)
8
return Response(output.getvalue(), mimetype='image/png')
9