Skip to content
Advertisement

How do i embed nnv diagram in flask?

I want to embed a nnv render in flask but my code semms to not be rendering the neural-network diagram.

My code:

from nnv import NNV

layersList = [
    {"title":"input", "units": 3, "color": "darkBlue"},
    {"title":"hiddennlayer", "units": 3},
    {"title":"output", "units": 6,"color": "darkBlue"},
]




from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return f"{NNV(layersList).render()}"

if __name__ == '__main__':
    app.run()

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.

import io
from flask import Flask, Response

@app.route('/')
def index():
    output = io.BytesIO()
    NNV(layersList).render(save_to_file=output)
    return Response(output.getvalue(), mimetype='image/png')
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement